Tutorial 3 – Collisions

  1. In an empty scene, Create an GravityEngine (GameObject/3D Object/GravityEngine)
  2. Create 2 NBody objects by dragging the Prefab “Star” into the scene twice. Change the names to NBody1 and NBody2
    1. Move bodies to X=-10 and X=10 respectivly.
  3. 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
    1. Confirm a Sphere Collider is present (it’s part of the prefab)
    2. Ensure isTrigger is enabled for the collider
    3. Add a RigidBody (Component/Physics/RigidBody)
    4. 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.

  1. Add a NBodyCollision to the model attached to NBody2. This is the body that will be absorbed.
    1. It MUST be attached to the model, since that’s where the Rigidbody/Sphere Collider are attached!
  2. In the Inspector for NbodyCollision set the type to ABSORB_IMMEDIATE
  3. Press PLAY
    1. The smaller body will move towards the larger and at the moment of contact be inactivated.
    2. The momentum of the smaller body will be used to adjust the velocity of the larger body so that momentum is conserved.
    3. 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.

  1. Press PLAY to see this happen.
  2. 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.

  1. Drag the ExplosionSmall prefab onto the Inspector entry for the NBodyCollision Explosion Prefab field. (Small refers to the size of the particles)
  2. Select Customize Explosion.
  3. Adjust explosion parameters
    1. Cone angle is the ejection cone for the particles generated
    2. 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”
    3. Velocity adjust modifies the particle velocity calculated to reach the explode size of the body being impacted. Try 0.7
    4. 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%)
  4. Press PLAY and observe the explosion.