Lab 6: Enable collision Detection on a 3D Game Object

Our goal is to enable Collision Detection between the astronaut and the island using the steps mentioned in this article:

image

For the sake of simplicity, the island already has Collision-Detection enabled. You are going to enable collision-detection to act on the astronaut.

Step 1. Enable Kinetic Behavior

In the engine, open the file Earth.mm.

Before we can collision-detection to an object, we need to create it. Therefore, create a character as specified in Rendering Lab 1

Once you have created the character, enable the character's kinetic behavior as shown in the snippet below (line 6).

//Line 5. Make sure the character is not null
if (myAstronaut!=nullptr) {
//Line 6. Enable kinetics
myAstronaut->enableKineticsBehavior();
}

Step 2. Enable Collision Behavior

Next, enable the character's collision behavior to act on the character.

//Line 7. Enable collision
myAstronaut->enableCollisionBehavior();

Step 3. Set the Coefficient of Restitution & Mass

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.

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
myAstronaut->initCoefficientOfRestitution(0.5);
//Line 9. Set Mass
myAstronaut->initMass(1.0);

Finally, let's give the astronaut some space to fall and hit the island. Let's move the astronaut three units up.

//Line 10. Translate the character three units up
myAstronaut->translateBy(0.0, 3.0, 0.0);

Complete code snippet

Code snippet: init() method

The init() method should look as follows. Pay particular attention to lines (5-10):

void Earth::init(){
/*----DO NOT REMOVE. THIS IS REQUIRED-----*/
//Configures the perspective view, shadows, lights and camera.
setupConfiguration();
//Renders the background models: skybox and island models. Remove if you want to.
setupBackgroundModels();
/*----CREATE ASTRONAUT HERE-----*/
//Line 1. Create an instance of U4DGameObject type
myAstronaut=new U4DEngine::U4DGameObject();
//Line 2. Load attribute (rendering information) into the game entity
if (myAstronaut->loadModel("astronaut","charactersAttributes.u4d")) {
//Line 3. Load rendering information into the GPU
myAstronaut->loadRenderingInformation();
//Line 4. Add astronaut to the scenegraph
addChild(myAstronaut);
}
//Line 5. Make sure the character is not null
if (myAstronaut!=nullptr) {
//Line 6. Enable kinetics
myAstronaut->enableKineticsBehavior();
//Line 7. Enable collision
myAstronaut->enableCollisionBehavior();
//Line 8. Set Coefficient of Restitution
myAstronaut->initCoefficientOfRestitution(0.5);
//Line 9. Set Mass
myAstronaut->initMass(1.0);
//Line 10. Translate the character three units up
myAstronaut->translateBy(0.0, 3.0, 0.0);
}
}

Results

In Xcode, select either the iOS or Mac device and then click on Run.

The 3D entity should collide with the island as shown below.

image1

Source Code

Download the source game files from our Github Tutorials page.