Lab 3: Apply a force to a 3D Game Object

Our goal is to apply a force to the 3D model using the steps mentioned in this article.

You will learn how to enable the Physics Engine, how to compute an external force and how to apply it to a 3D entity.

### Step 1. Enable Kinetic Behavior

In the engine, open the file Earth.mm.

Before we can apply a force 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 (line 3) and set its mass to 1.0 (line 4).

//Line 3. Enable Kinetics Behavior
myAstronaut->enableKineticsBehavior();
//Line 4. Set mass of entity
myAstronaut->initMass(1.0);

### Step 2. Remove Gravity Forces

If you were to run the project, you would see that the 3D entity starts to fall. This behavior is expected since Gravity is set by default.

To prevent entities from falling, the Untold Engine allows you to control the force of gravity. In this project, we want the force of gravity to be equal to zero.

Set the force of gravity to zero as shown in line 5 in the snippet.

//Line 5. Set gravity to zero
U4DEngine::U4DVector3n zero(0.0,0.0,0.0);
myAstronaut->setGravity(zero);

#### Complete code snippet

##### Code snippet: init() method

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

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
//Line 3. Enable Kinetics Behavior
myAstronaut->enableKineticsBehavior();
//Line 4. Set mass of entity
myAstronaut->initMass(1.0);
//Line 5. Set gravity to zero
U4DEngine::U4DVector3n zero(0.0,0.0,0.0);
myAstronaut->setGravity(zero);
//Line 6. Load rendering information into the GPU
//Line 7. Add astronaut to the scenegraph
}
}

### Step 3. Compute an External Force

Now, let's compute an external force that will move the astronaut to the right.

Scroll down to the update() method, and compute the force as shown in the snippet below:

void Earth::update(double dt){
//Line 8. Compute a force=m*(vf-vi)/dt
//Line 8a. Get the mass of the entity
float mass=myAstronaut->getMass();
//Line 8b. set a final velocity vector
U4DEngine::U4DVector3n finalVelocity(1.0,1.0,-1.0);
//Line 8c. compute force
U4DEngine::U4DVector3n force=finalVelocity*mass/dt;
//Line 9. Apply force to entity
//Line 10. set initial velocity to zero
U4DEngine::U4DVector3n zero(0.0,0.0,0.0);
myAstronaut->setVelocity(zero);
}

In line 8a we get the mass of the entity and assign it to the variable mass. In line 8b, we set the desired velocity vector. In this instance, we want to velocity to be one unit in the +x direction, one unit in the +y direction and one unit in the -z direction. Finally, in line 8c, we compute the force.

We apply the force to the 3D entity in line 9.

Since we want a constant force, we set the initial velocity to zero as shown in line 10.

### Results

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

The 3D entity should move as shown below: 