Public Group

# Basic math question

This topic is 3549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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?

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631740
• Total Posts
3001964
×