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
...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."
Offline
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
You know, you could have this long complicated formula with trig or a long complicated formula with if-then and slope.
Take a pick.
Offline
1. We can easily discover the 45, 135, 225 (-135), and 315 (-45) degree.
[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)
Offline
^^ Precisely. Give me a minute to get pen and paper and I'll give you the answer
Offline
D = tan-1 of y/x
Now testing.
Offline
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
Offline
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
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
i think this should be in all about scratch.
Offline
ok thanks for all the help guya! didn't know you could get this much math help in the scratch forums
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...
Last edited by zxz1661 (2011-10-04 15:21:26)
Offline
zxz1661 wrote:
ok thanks for all the help guya! didn't know you could get this much math help in the scratch forums
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...![]()
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
joefarebrother wrote:
i think this should be in all about scratch.
No, this is definitely too advanced for AAS.
Offline
midnightleopard wrote:
aw man. I love trig. I wish saw this earlier!
I wish you were here now too...
Offline
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
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:
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)
Offline
Err... I hate to sound ignorant, but isn't it just this:
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)
}
}Offline
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
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