[Solved] Urgent question about constant speed and gravity

Topics: Developer Forum, User Forum
Jul 29, 2012 at 9:21 AM

Hi all,

I need to throw an object touching a point on screen to bounce or collide with others.

The issue is my object must have a constant speed and no gravity while other objects have gravity. It's possible to realize this?

I tried to build my object with no Farseer and other objects with it but I have more issues to manage the collisions, while I tried to build my object with Farseer but I don't know how set a constant speed and no gravity for only one object.

I have a bit confused...

Please, help me.




Jul 29, 2012 at 12:11 PM

Hi, try setting body.IgnoreGravity = true on the object you want to ignore the gravity, and constant speed can be achieved by setting the linear velocity on each update cycle.

Jul 30, 2012 at 8:18 AM

Hi Morbidi,

I tried to set body.IgnoreGravity = true and it's run correctly!

I have set the linear velocity to achieve the constant speed and it's ok but now I have a bit issue to move my object correctly to point destination, it's move near (with a random delta) to point. Why?

Thank you very much for your help.



Jul 30, 2012 at 12:06 PM

Hmm, hard to say without knowing the actual scenario. It may be a problem with the floating point inaccuracy.

Another way to do it is to set the linear damping to zero and apply a linear impulse while limiting the velocity to something suitable.

Jul 30, 2012 at 12:43 PM

And how to correct the floating point inaccuracy?

It may be that problem or maybe my not correct use of ConvertUnits.ToSim() and ConvertUnits.ToDisplay()... I not clear in mind when use it.

Jul 30, 2012 at 4:34 PM
Edited Jul 30, 2012 at 4:35 PM

The floating point inaccuracy is caused by the fact that processors cannot handle floating point values 100% accurately - approximations are used. This can be seen quite easily if you create a simple program like this:


float val = 0f;
for(int i = 0; i < 100; ++i)
    Console.WriteLine("Val is " + val);
    val = val + 0.1f;


After a few lines, it will give you incorrect values. There's nothing that can fix this, we just have to live with it and try to go around it. I don't think it's a problem in your unit conversions, since they just map a value from the physics world to your rendering coordinate system.
I suggest that you keep updating the direction of your object every frame to ensure the correct behaviour.

Jul 30, 2012 at 7:55 PM

Look this code:

public void Move(float velocity, Vector2 initialPosition, Vector2 destination, GameTime gameTime)
   if (velocity <= 0)
                throw new ArgumentOutOfRangeException("velocity", "Argument must be greater than 0.");

            Vector2 direction = (ConvertUnits.ToSimUnits(destination) - ConvertUnits.ToSimUnits(initialPosition));

            if (direction.Length() <= 1)

            dishTextureBody.Body.IgnoreGravity = true;           
            dishTextureBody.Body.FixedRotation = false;           
            dishTextureBody.Body.LinearVelocity = velocity * Vector2.Normalize(direction);

in Update() method how to keep update the direction set in that code?

Thank you very much for help me!


Jul 31, 2012 at 6:31 AM

You have to update your initialPosition each frame so it's always calculating the direction from the current position, instead of the original position. This way the position can be adjusted each frame, although the adjustments are so small you can't see it on frame-by-frame basis, you can still see the effect on the end result. :)

By the way, on a different subject, you should always use LengthSquared() instead of Length() whenever possible. Calculating the length of a vector is more costly than calculating the square of the length, because of the square root calculation which is really costly. In your case, you can just switch the Length() to LengthSquared() without any other changes, since the square of 1 is 1. Small thing, but may be a huge boost in performance if done many times per frame. :)

Jul 31, 2012 at 11:39 AM

I solved my issue! The problem was my not correct use of ConvertUnits.ToSim() and .ToDisplay() in different places of my code so the gesture.position result was not correct.

Morbidi thank you very much for your help! This discussion was much important for me and I hope to find you in case of other needs! :-)

Thank you again.