Sign in to follow this  
NukeCorr

Basic math question

Recommended Posts

With what simple code I could move object from point A to point B so that the object reaches the point's x and y destination at the same time? eg. from Point A: 10,20 to Point B: 80,50 or any other coordinates. Thanks.

Share this post


Link to post
Share on other sites
Get B relative to A. Do this by simply subtracting A from B. Then divide that vector by the time, and that's how far you need to move per unit of time.

eg. A = {10, 20}, B = {80, 50}, want to get from A to B in 10 seconds

AB = B - A = {70, 30}
AB / 10 = {7, 3}

Therefore, you need to move {7, 3} per second to get from A to B in 10 seconds.

Share this post


Link to post
Share on other sites
Sc4Freak: I didn't even think about it that way. Usually I have a speed and a final location, so I take the relative X and Y components between the two points, theta=ATAN2(Y, X) to find the angle, then modify my character's current X and Y locations with cos(theta)*speed*incrementedTime and sin(theta)*speed*incrementedTime. I was programming in Java at the time, and I know that trig functions take a significant amount of time, but I didn't know of any alternative.

Your solution could make it a much quicker process.

//Get the relative X/Y components
AB = B - A = {70, 30}

//Find the distance between the two
C = sqrt(A^2 + B^2)

//Determine how many seconds it will take to get there
Time = C / Speed

//Then, the move per second speed
dXdY = AB / Time

I don't know how much of a time difference there is, but for simple movement schemes, this would work nicely. The only problem I have is that I try to make simulations that deal with VelocityXY and ForceXY, so when there are multiple forces acting on an object (an explosion pushing it one direction while trying to move in another), I end up defaulting back to trig functions. Is there a better way to deal with this?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this