Chipmunk2D Unity Documentation | Main Page | Forum

Manual

Tutorials

API

Classes

Structs

Enumerations

ChipmunkCollisionManager

Class : Inherits from MonoBehaviour

This class is used as a delegate to receive collision events.

Chipmunk works by having a single collision callback for a pair of colliding shapes. You can have multiple ChipmunkCollisionManager objects within a scene. There are four events you can catch that are described below. As an example, when you want to find out when a shape with a ChipmunkShape.collisionType of "player" begins colliding with one of ChipmunkShape.collisionType "monster", you would need to make a ChipmunkBegin_player_monster() method on a manager in the scene.
This doesn't work quite like the way you are used to in Unity, where it sends messages like OnCollisionEnter() to all MonoBehaviours attached to the collider. Unity uses private APIs to send those events efficiently.

// Make your class extend ChipmunkCollisionManager
class MyCollisionMananger extends ChipmunkCollisionManager {
  
  // This function will be called each time a shape marked
  // with "player" first touches a shape marked with "bullet".
  function ChipmunkBegin_player_bullet(arbiter : ChipmunkArbiter){
    var player : ChipmunkShape;
    var bullet : ChipmunkShape;
    
    // Because of the name of this function, you will always find that:
    // player.collisionType == "player"
    // bullet.collisionType == "bullet"
    
    // The order of the arguments matches the order in the function name.
    arbiter.GetShapes(player, bullet);
    
    player.GetComponent(Player).HitByBullet(bullet);
    
    // Returning false from a begin callback means to ignore the collision
    // response for these two colliding shapes until they separate.
    return true;
  }
  
  // Pre-solve functions run every fixed time step. They give you the opportunity
  // to set ChipmunkArbiter values or do per-frame collision rejection.
  function ChipmunkPreSolve_player_oneway(arbiter : ChipmunkArbiter){
    // You can override friction, elasticity, or
    // surface velocity calculations in a pre-solve function.
    arbiter.friction = 0.3;
    
    // You can also conditionally reject a collision.
    // This is how you could implement one-way platforms.
    if(arbiter.GetNormal(0).y > -0.7f){
      // Ignore the collision of the two shapes until they separate (starting next frame).
      arbiter.Ignore();
      
      // Also ignore their collision for the current frame.
      return false;
    }
    
    // Normally you want the player to collide with (and stand on) a platform.
    return true;
  }
  
  // Post-solve functions run every fixed time step. They give you the opportunity
  // to get ChipmunkArbiter values that were just calculated.
  function ChipmunkPostSolve_crate_rock(arbiter : ChipmunkArbiter){
    if(arbiter.kineticEnergy > 1000f){
      // The crate was smashed!
    }
  }
  
  // Separate functions are called on the first fixed time step after two shapes
  // stop colliding or when one of them is destroyed.
  function ChipmunkPostSolve_player_pressureSwitch(arbiter : ChipmunkArbiter){
    var player : ChipmunkShape;
    var pressureSwitch : ChipmunkShape;
    arbiter.GetShapes(player, pressureSwitch);
    
    pressureSwitch.GetComponent(PressureSwitch).ToggleOff();
  }
}


Overridable Functions


ChipmunkBegin_type1_type2Called on the first fixed time step that two shapes with the given ChipmunkShape.collisionTypes begin colliding.
ChipmunkPreSolve_type1_type2Called each fixed time step for two shapes with the given ChipmunkShape.collisionTypes before the solver runs.
ChipmunkPostSolve_type1_type2Called each fixed time step for two shapes with the given ChipmunkShape.collisionTypes after the solver runs.
ChipmunkSeparate_type1_type2Called on the first fixed time step after two shapes with the given ChipmunkShape.collisionTypes have stopped colliding.