Weird behavior of collisions in combination with Body.SetTransform

Topics: Developer Forum, User Forum
Nov 18, 2014 at 1:05 AM

My problem is quite complicated. If someone will respond / tries to help I will build an code example to show my problem in detail. I am note sure if anyone is reading this?!?

I switched a few days ago from Box2D to Farseer but noticed that the collision handling seem to differ. My game uses client side prediction to allow my physics simulation to run in sync on multiple computers at the same time. Because of numerical errors or network problems it can happen that the simulation of the server and the clients result in slightly different outcomes of the simulation. To make sure that they don't run out of sync the server sends the position it calculates to the client where I use SetTransform to correct the position. This works with Box2D without problems.

the problem:
If for example my body collides with a wall and I reset the position and velocity directly after the collision to 0 the body suddenly starts to move again in the next frame as it would still bounce of the wall it collided with a frame ago. Is there any major difference how farseer treats collisions in comparison to Box2D? I noticed there are two methods to change the transformation: SetTransform and SetTransformIgnoreContacts sadly none of the two allows me to reset my body and to forget about collisions that happend in the past.

Currently, there seem to be no way to implement network games with client side prediction using Farseer because of this problem.

Here is a simple example:
Imagine a sphere with radius 1m moves with 1 m per time step to the right and bounces off a wall at x=3.5

The positions will be:
Step 1: x = 0
Step 2: x = 1
Step 3 x = 2
Step 4 -> Collision at 2.5 -> object bounces of the wall and moves back to x = 2
Step 5: x = 1

If I now change the position to x= 0 and velocity to 0 after Step 4 you would expect nothing happens in Step 5 and it still stands on x =0 but the outcome will be x = -1. and the velocity is suddenly -1 as it still bounces off the wall even though it is far away from the wall and isn't suppose to move anymore.
Nov 18, 2014 at 3:15 AM
I spent a bit more time debugging using the BeforeCollision and AfterCollision events. If I change the object data using SetTransform + setting the linear velocity after a simulation step in which a collision happend and AfterCollision was called then AfterCollision will be called AGAIN in the following time step and the impulse from the collision will be applied twice.

So my example should maybe look like this:

Step 1: x = 0 v = 1
Step 2: x = 1 v = 1
Step 3: x = 2 v = 1
Step 4: x = 2 v = -1 (AfterCollision is called)
-> I set position to 0 and velocity to 0
Step 5 -> during step 5 it calls AfterCollision again and changes the velocity even so BeginCollision isn't called and the object itself is far away from anything it could collide with.

It starts to look like a bug to me!?