This project has moved. For the latest updates, please go here.

Broadphase collisions occuring during object creation time instead of next World.Step()

Topics: Developer Forum, User Forum
Apr 24, 2013 at 6:34 AM
Hola again,

As a farseer/box2d newb I'm still fighting with collision management, especially with broad phase check at the moment.

During debugging the operation where player shoots bullets I noticed that the collisions seem to occur during object creation time. Meaning that when I, as a player, press button to shoot a new bullet entity with physics body/fixture is created. And before the object has been fully created farseer reports a broadphase collision with the player ship and the bullet (both have bodies). Seems to be occuring just after the body has been added to the physics World...

Should this happen like this?

On my Bullet constructor I set the flags for ignoring collisions with certain types of objects, but Farseer reports a collision before the constructor has been able to call "IgnoreCollisionWith" etc.

Entity class has Shoot()-method:
                Bullet bullet = new Bullet(bulletTexture, Owner.LevelBounds, Owner.World, TTL, Owner);
                bullet.Body.Position = Owner.Body.Position;
                bullet.Body.Rotation = Owner.Body.Rotation;
                ... etc ...
                // We don't want this bullet to report collision with the source ship
How bullet constructor creates a body using BodyFactory.CreateCircle() and sets all other parameters like IsSensor, IsBullet etc.

Farseer reports broad phase collision with the bullet and originating ship when I set the position for the bullet's body, and before the Shoot() method has finished. Shoot is not called during World.Step(), it's called in the game Update()-loop when player has pressed a required button/key.

Oh, and moving the IgnoreCollisionWith()-calls before the line which sets bullets position does not help either...

Am I misunderstanding something?
Apr 24, 2013 at 6:47 AM
And to answer myself, looking at the farseer code for Body.Position and continuing to the called SetTransform():
        public void SetTransform(ref Vector2 position, float rotation)
            SetTransformIgnoreContacts(ref position, rotation);

I suppose I can use SetTransformIgnoreContacts(ref Vector2 position, float angle) to move the body without causing broadphase to shoot me in this situation.

Which I think is a correct behavior :). So forget my question. What I am still wondering is that why the IgnoreCollisionWith() methods do not work in this case..
Apr 24, 2013 at 6:56 AM
Also, if I use the Body.OnCollision delegate things work nicely in this situation. Even those IgnoreCollisionWith()-problems above disappear.

SO, to conclude myself, seems that this just was another farseer/box2d n00b issue :)