Impulse when shooting

Hey there :slight_smile:

I’m working on making a gun shoot and apply an impulse if it hits a Rigidbody. I found this thread with a solution from @will

https://forum.playcanvas.com/t/using-impulses-to-shoot-boxes/2038

It works quite well!.. That is until my Rigidbodies (boxes) have been turned 180 degrees by shooting at the corners. Then it’s as if they try to rotate in the opposite direction - against the bullet! :thinking:

Any idea why that is?

Here’s my own code:

const impulse = new pc.Vec3().copy(this.outTo).sub(this.player.view.head.getPosition()).normalize().mulScalar(10);

const localOffset = new pc.Vec3();
const mat = rb.entity.getWorldTransform().clone();
mat.invert();
mat.transformPoint(this.outHit.point, localOffset);

rb.applyImpulse(impulse, localOffset);

Ah! I found this one:

https://pybullet.org/Bullet/phpBB3/viewtopic.php?t=8219

Apparently, you do it the somewhat simple way, not taking rotation into account. This seems to work.

const impulse = new pc.Vec3().copy(this.outTo).sub(this.player.view.head.getPosition()).normalize().mulScalar(10);
const localOffset = this.outHit.point.clone().sub(rb.entity.getPosition());
rb.applyImpulse(impulse, localOffset);
1 Like