Math Test

Before getting into the new math functions, let’s run the Math class through a few tests to make sure it’s working as expected. This is always a good idea before plugging a new module or class into the engine (and assuming it works without testing). Figure 10.1 shows the output of the MathTest program. Note that this program is using the same values that were used in the VectorTest program back in Chapter 3, but the output has been changed. (Since we know that the Vector3 class is working as expected, the property tests have been removed.) The calculations are now being performed by the Math class rather than by Vector3’s methods. Pay particular attention to the outputs for “Angle to target” and “Linear velocity,” which demonstrate these functions that will be covered next.

Figure 10.1. This program demonstrates the functionality of the Math class.


#include <iostream>
#include <iomanip>
#include "Math.h"
using namespace std;

int main(int argc, char *argv[])
{
    Math math;
    float angle,x,y;
    cout << "MATH TEST" << endl << endl;
    cout.setf(ios::fixed);
    cout << setprecision(2);

    Vector3 A(5,5,1);
    cout << "A = " << A.getX() << "," << A.getY() << "," << A.getZ() << endl;

    Vector3 B(90,80,1);
    cout << "B = " << B.getX() << "," << B.getY() << "," << B.getZ() << endl;
    cout << endl << "Distance: " << math.Distance( A, B ) << endl;
    cout << "Dot Product: " << math.DotProduct( A, B ) << endl;

    Vector3 D = math.CrossProduct( A,B );
    cout << "Cross Product: " <<
        D.getX() << "," << D.getY() << "," << D.getZ() << endl;
    D = math.Normal( A );
    cout << "Normalized A: " << D.getX() << ","
        << D.getY() << "," << D.getZ() << endl;
    D = math.Normal( B );
    cout << "Normalized B: " << D.getX() << ","
         << D.getY() << "," << D.getZ() << endl << endl;

    angle = math.AngleToTarget( A, B );
    cout << "Angle to target: " << angle << " radians (";
    cout << math.toDegrees(angle) << " degrees)" << endl << endl;

    for (angle=0; angle<360; angle+ =45) {
        x = math.LinearVelocityX(angle);
        y = math.LinearVelocityY(angle);
        cout << "Linear velocity (" <<
            setprecision(0) << angle << " degrees): ";
        cout << setprecision(2) << x << "," << y << endl;
    }
    system("pause");
    return 0;
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset