Advertisement

Calculate speed to a point

Started by December 29, 2002 10:47 PM
1 comment, last by doogle 22 years, 1 month ago
This is annoying me now, because I thought I could work it out myself. I have one object at point x1, y1 that wants to get to point x2, y2. I knows it needs to get there at a certain speed, ie speed=2. How can I work it out??? It should be sooo simple. I have tryed lots of different methods, but nothing seems to work. I can work out the actual distance from point 1 to point 2 using pythagorus (sp?), and I know trig. I don''t need a total answer, but any hints? Thanks a lot
Speed describes the length of a velocity vector. In 2D, your basic velocity vector consists of 2 components, an X and a Y. You can think of speed as the hypotenuse of a triangle, and X and Y being the sides of that triangle.

You have the two points, so what we have here is similar triangles. The distance between the two points is the hypotenuse of a larger triangle. The corresponding X and Y distances between the two points is the sides of the larger triangle. If we compare the distance between the points to the speed we wish to go at, we can use ratios to find the X and Y components of the velocity.

We find the ratio of the speed to the distance:
float ratio = speed / distance_between_points;

We then multiply that ratio by the X and Y distances between the points:
float x_velocity = ratio * (x2 - x1);
float y_velocity = ratio * (y2 - y1);


And there ya go!
Advertisement
Ok, thanks a lot. Seems to work now...

I was close:
I was working out a ratio and multiplying it by the respective distances, but I was just working out the ratio wrong

Thanks again

This topic is closed to new replies.

Advertisement