Page 1 of 1

Physical contacts and impulses

Posted: Fri Sep 12, 2014 9:48 am
by nikolaiko
Hi, I continuing my work on my game using this engine and I have few questions about contacts detecting and impulses.

In my game players fight with each other. So, one of the ways to destroy opponent - shot a rocket and hit enemy. I have implemented this functionality using ContactCreated event. Then player launch rocket I just add event listener to this rocket

Code: Select all

CharacterController.Body.CollisionInformation.Events.ContactCreated += ProjectileOnContactCreated
and then in this function resolving contact. I working with some logic stuff there (checking health, if player dead and some other things), but anyway, at the end I delete rocket from space and remove player - if it dies :

Code: Select all

var CharacterController = ((BasePhysics) Player.Physics).GetCharacterController();                   
Space.Remove(CharacterController.Body);
And then user click button to respawn again, I respawn him :

Code: Select all

var CharacterController = ((BasePhysics)Player.Physics).GetCharacterController();
CharacterController.Body.Position = GetRandomRespawn();            

Space.Add(CharacterController.Body);
The problem is - then player respawned - he is pushed back, right after appearing. I assume that is is some kind of impulse or something else, after collision with rocket. Because after death I remove player from space - it removes him from simulation loop. But then I add him back - he is active again and system immideatly start to process impulse data from prev collision. Is there any way to fully reset user impulses, velocity's and other stats to make him stand still after respawn? I think that problem is in physical effects, because then I kill player with laser (wich doesn't push player back then hit), killed player respawns well.

Thanks.

Re: Physical contacts and impulses

Posted: Fri Sep 12, 2014 6:13 pm
by Norbo
Setting CharacterController.Body.LinearVelocity to zero should do the trick, assuming there aren't any external things continuing to apply force.

For an object which can rotate (character controller bodies can't), setting the AngularVelocity to zero would be needed as well.

By the way, I recommend against adding/removing the CharacterController.Body directly to/from the space. This can leave the character in an invalid state. Instead, add/remove the CharacterController object itself. The Body will be added and removed for you.

Re: Physical contacts and impulses

Posted: Tue Sep 16, 2014 9:33 am
by nikolaiko
Sorry, for long silence, worked with level design, and didn't touch player. Thanks for answer, will try yor advices and tell about results.

Re: Physical contacts and impulses

Posted: Fri Sep 26, 2014 7:49 am
by nikolaiko
Yep, setting LinearVelocity to zero solved problem.

Thanks.