tips4design Posted February 9, 2015 Share Posted February 9, 2015 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 More sharing options...
ForgeableSum Posted February 9, 2015 Share Posted February 9, 2015 Have you tried setting the velocity to 0 on the function called on collision? npc.body.velocity.x = 0; npc.body.velocity.y = 0; Link to comment Share on other sites More sharing options...
tips4design Posted February 9, 2015 Author Share Posted February 9, 2015 The problem is that the NPC is not still, so if I hit him with a bullet while moving he will stop instead of continuing his movement. I tried adding more friction using damping, but it's still not enough to keep the npc still after getting hit (or having his movement being affected less). Link to comment Share on other sites More sharing options...
ForgeableSum Posted February 9, 2015 Share Posted February 9, 2015 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 More sharing options...
tips4design Posted February 9, 2015 Author Share Posted February 9, 2015 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 More sharing options...
ForgeableSum Posted February 9, 2015 Share Posted February 9, 2015 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. tips4design 1 Link to comment Share on other sites More sharing options...
tips4design Posted February 9, 2015 Author Share Posted February 9, 2015 Well, this will work, but will drop performance kinda hard, as every NPC will have 2 bodies instead of one. Maybe there is a less hacky solution. Link to comment Share on other sites More sharing options...
valueerror Posted February 10, 2015 Share Posted February 10, 2015 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 tips4design 1 Link to comment Share on other sites More sharing options...
tips4design Posted February 11, 2015 Author Share Posted February 11, 2015 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 More sharing options...
Recommended Posts