Tutorial 3 – Collisions

In an empty scene, Create an GravityEngine (GameObject/3D Object/GravityEngine)

Create 2 NBody objects by dragging the Prefab “Star” into the scene twice. Change the names to NBody1 and NBody2

Move bodies to X=-10 and X=10 respectivly.

To setup bodies for collisions we need to attach some components to the models of each NBody. i.e the child StarModel that has the MeshRenderer

Confirm a Sphere Collider is present (it’s part of the prefab)

Ensure isTrigger is enabled for the collider

Add a RigidBody (Component/Physics/RigidBody)

Set isKinematic enabled, rigidbody mass to zero, Use gravity disabled.

The models attached to the NBodies will now report trigger events when they touch each other. The action that occurs is under script control via the usual collision handling routine OnTriggerEnter(), OnTriggerStay() and OnTriggerExit().

Space Physics provides a NBodyCollision script that provides several options for a collision:

Bounce

Explode: Remove the body and start a particle explosion (e.g. small body hits a larger one and is destroyed)

Absorb: remove the body from physics evolution

Looking at these each in turn:

Try BOUNCE. When PLAY is pressed the objects move together and then bounce away from each other.

Absorb Immediate is a good choice when two small bodies collide and one is absorbed at the moment of contact.

Add a NBodyCollision to the model attached to NBody2. This is the body that will be absorbed.

It MUST be attached to the model, since that’s where the Rigidbody/Sphere Collider are attached!

In the Inspector for NbodyCollision set the type to ABSORB_IMMEDIATE

Press PLAY

The smaller body will move towards the larger and at the moment of contact be inactivated.

The momentum of the smaller body will be used to adjust the velocity of the larger body so that momentum is conserved.

The mass of the remaining body will be increased by the mass of the colliding body

Now try ABSORB_ENVELOP.

The body being absorbed must first enter completely inside the other body before being. With bodies the same size they often fly apart before the envelop can be recognized.

Press PLAY to see this happen.

Change the size of the StarModel being absorbed to be 2 and PLAY again. Now the body is absorbed.

To absorb the body and initiate an explosion of debris, select EXPLODE.

Drag the ExplosionSmall prefab onto the Inspector entry for the NBodyCollision Explosion Prefab field. (Small refers to the size of the particles)

Select Customize Explosion.

Adjust explosion parameters

Cone angle is the ejection cone for the particles generated

Explosion size is the target for the explosion size. However if the body is heavy and/or has a small radius the initial velocity may need to be adjusted down using “Velocity Adjust”

Velocity adjust modifies the particle velocity calculated to reach the explode size of the body being impacted. Try 0.7

Velocity spread defines the variation in the velocity. The value provided is the standard deviation. (A value of 1 will give a velocity spread of about +/- 30%)