Sign in to follow this  
crypozza

Please help with moving a particle in 2D space.

Recommended Posts

Here is my situation. I have a 2D Point (StartX, StartY) that start moving from a random location, with a random speed and random direction. I need to move this point to the certain location (DestX, DestY). It looks like: X += Velocity.X Y += Velocity.Y But i don't kown how to deal with Velocity.X and .Y to get my point moving to the right location (DestX, DestY).

Share this post


Link to post
Share on other sites
If I understand your question correctly, the following might do what you're asking:
velocity += normalize(target - position) * k * dt;
Where dt is the time step, and k is an appropriate constant.

This is basically equivalent to applying a force to the particle in the direction of the target position. The above equation assumes the particle has a mass of 1, but you can change that if you wish. (You might also need to add drag or friction - or just clamp the speed manually - to keep the particle from accelerating indefinitely).

Share this post


Link to post
Share on other sites
Your on the right track! so don't give up. Your main issue is determining the "vector" between your point and the destination point. This is easy to do, to get the vector, you simply subtract the x and y of your point from the x and y of the destination

vx = destx - pointx
vy = desty - pointy

thats not enough however, we need to normalize those vectors, this means that all the values for that vector will be made to be less than our equal to 1. I will get to why we do this in a sec, first, how to do it.

distance = SqRt((vx * vx)^2 + (vy * vy)^2)

By taking the vectors, and squaring them, adding them together, and then square rooting it, we get the distance between the two points. Now we normalize the vector by dividing each vector by the distance:

vx = (vx / distance)
vy = (vy / distance)

Using the math shown above:

Point Position = 10, 20
Destination Position = 0, 0

Direction Vector = -10, -20
Distance = 22.36, the squareroot of 500 = (10 * 10) + (20 * 20)
Normalized Vector = .44, .89

from here its just a matter of multiplying your velocity with the normalized vector.

X += Velocity.X * normalX
Y += Velocity.X * normalY

Your point should then move towards the destination. Hope this explains and helps, and isn't too confusing. If I need to explain something let me know.

Share this post


Link to post
Share on other sites
Actually, it should do exactly what I intend it to, or else what I'm working on shouldn't work. I'll do it within one dimension so it makes more sense.

We have 2 points, A and B
A = 10
B = 0

The distance/magnitude of the vector going from point a to point b is -10, therefore the vector pointing toward b is -1, if we apply a force like acceleration to our point, such as 2 per step, then it'll take 5 steps to move to our destination.

Start point = 10
Acceleration = 2
Normal Vector = -1
Velocity = 2 * -1 or -2
which moves the point 2 steps closer to 0.

Therefore in my example we had a distance/magnitude of 22.36, and a normalized XY of .44, and .89. By multiplying a acceleration with these, such as 10, we get:

Start Position: 10, 20
Normal Vector: -4.4, -8.9
New Position: 5.6, 11.1

Using the same distance formula i explained above
(4.4 * 4.4) + (8.9 * 8.9) = 98.87
The squareroot of 98.87, is roughly 9.9, and it would probably be 10 if I was being more accurate with the math. This means the total distance traveled from our Start Position to our New Position, we get a total distance traveled of nearly 10, which is, not surprising, the exact amount I wanted to move with an acceleration of 10.

When we normalize the vector, we make it so that, with the values in that vector, we can move the vector towards a point, by passing it values, so for our vector, .44 and .89, if we passed a velocity of 1, it would move our particle .44, .89 closer to our destination. if we passed a velocity of 22.36, we would move our particle all the way to the destination point instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by neoaikon
Actually, it should do exactly what I intend it to, or else what I'm working on shouldn't work. I'll do it within one dimension so it makes more sense.

We have 2 points, A and B
A = 10
B = 0

The distance/magnitude of the vector going from point a to point b is -10, therefore the vector pointing toward b is -1, if we apply a force like acceleration to our point, such as 2 per step, then it'll take 5 steps to move to our destination.

Start point = 10
Acceleration = 2
Normal Vector = -1
Velocity = 2 * -1 or -2
which moves the point 2 steps closer to 0.

Therefore in my example we had a distance/magnitude of 22.36, and a normalized XY of .44, and .89. By multiplying a acceleration with these, such as 10, we get:

Start Position: 10, 20
Normal Vector: -4.4, -8.9
New Position: 5.6, 11.1

Using the same distance formula i explained above
(4.4 * 4.4) + (8.9 * 8.9) = 98.87
The squareroot of 98.87, is roughly 9.9, and it would probably be 10 if I was being more accurate with the math. This means the total distance traveled from our Start Position to our New Position, we get a total distance traveled of nearly 10, which is, not surprising, the exact amount I wanted to move with an acceleration of 10.

When we normalize the vector, we make it so that, with the values in that vector, we can move the vector towards a point, by passing it values, so for our vector, .44 and .89, if we passed a velocity of 1, it would move our particle .44, .89 closer to our destination. if we passed a velocity of 22.36, we would move our particle all the way to the destination point instead.
I understand what you're saying, but I think you're confusing 'speed' with 'velocity'.

In other words, the concept is correct, but the terminology you're using is wrong.

Share this post


Link to post
Share on other sites
I'll mention, that simply applying an accceleratng force directed towards the target will in many cases result in your object 'orbiting' the target instead of arriving at it. Especially if it has some inital velocity directed elsewhere.

It's the velocity, not the acceleration that needs to aim at the target.
think along the lines of: acceleration = goalvelocity-oldvelocity

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