This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-10-03 19:11:15

zxz1661
Scratcher
Registered: 2010-01-06
Posts: 52

Trigonometry in scratch?

I need help with trigonometry in my scratch game.

So what i need to do is have my sprite point in the direction it is moving (with variables that store the sprites x and y velocity).

Example: If the sprites y velocity and x velocity are both the same number (lets say 5 for example), the script would make it point in a 45 degree angle, and point accordingly to other values the x and y velocities may give.

What trig function/equation could do this? Is it in Scratch? If not, is there any other way to do this?

Thanks in advance! I dont know if anybody will be able to help me...

Offline

 

#2 2011-10-03 19:21:58

bbbeb
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: Trigonometry in scratch?

...imo, it's not clear enough.
especially...
" the script would make it point in a 45 degree angle, and point accordingly to other values the x and y velocities may give."


Back in my day.... there were no laws that censored the internet... now, there are.... nah.

Offline

 

#3 2011-10-03 19:26:34

zxz1661
Scratcher
Registered: 2010-01-06
Posts: 52

Re: Trigonometry in scratch?

What do you mean? I think it is perfectly clear. I will try rewording it for you. Is there a trigonometric equation that points a sprite in the direction is moving? For example, if the sprite was going to change its y by 5 and x by 5, where would you plug in those number to make it point in the right direction (in this case, 45 degrees)

Offline

 

#4 2011-10-03 19:29:53

bbbeb
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: Trigonometry in scratch?

Ohhhhh.

IDK atm, I'll try and think about it...

HOLD UP LOL
SLOPE



Do you know slope?


Back in my day.... there were no laws that censored the internet... now, there are.... nah.

Offline

 

#5 2011-10-03 19:33:00

zxz1661
Scratcher
Registered: 2010-01-06
Posts: 52

Re: Trigonometry in scratch?

thats not what this is about. If your talking about the equation "y=mx+b", that is. Im talking about TRIGONOMETRY. TRIG.

Last edited by zxz1661 (2011-10-03 19:34:22)

Offline

 

#6 2011-10-03 19:42:08

bbbeb
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: Trigonometry in scratch?

You know, you could have this long complicated formula with trig or a long complicated formula with if-then and slope.
Take a pick.


Back in my day.... there were no laws that censored the internet... now, there are.... nah.

Offline

 

#7 2011-10-03 19:42:39

zxz1661
Scratcher
Registered: 2010-01-06
Posts: 52

Re: Trigonometry in scratch?

ok slope.

Offline

 

#8 2011-10-03 19:47:00

bbbeb
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: Trigonometry in scratch?

1. We can easily discover the 45, 135, 225 (-135), and 315 (-45) degree.

Code:

[if <x pos - y pos = 0>]
[if x > 0 and y > 0]
[set direction to 135]
[/if]
[if x > 0 and <not y > 0>>]
[set direction to 45] 
[/if]
[if <not <x > 0 and y > 0>>]
[set direction to -135]
[/if]
[if <not <x > 0>> and <y > 0>]
[set direction to -45]
[/if]
[/if]

That's for now.
I'ma do the rest soon.

Last edited by bbbeb (2011-10-03 19:47:58)


Back in my day.... there were no laws that censored the internet... now, there are.... nah.

Offline

 

#9 2011-10-04 07:58:49

zxz1661
Scratcher
Registered: 2010-01-06
Posts: 52

Re: Trigonometry in scratch?

That is going to be way too long. One line of a trig equation and its done.

Offline

 

#10 2011-10-04 11:16:33

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Trigonometry in scratch?

^^ Precisely. Give me a minute to get pen and paper and I'll give you the answer  smile

Offline

 

#11 2011-10-04 11:18:29

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Trigonometry in scratch?

D = tan-1 of y/x

Now testing.

Offline

 

#12 2011-10-04 11:19:19

roijac_test
Scratcher
Registered: 2011-08-31
Posts: 49

Re: Trigonometry in scratch?

ok, i'm not sure, but using arctan(hspeed/vspeed) should help. try add 90/180/270 degrees if its not working, or switch hspeed with vspeed. i don't have scratch at the moment so i can't check it


http://gigabyte.50webs.com/funnyerrormessages/work013.jpg

Offline

 

#13 2011-10-04 11:28:44

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Trigonometry in scratch?

you're right, roijac.
It is indeed x before y.

Direction is atan(x/y). However, it glitches in the "lower hemisphere", if you wish  tongue  You'll probably need to add an if statement to it.

Also, to avoid errors when dividing by zero, add an if to check if Y vel is zero.

Offline

 

#14 2011-10-04 11:34:11

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

Re: Trigonometry in scratch?

i think this should be in all about scratch.


My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#15 2011-10-04 15:05:16

zxz1661
Scratcher
Registered: 2010-01-06
Posts: 52

Re: Trigonometry in scratch?

ok thanks for all the help guya! didn't know you could get this much math help in the scratch forums  wink  ill test it out

EDIT: i noticed, as you said, it gets very messed up in the 'lower hemishpere'...which i cant find a workaround for... sad

Last edited by zxz1661 (2011-10-04 15:21:26)

Offline

 

#16 2011-12-02 11:30:39

Splodgey
Scratcher
Registered: 2011-04-26
Posts: 500+

Re: Trigonometry in scratch?

zxz1661 wrote:

ok thanks for all the help guya! didn't know you could get this much math help in the scratch forums  wink  ill test it out

EDIT: i noticed, as you said, it gets very messed up in the 'lower hemishpere'...which i cant find a workaround for... sad

I've found a way:

if <(y position) > [0]>
 point in direction ([atan v] of (([0] - (x position)) / ([0] - (y position))))
else
 point in direction ([0] - ([atan v] of (([0] - (x position)) / ([0] - (y position)))))

Last edited by Splodgey (2012-03-10 05:53:19)

Offline

 

#17 2011-12-02 11:42:12

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: Trigonometry in scratch?

joefarebrother wrote:

i think this should be in all about scratch.

No, this is definitely too advanced for AAS.


Why

Offline

 

#18 2011-12-02 16:21:11

midnightleopard
Scratcher
Registered: 2007-09-13
Posts: 1000+

Re: Trigonometry in scratch?

aw man. I love trig. I wish saw this earlier!


http://pwp.wizards.com/5103673563/Scorecards/Landscape.png

Offline

 

#19 2011-12-03 06:24:11

Splodgey
Scratcher
Registered: 2011-04-26
Posts: 500+

Re: Trigonometry in scratch?

midnightleopard wrote:

aw man. I love trig. I wish saw this earlier!

I wish you were here now too...

Offline

 

#20 2011-12-05 05:00:43

eventexception
Scratcher
Registered: 2011-04-08
Posts: 500+

Re: Trigonometry in scratch?

Offline

 

#21 2011-12-11 17:52:05

VirtualClown
Scratcher
Registered: 2011-08-05
Posts: 55

Re: Trigonometry in scratch?

Set Variable To X, Set other variable to Y, wait until not x=variable and y-other variable, point in direction (asin of variable+acos of other variable)/2

Offline

 

#22 2011-12-12 03:01:39

floppy_gunk
Scratcher
Registered: 2008-11-14
Posts: 500+

Re: Trigonometry in scratch?

It like you want what is commonly known as the atan2 function. It's like atan(y/x) but it accepts x and y individually. Normal atan wouldn't work because the it gets messed up in the lower hemisphere. You could use the standard definition, which would be complicated to program, or you could use:
http://upload.wikimedia.org/wikipedia/en/math/2/c/d/2cdc5069481f6367badd314ee7c80e65.png
and define a special case for when x=0. (Remember to swap x and y, as rotation in Scratch is different than in math) This is my preferred method, as it's relatively smaller. It's probably a bit slower, but Scratch is slow anyway, so there's not much difference there.

Last edited by floppy_gunk (2011-12-12 03:09:30)


http://img163.imageshack.us/img163/1917/2856lg.jpg Get it now!  smile

Offline

 

#23 2011-12-12 07:33:50

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Trigonometry in scratch?

Err... I hate to sound ignorant, but isn't it just this:

Code:

if( Yvelocity > 0 )
{
     point in direction ( atain (Yvelocity / Xvelocity) )
}
else
{
     if()
     {
          point in direction ( atan (Yvelocity / Xvelocity) + 180 )
     }
     else
     {
          point in direction ( abs of (Xvelocity / Xvelocity) * 90)
     }
}

http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#24 2012-03-10 05:54:37

Splodgey
Scratcher
Registered: 2011-04-26
Posts: 500+

Re: Trigonometry in scratch?

MoreGamesNow wrote:

Err... I hate to sound ignorant, but isn't it just this:

Code:

if( Yvelocity > 0 )
{
     point in direction ( atain (Yvelocity / Xvelocity) )
}
else
{
     if()
     {
          point in direction ( atan (Yvelocity / Xvelocity) + 180 )
     }
     else
     {
          point in direction ( abs of (Xvelocity / Xvelocity) * 90)
     }
}

Um.. I thought it was

if <(y position) > [0]>
 point in direction ([atan v] of (([0] - (x position)) / ([0] - (y position))))
else
 point in direction ([0] - ([atan v] of (([0] - (x position)) / ([0] - (y position)))))

Offline

 

#25 2012-03-10 11:24:36

rdococ
Scratcher
Registered: 2009-10-11
Posts: 1000+

Re: Trigonometry in scratch?

Converting x and y velocities to direction

It's simpler than that...

Use

([asin v] of ((x vel) / (speed)))
or

([acos v] of ((y vel) / (speed)))
That will work very well, provided that both values are not exactly equal to the speed all the time. If the direction was 45, and the speed was 5, then both x and y velocities must be 2.5.

Last edited by rdococ (2012-03-10 11:59:58)

Offline

 

Board footer