Game Development Reference
In-Depth Information
Handling projectile collision impacts
Bullets aren't very interesting unless we show where they impacted the environment. First,
let's take a look at the collision object that's passed into a collision callback. The collision
object has the two sandbox objects that collided and will always set the object that re-
gistered with the callback as
objectA
and the other object as
objectB
.
PointA
and
PointB
are the positions on each object that the collision took place on. The
normalOnB
value represents the normal vector to the collision point relative to
objectB
:
Collision Object
collision.objectA
collision.objectB
collision.pointA
collision.pointB
collision.normalOnB
Creating a particle impact for a bullet creates a new particle system, which is
BulletIm-
pact
, and then sets the particle's direction based on the normal vector of the collision. The
callback also removes the bullet from the sandbox:
SoldierAgent.lua
:
local function _ParticleImpact(sandbox, collision)
-- Remove the bullet particle.
Sandbox.RemoveObject(sandbox, collision.objectA);
-- Create an impact particle where the bullet collided
with
-- another object.
local particleImpact = Core.CreateParticle(
sandbox, "BulletImpact");
Core.SetPosition(particleImpact, collision.pointA);
Core.SetParticleDirection(
particleImpact, collision.normalOnB);
end
local function _ShootBullet(sandbox, position, rotation)