Collisions (ohysics) for Missiles, Bullets, Explosions etc


the magic in the magic show, is not in the hand or hat of the magician, its in his assistants underwear, its mostly slight of hand, doing the actual math is too slow, so we find ways to approximate it and get it done, getting shit done is the word of mouth way the game industry works, if you can get shit done, you get a job, and if you cant, you get the sack, its not a perfect world - but you can learn real magic from people around you, and apply it


:thinking: but the mass times speed wont equal force at all


assuming the bullet is moving in a perfect vacuum, the force will be speed times mass, a basic newtonian physics constant, which I think personally is bullshit, because nothing is perfect, and the universe (mostly) sucks

In a few weeks, I am going back to school, to get a teaching certificate

Newton was very bright, but he did not define our physical world, he merely attempted to define it using the state of the art math he had back then.


:rofl:i think i can trust you guys in this one…

now lets talk about explosions, can i do a great number of RaycastSingle()s from the explosion origin to all different directions with the length being its effective diameter, instead of doing a SphereCast()?
and i wonder if this can be possibly done in 1/60 second?

cuz if i do a SphereCast() to find out the targets being affected by the explosion, i cant think of a way to determine if those targets are behind covers or not :thinking:


Just, no. you can do a few ray casts, but a shitload of raycasts costs a shitload. Your app will have a heart attack and you’ll feel foolish for trying to do a death star explosion with a physics engine.
In physics you get what you paid for, which is why in games, we try to avoid paying full cost

By the way, the original death star explosion in star wars took months, using supercomputers of their era


:sob:then any suggestions for determining if a target is behind cover or not? if im to use SphereCast()?




Ah, but it is not mass times speed: It’s mass times acceleration; a change in speed… and let’s assume the bullet instantly comes to a full stop upon impact.
This also means that when no forces (like gravity, drag or collisions) are applied to an object it will continue at the same velocity (linear and angular), in the same direction. Like an asteroid in deep space.

You could use a spherecast to find the objects that you would then each check with one or several raycasts for possible escaping of the blast.


this raycasts here is the new question, how to do them? :thinking:
i mean how do i determine the parameters for the raycasts


Go over some of the examples that use raycasts.


Sample using Octree raycast:

Similarly you can use these PhysicsWorld functions:


update: about the “force” to apply on the hit target to simulate the reaction of the bullet impact, i accidently found this momentum and its conservation principle, so there is
(subscript b means bullet, t means target)
and so the change of target’s velocity SharedScreenshot4 SharedScreenshot2
and becuz the mass of both objects and the initial velocity of the bullet when impact are known, the change of bullet’s velocity are determined by game logics, SharedScreenshot4 is solvable.

now we can finally & simply do this:

Vector3 finalVelocity = target.GetLinearVelocity() + change_of_velocity;

im an idiot of logics and math, criticize me if i got anything wrong :bowing_man:


The main problem I see is the calling of SetLinearVelocity, this is something you want to avoid. Instead apply forces and impulses.

Also, you do not need math to just try out some numbers and (not) see their effect. :slight_smile:


:disappointed_relieved:i just want the codes to generate the impact automatically so i dont need to provide a random number every time


To simulate a bullet impact I use code like this:

hitRigidBody.ApplyImpulse(((StereoApplication)Application).LeftCamera.Node.Direction*10, result.Value.Position);

So 10 times the camera direction. It’s quite a powerful bullet. Spins a ragdoll right around and off it’s feet.

And another example:

hitRigidBody.ApplyTorqueImpulse(Vector3.Up * 4);


I would add some mention of Collision Filtering - ie, Collision Group and Collision Mask (the latter is called collision layers in Urho)


:thinking:the real question there is not if the hit targets are correctly collided or not,
but whether there are obstacles between these targets and the explosion center or not.


You want to determine which object was hit first - a ray cast starts at its origin, and in theory, moves forwards, and you can return the first object that was hit, and determine if it was a wall, or not. Filtering is useful to ignore collisions with things you don’t want included in the result, such as trigger (aka sensor) volumes


:rofl:im not talking about excluding walls or other non-player related game objects
im talking about how to check if any of the results are actually behind covers


Ray casts only return the first thing that the ray hit - in your case, you will either be told that the ray hit a wall, or it hit a game character, so we can deduce, hey, if my bullet hit the wall, then the character was not hit.