Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Question about making something "move towards a point" and calculating speed.  (Read 2084 times)

0 Members and 1 Guest are viewing this topic.

Mamoruanime

@Mamoruanime
Question about making something "move towards a ...
« on: November 13, 2010, 07:19:05 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
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
Logged
Re: Question about making something "move toward...
« Reply #1 on: November 13, 2010, 07:57:42 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
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.
« Last Edit: November 13, 2010, 08:12:18 am by Niek »
Logged

Xiphirx

wat
Re: Question about making something "move toward...
« Reply #2 on: November 13, 2010, 08:24:32 am »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
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:
Logged
  • For The Swarm

Mamoruanime

@Mamoruanime
Re: Question about making something "move toward...
« Reply #3 on: November 13, 2010, 08:27:53 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
Wait; why would Niek's only work for 90 degree angles? You can independently calculate the x and y coords D:
Logged
Re: Question about making something "move toward...
« Reply #4 on: November 13, 2010, 08:33:27 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
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.
« Last Edit: November 13, 2010, 08:37:05 am by Niek »
Logged

Mamoruanime

@Mamoruanime
Re: Question about making something "move toward...
« Reply #5 on: November 13, 2010, 08:35:06 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
That's what I figured; since you're defining x and y independently, it wouldn't make sense for it to *not* work :P
Logged
Re: Question about making something "move toward...
« Reply #6 on: November 13, 2010, 04:49:46 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
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.
Logged



i love big weenies and i cannot lie

Xiphirx

wat
Re: Question about making something "move toward...
« Reply #7 on: November 13, 2010, 07:32:31 pm »
  • Xiphirx
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3007
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.
Logged
  • For The Swarm
Re: Question about making something "move toward...
« Reply #8 on: November 13, 2010, 09:13:40 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
EDIT: Disregard this, I suck !@#$%.
« Last Edit: November 13, 2010, 09:16:40 pm by MG-Zero »
Logged



i love big weenies and i cannot lie
Pages: [1]   Go Up

 


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



Page created in 0.064 seconds with 55 queries.

anything