How do you increase the velocity of a vector

Topics: Developer Forum, User Forum
Sep 3, 2010 at 10:25 PM

Say I have a Vector2 for a Body. How can I over time continually (every loop) add to it's velocity?



Sep 4, 2010 at 2:33 PM

A vector is made up of 2 things: A length (magnitude) and a direction. If you multiply a scalar (single value; a number) with the vector, you will increase (if the scalar is >1) the magnitude of the vector.


Vector2 force = new Vector2(10,0); // 10 in force along the x-axis
force = force * 2; // 20 in force along the x-axis

To apply a constant force, you use the Body.ApplyForce() method. Give it the force vector as an argument.

Sep 5, 2010 at 8:31 PM

Yes, as genbox says. Apply a force every loop in the direction you want to go.

Sep 5, 2010 at 9:02 PM

I am missing something (as you can see, I am using Andy's helper)...

                foreach (PhysicsSprite missile in missiles.Values)
                    Vector2 vector2 = missile.BodyObject.Force;
                    vector2 = vector2 * 2;

However, it does not seem to 'accelerate'.

Sep 6, 2010 at 7:32 AM
Edited Sep 6, 2010 at 10:00 AM

If you want to duplicate the body's velocity, you should use impulse instead:


foreach (PhysicsSprite missile in missiles.Values)
Vector2 vector2 = missile.BodyObject.LinearVelocity;
vector2 = vector2 * 2;

Edit: Sorry, this should triplicate the velocity, for duplicating it, dont multiply "vector2" by 2.

Sep 6, 2010 at 5:50 PM
Edited Sep 6, 2010 at 5:50 PM

Now I'm in business:

                foreach (PhysicsSprite missile in missiles.Values)
                    Vector2 vector2 = missile.BodyObject.LinearVelocity;
                    if (vector2.Length() < 2000)
                        vector2 = vector2 * .02f;

Thanks all!