Jump to content

P2JS Delete body on collision


tips4design
 Share

Recommended Posts

I am using Phaser P2JS physiscs.

 

I have a game where the player shoots bullets in some NPCs. The problem is that the bullet, when it hits the NPC, it pushes the NPC very far (because the bullet has a high velocity). I want it to be destroyed on collision and the NPC to keeep still, as nothing happened.

npc.body.onBeginContact.add(npc.contact, npc);

How can I stop the contact processing, in player.contact function? Even if I set the bullet.body.static = true in the contact function, and kill the sprite it still pushes the NPC back.

 

If I set the bullet mass to 0, everything goes crazy. If I use kinematic, it pushes the objects much further away. If I use static, they don't move. I just want to make the bullets to do nothing on collisions, except trigger them.

 

So, how can I disable collisions with specific objects? (but still be able to trigger the contact handler when the bullet hits the NPC).

Or, how can I skip the current collision step for an object? (so it does not affect other objects)

Thanks!

Link to comment
Share on other sites

Let me see if I understand this correctly. 

 

The player shoots the NPC with a bullet. The NPC is already moving while this happens. When the bullet hits the NPC, you want it to disappear. You can't stop velocity because the NPC appears to stop just before disappearing? 

 

Have you tried stopping velocity on kill instead of on collision? 

Link to comment
Share on other sites

When the bullet hits the NPC I want the bullet to disappear and the NPC to keep doing its thing.

 

No, I tried stopping the velocity (actually resetting the whole bullet) before killing it, but the collision still happened. It seems like once it reaches onBeginContact you can not stop the contact anymore, or I am doing something wrong.

 

I will try to create a fiddle.

Link to comment
Share on other sites

My bad. I misread your original post. 

 

Here's something you could try. It's a bit messy, but it's the only solution I can think of:

* create a group of invisible physics-enabled sprites paired with the npcs (paired with something like a property name-value)

* set the invisible sprite to have the same x/y as its corresponding sprite in the update function. 

 

invisibleSprites.forEach(function(sprite) {

var correspondingSprite = npcGroup.iterate('name',sprite.name,2);

 

sprite.x = correspondingSprite.x;

sprite.y = correspondingSprite.y;

 

 

}); 

 

 

* then, instead of setting collision detection with the bullet and npc, do it with the bullet and invisible sprite. the invisible sprite body width and height will be larger than the npc body, so collision will happen there first

* On collision, destroy the bullet. 

 

It's messy but, it should work.

Link to comment
Share on other sites

setting the mass to 0 is the same as setting the body to static..   to minimize impact lowering the mass is a good approach..  set it to 0.000001  and it will not affect the npc very much..  

 

but why not go a step further..  you could set the bullet.body.data.shapes[0].sensor = true;    it will still "report" the collision but no actual collision will happen

Link to comment
Share on other sites

setting the mass to 0 is the same as setting the body to static..   to minimize impact lowering the mass is a good approach..  set it to 0.000001  and it will not affect the npc very much..  

 

but why not go a step further..  you could set the bullet.body.data.shapes[0].sensor = true;    it will still "report" the collision but no actual collision will happen

 

This sounds like what I was looking for, will try that, thanks!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...