How to move a body to a specific position

10 views
Skip to first unread message

AJ

unread,
Mar 25, 2010, 1:50:35 PM3/25/10
to Physics2D.Net
I'd like to smoothly move a body to a specific location when the user
presses a button. What is the best way to achieve this?

JonoPorter

unread,
Mar 25, 2010, 2:46:37 PM3/25/10
to Physics2D.Net
Depends on what you mean by smooth.
Do you want it to move at a constant Acceleration to get there?
Or do you want it to move at a constant Velocity to get there?
Do you want it to be at a complete stop when it gets there?

AJ

unread,
Mar 25, 2010, 2:59:49 PM3/25/10
to Physics2D.Net
Let's say constant acceleration with a complete stop when it gets
there...

JonoPorter

unread,
Mar 25, 2010, 5:34:53 PM3/25/10
to Physics2D.Net
Ok, there is no built in way to do this. But you can write it.
This is how I would do it:
copy the VelocityLimitLogic class and rename it to something Like
ConstAccelMovementLogic.
Pass it the things it needs in the constructor like Acceleration,
Destination and the Body it is to work on.
then in the RunLogic method calculate the vectors
need to calculate the normal and its tangent between the start and the
stop.
need to calculate the current linear velocity in the direction of the
destination.
Then get the force equivalent to the acceleration by force * mass
Then get the acceleration need to stop the body by (velocity^2)/
(2*distance) = acceleration
put an if statement in to to test if that accel is equal or greater
then Acceleration passed into the ctor
if its greater or equal you make the object decelerate otherwise
accelerate by passing the ApplyForce method a vector created from the
product of the diction normal and the force.

you will also need to calculate the force needed to neutralize any
tangential velocity and apply it.

I'm pretty sure this will work.

JonoPorter

unread,
Mar 26, 2010, 12:34:16 AM3/26/10
to Physics2D.Net
Ok Since its pretty complicated So I wrote it for you.

The logic class you want to use is MoveToPointLogic.

the code is in the svn.

AJ

unread,
Mar 28, 2010, 1:29:12 AM3/28/10
to Physics2D.Net
Jono,

Thanks so much for your help with this. There is little chance I
would've been able to put this together.
It seems to be working pretty well. The only issue I'm running into is
that the condition to stop it is never reached... any ideas? It
actually seems to overshoot the target, and then very slowly move
backwards.

if (distance < Math.Abs(step.Dt * (step.Dt *
maxAcceleration - Math.Abs(velocity))))
{
body.State.Velocity.Linear = Vector2D.Zero;
body.State.Position.Linear = destination;
this.Lifetime.IsExpired = true;

JonoPorter

unread,
Mar 28, 2010, 2:19:27 PM3/28/10
to Physics2D.Net
I'm unable to recreate the issue where does not reach the end
condition. What Values are you providing the logic?
Reply all
Reply to author
Forward
0 new messages