0
votes

I have two bodies in the world. The first is the player moving around. While the second, the enemy tries to hit the player. I want to implement a shield for the player so when the enemy touches the player it does not affect the players linear velocity, instead it keeps moving.

I tried adding a third body as kinematic binding it to the player (having it a little bigger than the player) but for some reason it was sticking to the wall when linear impulse are applied to the player.

So i tried to force a stop on player and apply the inverted linear velocity of the player to the enemy. That works fine when my player is steady but not moving.

How can i make it a kinematic like body with all features of a dynamic keep moving but NOT Being affected by the enemies hits.

This is the code i tried working fine when the player is steady since iam nulling the linear velocity after the hit, but this is not really a clean solution.

Can i actually have kinematic bodies and use applyLinearImpulse on them? The problem i see with kinematic bodies is, my player would simply ignore the walls, but it should still collide with walls and everthing else in the world EXCEPT enemies which should bounce of the player like they hit a wall.

func postSolve(contact: b2Contact, impulse: b2ContactImpulse) {
  ...
  if player.ShieldActive {
    let playervelocity = player.getLinearVelocity()
    player.forceStop // sets linearvelocity to zero including angularvelocity
    enemy.applyLinearImpulse( playervelocity.invert()*0.1) // apply the inverted velocity of the player with a scalar so the enemy moves aways like it hit a wall
  }
  ...
}
1

1 Answers

0
votes

Adding seperate body as kinematic in the size of my player and setting the proper category and maskbits made it possible. I was able to move freely and enemies could not harm my player anymore while the shield was active.