ZFGC

Resources => Coding => Topic started by: Mamoruanime on November 13, 2010, 07:19:05 am

Title: Question about making something "move towards a point" and calculating speed.
Post by: Mamoruanime on November 13, 2010, 07:19:05 am
I'm no good with math, so this definitely perplexes me.

Lets say I have an object with a position of 128,256 and I want it to move towards another object with a position of 64x32 with a speed of 4 pixels per frame. How exactly do I determine the amount of x it must travel vs the amount of y it must travel to maintain a speed of 4 pixels per frame (in such a way that it's moving exactly the same speed as if it were just moving along the y axis or just the x axis respectively)?

Sure I could cop out and simply subtract from x and y each frame, but that would look really weird, and it would completely ignore the speed factor.

Insight on this crazy "probably needs sqr's" dilemma would be much appreciated ;p
Title: Re: Question about making something "move towards a point" and calculating speed.
Post by: Zaeranos on November 13, 2010, 07:57:42 am
ms = movement speed (4)
dx = difference between the x coordinates
dy = difference between the y coordinates

move_x = ms ( dx / (sqrt( dxdx  + dydy ) )
move_y = ms ( dy / (sqrt( dxdx + dydy ) )

That is with the Pythagorean theorem. I don't know if there is any easier way to do it, without the use of sqrt. Maybe using matrices, but I'm not to sure about that.
Title: Re: Question about making something "move towards a point" and calculating speed.
Post by: Xiphirx on November 13, 2010, 08:24:32 am
ms = movement speed (4)
dx = difference between the x coordinates
dy = difference between the y coordinates

move_x = ms ( dx / (sqrt( dxdx  + dydy ) )
move_y = ms ( dy / (sqrt( dxdx + dydy ) )

That is with the Pythagorean theorem. I don't know if there is any easier way to do it, without the use of sqrt. Maybe using matrices, but I'm not to sure about that.

That only works for 90 degree angles...

x += speed*cos(atan2(64,32));
y += speed*sin(atan2(64,32));

? D: Probably wrong, I haven't messed with cos and sin in a while D:
Title: Re: Question about making something "move towards a point" and calculating speed.
Post by: Mamoruanime on November 13, 2010, 08:27:53 am
Wait; why would Niek's only work for 90 degree angles? You can independently calculate the x and y coords D:
Title: Re: Question about making something "move towards a point" and calculating speed.
Post by: Zaeranos on November 13, 2010, 08:33:27 am
ms = movement speed (4)
dx = difference between the x coordinates
dy = difference between the y coordinates

move_x = ms ( dx / (sqrt( dxdx  + dydy ) )
move_y = ms ( dy / (sqrt( dxdx + dydy ) )

That is with the Pythagorean theorem. I don't know if there is any easier way to do it, without the use of sqrt. Maybe using matrices, but I'm not to sure about that.

That only works for 90 degree angles...

x += speed*cos(atan2(64,32));
y += speed*sin(atan2(64,32));

? D: Probably wrong, I haven't messed with cos and sin in a while D:
But if you have only two points and you need a third point to get a triangle. Just pick the x from one point and the y from the other and you have your 90 degrees angle. The 90 angle point is not one of the two given points and pretty much irrelevant.

Mammy it works fine. And you would prefer to use only once sqrt, instead of twice atan2 + cos + sin in your game.
Title: Re: Question about making something "move towards a point" and calculating speed.
Post by: Mamoruanime on November 13, 2010, 08:35:06 am
That's what I figured; since you're defining x and y independently, it wouldn't make sense for it to *not* work :P
Title: Re: Question about making something "move towards a point" and calculating speed.
Post by: MG-Zero on November 13, 2010, 04:49:46 pm
Wait; why would Niek's only work for 90 degree angles? You can independently calculate the x and y coords D:

Pythagorean Theorem only works with triangles with a 90 degree angle in them.  The same goes for SohCahToa.  Otherwise you need to use law of sines and cosines.  But yea, Niek's formula looks pretty good...you'll get a dimensionless value for the amount of distance in both x and y directions and then multiply that by the speed to get individual velocities.
Title: Re: Question about making something "move towards a point" and calculating speed.
Post by: Xiphirx on November 13, 2010, 07:32:31 pm
Oh I see, you're creating a triangle with a right angle every time. I never thought of it that way lol thanks for the info.
Title: Re: Question about making something "move towards a point" and calculating speed.
Post by: MG-Zero on November 13, 2010, 09:13:40 pm
EDIT: Disregard this, I suck !@#$%.

Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved