Epi
Epi

Reputation: 722

Knockback effect with top down graphics on box2d

I'm currently making a top down gfx actiong RPG on java, using jbox2d. Basically I want the enemies to recoil back after they get hit by the player (with top down graphics I don't have friction or gravity). I tried with restitution, applying impulse and setting linear velocity but I didn't get what I expected: the enemies teleport to the destination istantly and if they are near the wall they get ported out of the map. How can I fix this and what is the best to do it in your opinion? thanks a lot

Upvotes: 0

Views: 601

Answers (1)

NauticalMile
NauticalMile

Reputation: 1795

I don't know how you are using jbox2d and c++ at the same time...

Regardless, if your enemies are represented physically by b2_dynamicBodys then you probably want to apply a linear impulse and set the linear damping of the enemy body to a value greater than zero. Linear damping works like air drag - The faster the object is moving, the larger the force applied in the opposite direction. Applying a large linear impulse and setting a high linear damping will cause your enemies to fly away from your hero with a very high initial velocity, but they will come to rest very quickly.

I hope this helps!

Upvotes: 1

Related Questions