Skip to main content
gamedev.net
🔒 Locked

Object Rotations

Started by darrin Feb 24, 2000 at 2:24 PM 6 replies 500+ views
Original Post
darrin
darrin
Hi All, Just a small problem I encountered, any help would be much appreciated. I am creating a flight sim, and have the aircraft doing all the roll, pitch, bank stuff. I now want to rotate the object around the world y-axis, not the AC y-axis. Just doing a PR_RotateEntity(ent,0,1,0) does not work as it rotates the AC around its own Axis, and if it is rolled, this causes problems for me. Any Help? Cheers Darrin
Sovereign
Sovereign
Hi Darrin

I am also working on a space/flight sim type of model.

I don't really have the camera associated with an entity but that should not be a problem to implement. As for the movement controls I,m just using the keyboard for now. That as long as I have key depressed the camera either moves in particular direction and when I release the button there is no change in motion. My basic controls are set up this way 1,2,3,4 left and right up and down (probably usefull for vtol type of aircraft), 5,6 forward and reverse 7,9 pitch 9,0 yaw a,z used for the roll and d & f free camera movement for up and down camera rotation g and h for left and right camera rotations basically to simulate the visuall effect of the pilot looking around the cockpit. So if you move the camera left or right and hit the a and z keys your going see a barrel like roll. So no matter what my orientation such as moving my 0 key which rotates my camera to the right I going to go forward in that direction.
I don't have any roll added when I rotate left or right to give a banking effect which is typical of aircraft movement but that shouldn't be a problem. I can pretty much navigate any part of world space.

Since your were nice about helping me out with a particular
problem, I would be willing to share some sample code that might help solve your problem. Let me know if your interested.

Doug



Edited by - sovereign on 2/24/00 4:17:40 PM

Edited by - sovereign on 2/24/00 4:52:48 PM
darrin
darrin
Hi Doug,
Thanks for the comments. I currently have the aircraft rotating and behaving pretty much like a real Jet (Using Joystick & Throttle), the only problem is when I do Roll, I am trying to get the aicraft to bank around the world y-axis.
I have just had a few thoughts which may help, so will let you know.

I also have the Camera Aligned to the AC Entity (Target Cam), Source Code Below. (Also allows for camera offsets from entity (both target & source positions)

Cheers
Darrin



void AlignCamera(PR_ENTITY *ent)
{
PR_REAL x,y,z,x2,y2,z2;
PR_REAL camx,camy,camz;
PR_REAL camtx,camty,camtz;
PR_ORIENTATION *orient;

camtx=DNPView[DNPView_current].target[0]; //this is from my view structure
camty=DNPView[DNPView_current].target[1]; //just replace with target/source offsets
camtz=DNPView[DNPView_current].target[2];
camx=DNPView[DNPView_current].source[0];
camy=DNPView[DNPView_current].source[1];
camz=DNPView[DNPView_current].source[2];

x = x2 = ent->orientation.location.x;
y = y2 = ent->orientation.location.y;
z = z2 = ent->orientation.location.z;

orient = &ent->orientation;
tvector[0] = camx;
tvector[1] = camy;
tvector[2] = camz;
PR_Transform (orient->rot_mat);
x += tvector[0]; // Add the vector to move it behind the ship
y += tvector[1];
z += tvector[2];
PR_PositionCameraSource (camera, x, y, z);

tvector[0] = camtx;
tvector[1] = camty;
tvector[2] = camtz;
PR_Transform (orient->rot_mat);
x2 += tvector[0];
y2 += tvector[1];
z2 += tvector[2];
PR_PositionCameraTarget (camera, x2,y2,z2);


tvector[0] = 0;
tvector[1] = 1;
tvector[2] = 0;
PR_Transform (orient->rot_mat);
camera->up.x = tvector[0]; // Set the up vector of the camera
camera->up.y = tvector[1];
camera->up.z = tvector[2];

}
TC
TC
I''m not sure if this is the same problem you are describing. But if you are using Eular angles (the angle around the x axis, the angle around the y axis and the angle around the z axis) then there is difficulty in rotating objects about their own axises. For example if your turning a plane/shipship to the left or right you would rotate it along the world space''s y-axis. However if the plane/ship has its noise pointing downward and you want make turn towards its left or right you would have to rotate it along the world space''s z-axis. If the plane/spaceship can be at any angle at any time it is difficult (mabey impossible if you want to turn about all three posible angle) to do the roll, pitch, bank turning with eular angles.

What you can use are Quarternions ( a not often mentioned topic in Computer Graphics that is mentioned in advanced coarses and books). There is an article on the Web about quaternions:

http://www.gamasutra.com/features/programming/19980703/quaternions_01.htm

I am making a 3D space sim-like shooter cam and used quaternions to roll, pitch and bank my space ships. The method I used is kinda of complicated, but if you didn''t find the article helpful I can explain what I did.
darrin
darrin
Thanks for the info TC,
I should really have used Quarternions, looking back on the project, as better for flight modelling. This is only a pretty quick job, so don''t have time to change things.

However I have figured another way to solve my problem by creating a Rotation Matrix (0,rotangle,0) & multiplying it by the current object rotation matrix. (Works exactly as I wanted)

Cheers
Darrin
Kechong
Kechong
Do any of u guys have any sample code I could look at ?
thanks
K-D
darrin
darrin
Hi,
You are probably better looking into Quarternions, rather than matrix rotations for a flight sim. When I write Quarternion stuff I will probably post it somewhere, but this won''t be for a while.


Cheers
Darrin
TC
TC
kechong,

The url:
http://www.gamasutra.com/features/programming/19980703/quaternions_01.htm

has the code for implmenting quaternions.

I can show you how I used them. I still need to optimize a few things in this function.

My graphic engine uses the common scheme of having an poly object reprsented as an array of vertices and an array of faces that uses indexes in the array of vertices. My engine uses rotation matrices to rotate poly objects so I use the function that coverts a quaternion to a rotation matrix. For my space ship objects I add 3 additonal points to my array of vertices: (0,0,1), (0,1,0) and (1,0,0). I call these the _forwardVector, _topVector and _sideVector. I use these "vectors" for the vertor part of the quaternion, e.g is the x, y,z part of the quaternion .

The function Matrix_4x4.setAsQuaternionMatrix(w, x, y, z) turns the Matrix_4x4 object into a rotation matix version of the quaternion

public void turnShip(int verticalTurnAngle, int horizontalTurnAngle, int spinAngle, TrigLookupTable table)
{
int i;
float w, x, y, z, sinTemp,temp_x, temp_y, temp_z;
Point3D temp;
Matrix_4x4 rotationMatrix;
if(horizontalTurnAngle != 0 && horizontalTurnAngle != 360)
{
w = table._cos[(int)(horizontalTurnAngle/2)];
sinTemp = table._sin[(int)(horizontalTurnAngle/2)];
temp = _localVertices[_topVector];
x = sinTemp * temp._x;
y = sinTemp * temp._y;
z = sinTemp * temp._z;

_quaterMatrix.setAsQuaternionMatrix( w, x, y, z);
rotationMatrix = _quaterMatrix;
// rotate all points
for(i = 0; i < _vertexCount; i++)
{
// x componet
temp_x = _localVertices ._x * rotationMatrix._matrix[0][0] +
_localVertices._y * rotationMatrix._matrix[1][0] +
_localVertices[i]._z * rotationMatrix._matrix[2][0];

// y componet
temp_y = _localVertices[i]._x * rotationMatrix._matrix[0][1] +
_localVertices[i]._y * rotationMatrix._matrix[1][1] +
_localVertices[i]._z * rotationMatrix._matrix[2][1];

// z componet
temp_z = _localVertices[i]._x * rotationMatrix._matrix[0][2] +
_localVertices[i]._y * rotationMatrix._matrix[1][2] +
_localVertices[i]._z * rotationMatrix._matrix[2][2];

// store the rotated coordinates back
_localVertices[i]._x = temp_x;
_localVertices[i]._y = temp_y;
_localVertices[i]._z = temp_z;
}
}

if(verticalTurnAngle != 0 && verticalTurnAngle != 360)
{
w = table._cos[Math.round(verticalTurnAngle / 2)];
sinTemp = table._sin[Math.round(verticalTurnAngle/2)];
/*w = (float) Math.cos((verticalTurnAngle / 2) * degToRad);
sinTemp = (float) Math.sin((verticalTurnAngle/2) * degToRad);*/
temp = _localVertices[_sideVector];
x = sinTemp * temp._x;
y = sinTemp * temp._y;
z = sinTemp * temp._z;

_quaterMatrix.setAsQuaternionMatrix( w, x, y, z);
rotationMatrix = _quaterMatrix;
// rotate all points
for(i = 0; i < _vertexCount; i++)
{
// x componet
temp_x = _localVertices[i]._x * rotationMatrix._matrix[0][0] +
_localVertices[i]._y * rotationMatrix._matrix[1][0] +
_localVertices[i]._z * rotationMatrix._matrix[2][0];

// y componet
temp_y = _localVertices[i]._x * rotationMatrix._matrix[0][1] +
_localVertices[i]._y * rotationMatrix._matrix[1][1] +
_localVertices[i]._z * rotationMatrix._matrix[2][1];

// z componet
temp_z = _localVertices[i]._x * rotationMatrix._matrix[0][2] +
_localVertices[i]._y * rotationMatrix._matrix[1][2] +
_localVertices[i]._z * rotationMatrix._matrix[2][2];

// store the rotated coordinates back
_localVertices[i]._x = temp_x;
_localVertices[i]._y = temp_y;
_localVertices[i]._z = temp_z;
}
}

if(spinAngle != 0 // spinAngle != 360)
{
w = table._cos[Math.round(spinAngle/2)];
sinTemp = table._sin[Math.round(spinAngle/2)];
/* w = (float) Math.cos((spinAngle/2) * degToRad);
sinTemp = (float) Math.sin((spinAngle/2) * degToRad);*/
temp = _localVertices[_forwardVector];
x = sinTemp * temp._x;
y = sinTemp * temp._y;
z = sinTemp * temp._z;

_quaterMatrix.setAsQuaternionMatrix( w, x, y, z);
rotationMatrix = _quaterMatrix;
// rotate all points
for(i = 0; i < _vertexCount; i++)
{
// x componet
temp_x = _localVertices[i]._x * rotationMatrix._matrix[0][0] +
_localVertices[i]._y * rotationMatrix._matrix[1][0] +
_localVertices[i]._z * rotationMatrix._matrix[2][0];

// y componet
temp_y = _localVertices[i]._x * rotationMatrix._matrix[0][1] +
_localVertices[i]._y * rotationMatrix._matrix[1][1] +
_localVertices[i]._z * rotationMatrix._matrix[2][1];

// z componet
temp_z = _localVertices[i]._x * rotationMatrix._matrix[0][2] +
_localVertices[i]._y * rotationMatrix._matrix[1][2] +
_localVertices[i]._z * rotationMatrix._matrix[2][2];

// store the rotated coordinates back
_localVertices[i]._x = temp_x;
_localVertices[i]._y = temp_y;
_localVertices[i]._z = temp_z;
}
}
}


Edited by - TC on 3/3/00 12:21:22 PM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.