How to enable Kinetic Behavior

The steps to apply an external force to a 3D character are summarized below:

  1. Create a Dynamic Action

  2. Compute an external force

  3. Apply the external force

Step 1. Create a Dynamic Action

To enable kinetic behavior 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. At this moment, the physics engine will be able to exert external forces on the model.

//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 4. Load rendering information into the GPU
    myAstronaut->loadRenderingInformation();

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

}

Step 2. Compute an External Force

Next, we need to compute the external force and apply it to the model using the method addForce(). The following snippet shows the process:

void Earth::update(double dt){

    //Line 8. Compute a force=m*(vf-vi)/dt

    //Line 8a. Get the mass of the entity
    float mass=kineticAction->getMass();

    //Line 8b. set a final velocity vector
    U4DEngine::U4DVector3n finalVelocity(2.0,0.0,0.0);

    //Line 8c. compute force
    U4DEngine::U4DVector3n force=finalVelocity*mass/dt;

    //Line 9. Apply force to entity
    kineticAction->addForce(force);

    //Line 10. set initial velocity to zero
    U4DVector3n zero(0.0,0.0,0.0);
    kineticAction->setVelocity(zero);

}

In line 8a we get the mass of the entity using the method getMass(). In line 8b, we set the desired velocity vector. In this instance, we want to velocity to be two units in the +x direction. Finally, in line 8c, we compute the force.

In line 9, we apply the force to the 3D entity using the method addForce().

Since we want a constant force, we set the initial velocity to zero as shown in line 10 using the method setVelocity().

(Optional) Remove Gravity Forces

To prevent entities from falling, the Untold Engine allows you to control the force of gravity through the method U4DEngine::U4DDynamicModel::setGravity(). If you want objects not to fall, the force of gravity must be set to zero.

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

Result

Helper Functions

Here are some code snippets for your reference:

This snippet applies the appropriate force by providing the desired magnitude of the final velocity as an argument.

Note that in this instance, it is required to provide a force direction.

void Astronaut::applyForce(float uFinalVelocity, double dt){

    //force=m*(vf-vi)/dt

    //get the force direction
    forceDirection.normalize();

    //get mass
    float mass=kineticAction->getMass();

    //calculate force
    U4DEngine::U4DVector3n force=(forceDirection*uFinalVelocity*mass)/dt;

    //apply force to the character
    kineticAction->addForce(force);

    //set inital velocity to zero

    U4DEngine::U4DVector3n zero(0.0,0.0,0.0);
    kineticAction->setVelocity(zero);

}

Whereas this code snippet applies a force by providing the desired final velocity vector as an argument:

void Astronaut::applyVelocity(U4DEngine::U4DVector3n &uFinalVelocity, double dt){

    //force=m*(vf-vi)/dt

    //get mass
    float mass=kineticAction->getMass();

    //calculate force
    U4DEngine::U4DVector3n force=(uFinalVelocity*mass)/dt;

    //apply force to the character
    kineticAction->addForce(force);

    //set inital velocity to zero

    U4DEngine::U4DVector3n zero(0.0,0.0,0.0);
    kineticAction->setVelocity(zero);

}

Last updated