How to enable Collision Detection

The steps to enable collision-detection to act on a 3D character are summarized below:

  1. Create an instance of a Dynamic Action

  2. Set Coefficient of Restitution for character (optional)

  3. Set Mass of character (optional)

Step 1. Create a Dynamic Action

To enable Collision Detection on a game character, first create an instance of U4DEngine::U4DDynamicAction as shown on line 1. Then enable kinetic behavior on the dynamic action as shown on line 2 and enable collision detection as shown on line 3. At this moment, the Collision Detection System will be able to detect collision on the object.

//Create an instance of U4DModel type
myAstronaut=new U4DEngine::U4DModel();

//Load attribute (rendering information) into the game entity
if (myAstronaut->loadModel("astronaut")) {

    //Line 1. Create a Dynamic action
    kineticAction=new U4DDynamicAction(myAstronaut);
    
    //Line 2. Enable Kinetic Behavior
    kineticAction->enableKineticsBehavior();
    
    //Line 3. Enable Collision Behavior
    kineticAction->enableCollisionBehavior();
    
    //As a test, let's make the starting position of the model five units up
    myAstronaut->translateBy(0.0,5.0,0.0);
    
    //Line 4. Load rendering information into the GPU
    myAstronaut->loadRenderingInformation();

    //Line 5. Add astronaut to the scenegraph
    addChild(myAstronaut);

}

Step 3. Set the Coefficient of Restitution & Mass (Optional)

The Coefficient of Restitution determines the bounciness of the character. The value ranges from 0.0 to 1.0. A value of 1.0 represents a lot of bounciness and is set using initCoefficientOfRestitution().

Let's set the Coefficient of Restitution to a medium level 0.5. And set the mass of the astronaut to 1.0.

//Line 8. Set Coefficient of Restitution
kineticAction->initCoefficientOfRestitution(0.5);

//Line 9. Set Mass
kineticAction->initMass(1.0);

Result

Last updated