Hi John,
Well, if we are talking a very minimalistic movement system etc you
can probably get away with only four or five trig formulas for a basic
FPS game. Its really not that complex to calculate the distance
between two coordinates, calculate direction, and update your
position. Here is some sample functions written in C++ that should
give you the basic concept. These functions assume 0 is due north and
180 is south as you requested.
// Function: GetDirection(double, double, double, double).
// Description: Calculates the absolute direction between
// two sets of coordinates.
double GetDirection(double x1, double z1, double x2, double z2)
{
// Subtract x2 from x1
double x = x1 - x2;
// Subtract z1 from z2
double z = z2 - z1;
// Calculate theta by the arc tangent
// of -x*z
double theta = std::atan2(-x, z);
// Now, multiply theta by 180
// and divide by PI
double direction = (theta * 180) / 3.14159265;
// Return the direction
return direction;
}
// Function: GetDistance(double, double, double, double, double, double).
// Description: Calculates the distance between
// two sets of coordinates.
double GetDistance(double x1, double y1, double z1,
double x2, double y2, double z2)
{
// Subtract the first set of coordinates
// from the second set of coordinates
double x = x2 - x1;
double y = y2 - y1;
double z = z2 - z1;
// Square x, y, and z
x = x * x;
y = y * y;
z = z * z;
// Calculate the distance by getting the
// square route of x, y, and z
double distance = std::sqrt(x + y + z);
// Return the distance
return distance;
}
// Function: GetRelative(double, double, double, double, double).
// Description: Calculates the relative direction between
// two sets of coordinates.
double GetRelative(double angle, double x1, double z1,
double x2, double z2)
{
// First, we want to calculate the absolute direction
// between the two coordinates
double absolute = GetDirection(x1, z1, x2, z2);
// Calculate the relative direction by
// subtracting the direction the object is facing
// from the absolute direction
double relative = absolute - angle;
// Return the relative direction
return relative;
}
// Function: GetX(double, double, double).
// Description: Calculates the next possible x coordinate.
double GetX(double direction, double x, double velocity)
{
// Calculate the next x coordinate by
// multiplying velocity by the sine of
// direction*PI/180
x += velocity * std::sin((direction * 3.14159265) / 180);
// Return the new x coordinate
return x;
}
// Function: GetY(double, double, double).
// Description: Calculates the next possible y coordinate.
double GetY(double direction, double y, double velocity)
{
// Calculate the next y coordinate by
// multiplying velocity by the cosine of
// direction*PI/180
y += velocity * std::cos((direction * 3.14159265) / 180);
// Return the new y coordinate
return y;
}
// Function: GetZ(double, double, double).
// Description: Calculates the next possible z coordinate.
double GetZ(double direction, double z, double velocity)
{
// Calculate the next z coordinate by
// multiplying velocity by the cosine of
// direction*PI/180
z += velocity * std::cos((direction * 3.14159265) / 180);
// Return the new z coordinate
return z;
}
That code should give you a very minimal 3d system so you can
calculate distance, direction, and the next x, y, and z coordinates
along a given vector. I might add that x is left/right, y is up/down
vertically, and z represents your forward/back axis.
Cheers!
---
Gamers mailing list __ [email protected]
If you want to leave the list, send E-mail to [email protected].
You can make changes or update your subscription via the web, at
http://audyssey.org/mailman/listinfo/gamers_audyssey.org.
All messages are archived and can be searched and read at
http://www.mail-archive.com/[email protected].
If you have any questions or concerns regarding the management of the list,
please send E-mail to [email protected].