Physical contacts and impulses

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
nikolaiko
Posts: 43
Joined: Mon Aug 04, 2014 8:26 am

Physical contacts and impulses

Post 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.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Physical contacts and impulses

Post 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.
nikolaiko
Posts: 43
Joined: Mon Aug 04, 2014 8:26 am

Re: Physical contacts and impulses

Post 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.
nikolaiko
Posts: 43
Joined: Mon Aug 04, 2014 8:26 am

Re: Physical contacts and impulses

Post by nikolaiko »

Yep, setting LinearVelocity to zero solved problem.

Thanks.
Post Reply