Pam
Pam

Reputation: 4200

Impulse applied to sprite at angle and speed

I'm using andengine and box2d to generate a game like paper toss. What I can't calculate is the correct vector for the first parameter in the following function to make the ball go up in the correct speed and height (the idea is that the ball height does not exceed the screen height):

pelota_body.applyLinearImpulse(new Vector2(pointX, impulse), pelota_body.getWorldCenter());

In pointX I use the value vectorXPoint-(CAMERA_WIDTH/2)and it goes in the direction of the vector drawn by the finger, but the impulse value I can't seem to get it right and whatever value I use (aprox 400) works different on every device.

Upvotes: 1

Views: 166

Answers (1)

Andrew
Andrew

Reputation: 24846

In order the body to travel height h before falling down you need it's initial vertical velocity:

v_y = sqrt(2*g*h), where g is the gravity acceleration.

You can calculate the impulse based on this v_y or just use SetLinearVelocity method.

EDIT

//box2d source
inline void b2Body::ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point)
{
    if (m_type != b2_dynamicBody)
    {
        return;
    }

    if (IsAwake() == false)
    {
        SetAwake(true);
    }
    m_linearVelocity += m_invMass * impulse;
    m_angularVelocity += m_invI * b2Cross(point - m_sweep.c, impulse);
}

So I would just set the velocity

Upvotes: 1

Related Questions