Can not keep forces when changing position of body.

Topics: Developer Forum, User Forum
Mar 28, 2013 at 6:40 PM
Hey everyone,

This is what i wanna do:

When object touches wall I want to change its position to center of the screen but I want also to keep all forces.

This is how I'am trying to do it:
void ball_Collision(Spritehand.FarseerHelper.PhysicsSprite source, string collidedWith, FarseerPhysics.Dynamics.Contacts.Contact contact)
        {
        var linearVelocity = source.BodyObject.LinearVelocity;
        source.Position = new Vector2(1366/2,768/2);
        source.BodyObject.LinearVelocity= linearVelocity;
        }
But it works only when i multiply it by -1.
source.BodyObject.LinearVelocity= -1*linearVelocity;
in normal way it do not.

Any advices why ?

Thanks in advance.
Mar 28, 2013 at 9:00 PM
Sounds like you're trying to emulate a "portal" type behavior, correct?

My guess is that by the time the collision happens (and the event handler has been called), the collisions forces have already been applied to the body (again, this is just the case). So the velocity you're copying is the velocity after the collision (i.e. the ball is bouncing away from the wall). By negating it you are reversing it back towards the wall again.

Assuming my guess is correct, try using the pre-collision handler instead.

Hope that helps,
Mike.
Mar 28, 2013 at 9:43 PM
Edited Mar 28, 2013 at 10:23 PM
Thanks for reply.

Ok, I checked your advice and make up a simple test.
In PhysicsSprite object is only one event which have something in common with collisions.
It's called "Collision". This is where I fire up my pice of code.

To test it I set up a RestitutionCoefficient (bouncing) to 0.0 and do:
source.BodyObject.LinearVelocity= -1*source.BodyObject.LinearVelocity;
In your thesis this will be 0 = -1 * 0 because event is fired up after bounce and bounce makes all forces to 0. (when we're sending object only on Y axis)

But this makes object to bounce from wall with 100% strength.

Setting RestitutionCoefficient to 1.0 give the same.

I really appreciating your help but this one won't work or I am doing something wrong.

It still bugs me why after changing positions, changing LinearVelocity to old value with * -1 works and without not.

EDIT:

I've tried out:
var vector = new Vector2(0,0); //Takes object to center of screen.
source.BodyObject.SetTransform(vector,source.BodyObject.Rotation);
But it do not work either.
It still sends object after teleportation in wrong direction. (no matter if i multiply LinearVelocity by -1 or not)