This is going to be lengthy
The way i implemented it was like this. (in Java, but it should port well enough)
i made a Transform class, which held rotation, translation, and scaling data for a Polygon class, which held a list of Vectors that made up the poly. A poly that would move around a lot (such as a door or other entity) was given it's own Transform instance and the hierchy went on from there. (Polyhedrons would hold Poly lists as well as a master Transform, etc)
So then, i precomputed rotation values for that sort of thing;
In a nutshell, when i created a Transform it's constructor was passed the x y and z variables that it's Vector had. Then it went about creating cosine and sine values for the angles passed.
Code snippets use 'this' to emphasize it's a member variable...
Code: Select all
public Transform(float x, float y, float z)
{
this.cosX = Math.cos(x);
this.cosY = Math.cos(y); // Math.cos gets the cosine of the var.
this.cosZ = Math.cos(z);
//... same for Sine values
}
Simple enough.
So now, every update, the Vector class takes it's Transform instance and does this. (Sorry for the messiness, but it's a direct copy-paste)
Code: Select all
// where it all starts
public void addRotation(Transform t)
{
rotateX(t.getCosX(), t.getSinX());
rotateZ(t.getCosZ(), t.getSinZ());
rotateY(t.getCosY(), t.getSinY());
}
// GET ANGLES
public float getAngleX()
{return ((float)Math.atan2(sinX, cosX));}
public float getAngleY()
{return ((float)Math.atan2(sinY, cosY));}
public float getAngleZ()
{return ((float)Math.atan2(sinZ, cosZ));}
// SET ANGLES
public void setAngleX(float angleX)
{
cosX = (float)Math.cos(angleX);
sinX = (float)Math.sin(angleX);
}
public void setAngleY(float angleY)
{
cosY = (float)Math.cos(angleY);
sinY = (float)Math.sin(angleY);
}
public void setAngleZ(float angleZ)
{
cosZ = (float)Math.cos(angleZ);
sinZ = (float)Math.sin(angleZ);
}
// ROTATE AXIS
public void rotateX(float cos, float sin)
{// rotate this vector around the X AXIS
y = y*cos - z*sin;
z = y*sin + z*cos;
}
public void rotateY(float cos, float sin)
{
x = z*sin + x*cos;
z = z*cos - x*sin;
}
public void rotateZ(float cos, float sin)
{
x = x*cos - y*sin;
y = x*sin + y*cos;
}
public void rotateAngle(float angleX, float angleY, float angleZ)
{
rotateX(angleX);
rotateY(angleY);
rotateZ(angleZ);
}
So pretty much, this rotates a Vector around a point. I had to collaborate with my math prof for most of this, so it uses the right-hand rule. Not totally sure what it'd look like with the normal MOH-style ruling.
If you wanted to rotate around an "alien" point, you'd just pass the Transform different values for x, y, and z so it would precompute sine and cosine differently.
sorry i couldn't give you C++ code, not used to it myself. You know how it goes when you get in a roll with one language, don't really want to reinvent the wheel...