Collisions (ohysics) for Missiles, Bullets, Explosions etc

I struggle how to create collisions for bullets, missiles and all moving objects from shooting weapons.

I take into consideration Bullet (engine) for that, but I am not sure if isn’t take too much performance.
I thinking about, do it with raycast, but I don’t know how to raycast for objects like spheres or objects with custom shapes, because raycast is only 1dimensional line (ray from start point in to end point) and I need something like area damage for explosions. Imagine, when grenade explode near 4enemies, it affect all 4 enemies within explosion range.

What is best solution for that?


You may also look into PhysicsWorld::SphereCast(result, ray, radius, maxDistance), PhysicsWorld::GetRigidBodies(result, sphere) or similar functions.
In some cases you may want to use triggers instead. These rigid bodies - that had SetTrigger(true) called - will signal physics events without any forces being applied as a result.


One thing, what if bullet is going too fast? I turn ContinousPhysic Detection (I can’t remember the right name) and fast moving camera isn’t going throught objects anymore.
But from documentation, is mentioned it can slow down performance. Imagine I want have machine gun which shoot lot of bullets in high speed. Is it ok to make bullets with Bullets physic engine? Or raycast is better solution?
Maybe for player character it is not cost performance, but imagine if they will be enemies with machine guns too.

Ironically you’re probably better off by indeed casting a ray (or several consecutive ones).

My idea is create object (simple bullet maybe billboard with bullet texture) and it will be casting ray for object detection as he travel in scene. It is good idea? Only problem is, if he will moving fast, but I have some ideas how to make it correct. Or it is better only cast ray and make damge for first ray hit? But this is unrealistic when objects will be in different distance.

You can use raycast with a bullet velocity as the ray length. Perform raycast between bulletPos and bulletPos + bulletVelocity if no hits were registered you’re safe to move your bulletPos to bulletPos + velocity.
keep repeating this process until you hit something or your bullet reach max distance (goes out of level bounds).

1 Like

You’d actually want to ray cast with bullet velocity * time since last frame or it will overshoot. If they’re slow bullets you could also segment it to trace an approximately parabolic arc from gravity (but of course that would be somewhat more expensive).

1 Like

Just so you know: In my game, bullets are affected by gravity and there’s penetration logic. Instead of using a rigid body, I just cast rays all the time. Much less buggy.

1 Like

Thanks all for posts. Solution like use bullet velocity and time for the ray distance was what come to my mind when I started thinking about it. But thanks to confirm the idea.

I did bullets with raycast system, which do damage to object. I am going try something with area damage.

smellymumbler thanks for share experience.

:laughing:how did you calculate the impact of the bullets
if we used raycast for bullets, how do we calculate the impact and apply the force on the hit object?

Well, that depends on your game. In my game, impact and force and directly dependent on the character’s attributes + dice roll. It’s not meant to be realistic. So, the distance traveled by the bullet is the only thing that matters to me, since I used it to roll some dices versus the characters luck, perception, etc. I could add other interesting variables, like wind and so, but again, not trying to be realistic.

Some research material:

:joy:i didnt go to school, so i have not much knowledge in math or physics.

the only thing im thinking about (now) is how to calculate the impact of hits.
e.g. when shooting a small independent rock on the ground, how should it react to the hit

my current idea is to make every bullet a physical object, but with different collision mask from game world, make them not to collide with game objects. then subscribe to E_PHYSICSPOSTSTEP, raycast from last frame’s position to current position to determine hit targets. but i stuck right here: i totally have no idea how to implement the logics here :slight_smile:

My character class is derived from LogicComponent.
This gives me FixedUpdate method which I can override, it is called after the physics update.
I would recommend using raycasts to simulate bullets, don’t try to make 3D physics bullet objects which are very small, and send them at very high velocities, it will lead to trouble.

Bullet Physics works best with objects no smaller than a tennis ball, and no larger than a bus.
We can “fake” a physics bullet in a single frame, using a raycast.
We don’t need to draw that bullet, though we may wish to draw the damage it did when it hits something. Usually, we don’t try to draw bullets, because they are assumed to be moving so fast that we can’t really see them. We can use things like trail effects to draw them if we really feel like it.

[EDIT] You are correct in thinking that CCD will help with the case of fast-moving objects, but it’s not cheap, and there are still going to be numerical issues with using very small objects.
I needed to use CCD to prevent tunneling in a watermelon racing game, and that melon was 1x1.6 METERS in size, and moving at around 120KPH

the reason i make bullets real physics objects is the Bullet physics engine will do the gravity effect, and also make it simpler to do wind effect (just some Body::ApplyForce())
and the bullet can still be seen within the game world, tho its quite hard :v

The longer you spend making games, the more limitations you will encounter. The real magic is not to try to solve every problem, but to find an acceptable workaround, which does what you want, and then find a way to make it look pretty. First make it work, then worry about what it looks like. In your case, my suggestion to use raycasts will let you get the code working quickly, even though you can’t see any damage being done, you will be able to detect the hits and deal the damage and make it work. Once it works, you can make decisions about how that should look, and run into the next bottleneck / limitation.

As for learning math, man I have a degree and I still can’t read greek notation - I’d love to teach you what I can, as you find you need it, so don’t hesitate to ask - most game math boils down to dot and cross products, interpolation, and a bit of trigonometry - it can be learned on demand, as you find you need it

:joy:i mean bullets are made physical objects just to simulate their movement, and use raycast to detect hit targets.
and the problem here is i dont know how to apply “reaction” to the hit targets

Well I did suggest that how things look should come last, but you could for example, just change the material colour of a dead guy, just to display that something got hit, and i would also look at the Decal sample, which lets you shoot splats on the world
Bullet won’t do ALL the math for you, at some point you’re gonna need to get to grips with some math, I know it sucks, but if you can use it for something cool, it sucks less.

When I am writing something new, I often just spew some text to the console output, like “oh I got hit”
Debug Spew is underrated.

oops, im talking about the reaction of the impact
like shooting a small rock, the rock will “be pushed away”

im thinking of using RigidBody::ApplyForce() to make the reaction, just have no idea how to calculate the force for it

sry if my expression makes you confused, im not good at language either