Detect high speed collision: shot collision, bullet collision

Using cannon-es: cannon-es

What do you do to detect if a high speed mesh hits another mesh?

I’m having a hard time trying to detect if a bullet (sphere mesh) hit the target (box mesh) at high speed (100). It’s easy to detect at low speed (20), but not at high speed.

I understand that, at the time of detection, the bullet may have already passed through the target, so I’m asking. HOW?

// cannon-es physics body
physicsBody.addEventListener("collide", (event) => {
	event.body
});

You either run your physics at a small enough substep that the bullet never moves more than half its width, OR you use “CCD” .. continuous collision detection in a physics library that supports it like ammo or physx… or you implement your bullets with short raycasts from last to current position.

4 Likes

This seems like a good alternative. Thanks!

1 Like

Yeah.. I usually go with the raycasting approach.
CCD works, but can be a bit fiddly to set up… but sometimes you really do need it, for instance in something like a pinball game.

1 Like

Thanks to your feedback, I was able to implement what I need.

  • the cannon body has body.PreviousPosition :smiley:

It works exactly as you said:

raycasts from last to current position

Now this detects collisions at speed: 1, 20, 100, 500!

way better than physicsBody.addEventListener("collide")

Awesome!

Thank you very much :handshake:

import {
	Ray,
	RaycastResult
} from "cannon-es";

export const checkCollision = (world, body) => {
	const ray = new Ray(body.previousPosition, body.position);
	const result = new RaycastResult();

	ray.intersectBodies(world.bodies, result);

	if (result.body) {
		console.log("collision: ", result.body)
	}
};
3 Likes