3 Replies Latest reply on May 17, 2006 3:04 PM by blemmo

# Finding the consistant slope

I am trying to have a ball move from one side of the stage to the exact x and y points on the other side of the stage. For example, let's say the ball's x =25 and y=80, and the point I want it to move it to is at point x=350 and y=100. So if I want this ball to travel at a consistant speed (doesn't matter), how would I do this?

Does this make sense? It's simliar to a movie clip following the mouse, sort of...help please.
• ###### 1. Re: Finding the consistant slope
Is the speed fixed? If not, you just had to change the x and y position constantly, but I guess you wouldn't have asked then.
So when it's fixed, you need to know the distance from start to end point. As you know both points, you can use
distance = Math.sqrt( Math.pow(startPoint._x - endPoint._x, 2) + Math.pow(startPoint._y - endPoint._y, 2) );
this gets the length of the line between those points in pixels.
Then you can get the time it will take to go that distance in the given speed:
time = distance / speed;

Now if the speed is something like 50 pixels per second, the time is also in seconds, and you will get something like 5 seconds. Now take the animation speed (e.g. the framerate, if you animate with onEnterFrame), and get the number of animation steps it will take to reach the end:
steps = time * animation_speed

Finally get the total distances for x and y, which is endPoint.(x | y) - startPoint.(x | y), and divide those by the steps number to get the distance for each step:
stepX = (endPoint._x - startPoint._x) / steps;
stepY = (endPoint._y - startPoint._y) / steps;

Now you have the distance for each animation step and can use them in the animation to move the ball with the given speed.

hth,
blemmo
• ###### 2. Re: Finding the consistant slope
THANK YOU!