camera rotation
// Previous declarations
player cam; // Create a camera
const float PI = 3.14159265f;
//---------------------------------------------------------------// Object Definitions
//---------------------------------------------------------------// A 3D Point
typedef struct // Define A Point
{
float x, y, z; // Coordinates Of A Point
} point3f;
//---------------------------------------------------------------
// A 3D Vector
typedef struct // Define a vector
{
float x, y, z; // Lengths of vector components
} vector3f;
//---------------------------------------------------------------
typedef struct
{
// position, current view point, up vector
point3f pos, cvp, upv;
} player;
//---------------------------------------------------------------
// This function converts degrees to radians and
// then takes the cosine of the angle
//---------------------------------------------------------------float cosd(float degrees)
{
float radians, angle;
radians = degrees * PI / 180;
angle = (float)cos(radians);
return angle;
}
//---------------------------------------------------------------
// rotate function
//---------------------------------------------------------------
// This Function Will Rotate The Camera View Left And Right
//---------------------------------------------------------------
void RotateCam(float rotAngle)
{
float a, b, c; // variables for working out quadratics
vector3f vectorA,// vector from cam.pos to cam.cvp
vectorB1, vectorB2;// vector from cam.pos to cam.nvp
// get the vector from pos to cvp
vectorA.x = cam.cvp.x - cam.pos.x;
vectorA.z = cam.cvp.z - cam.pos.x;
// convert vectorA to a unit vector
float mag = (float)sqrt(vectorA.x * vectorA.x + vectorA.z * vectorA.z);
vectorA.x /= mag; vectorA.z /=mag;
// calculate the coefficients of the quadratic equation
a = (vectorA.x * vectorA.x) + (vectorA.z * vectorA.z);
b = -2 * vectorA.x * cosd(rotAngle);
c = (cosd(rotAngle) * cosd(rotAngle)) - (vectorA.z * vectorA.z);
// calcualte the answers for x and z
vectorB1.x = (-b + (float)sqrt((b*b) - 4 * a * c)) / (2 * a);
vectorB1.z = (cosd(rotAngle) - vectorA.x * vectorB1.x) / vectorA.z;
vectorB2.x = (-b - (float)sqrt((b*b) - 4 * a * c)) / (2 * a);
vectorB2.z = (cosd(rotAngle) - vectorA.x * vectorB2.x) / vectorA.z;
if (rotAngle > 0)
{
if (cam.cvp.z <= 0)
{
cam.cvp.x = vectorB2.x + cam.cvp.x;
cam.cvp.z = vectorB2.z + cam.cvp.z;
}
else
{
cam.cvp.x = vectorB1.x + cam.cvp.x;
cam.cvp.z = vectorB1.z + cam.cvp.z;
}
}
else if (rotAngle < 0)
{
if (cam.cvp.z <= 0)
{
cam.cvp.x = vectorB1.x + cam.cvp.x;
cam.cvp.z = vectorB1.z + cam.cvp.z;
}
else
{
cam.cvp.x = vectorB2.x + cam.cvp.x;
cam.cvp.z = vectorB2.z + cam.cvp.z;
}
}
}
//---------------------------------------------------------------
// it rotates, kind of, but not the way i expected, can someone please help me or can you rotate the camera without using vectors
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement