[SOLVED] move Body to specific Point with constant Velocity

Hello 🙂
I have the following code, to "shoot" a body to a specific point (in my case the mouse-cursor):

Code: Select all

   
function shootTouch(mousePosX, mousePosY)
{
        var impulse = Qt.point((mousePosX - image.pos.x) * 3,(mousePosY - image.pos.y) * 3);
        collider.body.applyForce(impulse, 0);

}


My problem is, that the body is moving with different velocities, depending on the position of the mouse-cursor. How can I handle a movement with constant speed?
I already tried setLinearVelocity() and applyForce(), but I couldn't figure out, how to move it to a specific point!

Thank you,
nor0x
You could limit the size of that impulse to some maximum value I guess.

(C++)

Code: Select all

float impulseLength = impulse.Length();
if ( impulseLength > maxImpulse )
    impulse *= (maxImpulse / impulseLength);
Thank you!

But a limit doesn't work in my case, because I don't want to limit the maximum velocity.
There should be one velocity-value and the body should move based on this value.
I see. But what was the problem with SetLinearVelocity then..?
But what was the problem with SetLinearVelocity then..?


I have a code like the following:

Code: Select all

collider.body.linearVelocity = Qt.point(mousePosX, mousePosY); //Qt.point is like Vector2


The problem is that if the mouse-click happend in an area near the top of the screen, the ball is moving fast. If the mouse-click was in an area near the bottom of the screen, the ball is moving slow.

thx
Velocity is a direction, not a point in the world. You need to use the difference between where the thing is now, and where you want it to go.
eg.

Code: Select all

b2Vec2 vel = targetPosition - currentPosition;
body->SetLinearVelocity( vel );

This will still give you the problem that it moves fast when further away, so you could limit the size of the velocity to a maximum value as I showed above (just replace impulse with velocity).
You could also explicitly set the velocity size like this:

Code: Select all

vel.Normalize();
vel *= desiredSpeed;
Thank you very much

- directions are working fine, but the velocity...


do you know what

Code: Select all

vel.Normalize()
exactly does, because i don't have such an Method. (not included in QT-Framework Box2D)
Or do you know what

Code: Select all

vel.Length()
exactly returns, I don't have it neither!

Thanks
Sure, you can check the exact details in the Box2D source 🙂
http://code.google.com/p/box2d/source/b ... Math.h#124
Thank you very very much!

it's working 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=9055