Adding a Translation

From C4 Engine Wiki

Jump to: navigation, search

Adding a Translation

This serves as an example Custom Controller. It presumes you have already worked through the code for that tutorial. If you want to have a translation as well as a rotation you can use the code provided here. I simply replaced the mdl with a WW II plane I downloaded from the net and used this simple code to have a whole fleet of these planes fly at me from over the horizon, twisting as they zoomed overhead.

Change:


    target->SetNodeTransform(originalTransform * transform);

to this:


    //Code added for a translation along the y axis
    Transform4D translation;
    Point3D myTranslation;
    myTranslation.x = 0.0F;
    myTranslation.y = angle;
    myTranslation.z = 0.0F;
    translation.SetIdentity();
    translation.SetTranslation(myTranslation);
    
    //apply the rotation and the translation to the original transform
    target->SetNodeTransform(originalTransform * translation * transform);

You should also change

    if (angle > K::pi) angle -= K::two_pi;
    else if (angle < -K::pi) angle += K::two_pi;

to something like

    if (angle > 100.0F) angle -= 100.0F;
    else if (angle < -100.0F) angle += 100.0F;

because otherwise the translation will be reset very quickly. You will also probably want to change

    rotator.SetRotationAboutZ(angle);

to something like

    rotator.SetRotationAboutY(angle/10.0F);

to roll the plane slowly around the Y axis, instead of spinning it around Z.

Of course, this is an abuse of the variable "angle" to get a quick result. The correct solution would be to add a separate variable (say "speed") which you would separately control via the World Editor.

Personal tools