Skip to main content
gamedev.net
🔒 Locked

D3D camera problems...

Started by krez Oct 10, 2001 at 9:23 PM 3 replies 350+ views
Original Post
krez
krez
i am trying to write a game (a first-person point-of-view flying-a-spaceship-type-thing) just to learn D3D some... but i am having problems with the camera. i can more or less aim the camera where i want, but when the player faces up or down (or even close to up or down) the camera rotates in strange ways (i assume this was meant to be helpful, for programs where you walk around and look; but for what i am trying to do, there is no real "up" since you are in space). this camera thing is screwing up my loops and cuban-8s... i think that changine the camera''s "up" vector each time the player changes direction will solve this problem, but then i would have to rotate the vector i am looking in over an arbitrary axis (90 degrees above forward is up, relatively speaking)... i tried to search for this on the internet, and found all of two pages full of theoretical stuff about "quaterains" or something of that sort. i am using VB5, and DX7. if anyone has some advice for me about how to make this work, please help me. heh heh, if anyone even has a clue about how to rotate an arbitrary vector over another arbitrary vector in VB, tell me (that might be enough). thanks in advance! --- krez (a big sig does not imply a big...)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Shannon Barber
Shannon Barber
I decided to use Q's, because it's the easiest...

Quarternions are to rotations, what vectors are to points.

You can create a Q that represents a 5o rotation upwards relative to the object being rotated . Sound like what you want to do?

You're going to either have to find a Q library for VB or write one yourself. It's just a little trig but I don't have the formulas in front of me.

This is my code for movement, I keep track of the elapsed time each frame (accurately) and update the postion of the object based on its velocity (linear and angular). I maintaine a Q that at any time represents the direction the object is facing (called m_qFacing). This way when they hit the up arrow, they always go forwrd in the direction they are facing, etc...

I had to play around with the axis of rotation to get yaw to be yaw, pitch to be pitch, and roll to be roll.
      
void CAvatar::MoveAhead(float fElapsed_sec)
{
float Move_m = m_fMovementRate_mps * fElapsed_sec;
D3DXVECTOR3 v3Looking_At, v3Up, v3Left;

D3DI3D_QuaternionToOrthoV3(v3Left, v3Up, v3Looking_At, m_qFacing);
m_v3Position += Move_m * v3Looking_At;
m_bMoved=TRUE;
}

void CAvatar::RotateRight(float fElapsed_sec)
{
D3DXQUATERNION dr;

float Turn_r = m_fTurnRate_rps * fElapsed_sec;

D3DMath_QuaternionFromAngles(dr.x, dr.y, dr.z, dr.w, 0.0f, -Turn_r, 0.0f);
m_qFacing = dr * m_qFacing;
m_bTurned = TRUE;
}




Magmai Kai Holmlor
- Not For Rent

Edited by - Magmai Kai Holmlor on October 11, 2001 1:18:43 AM
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
krez
krez
ah, that is my problem... every web site i find about the damn quaternions either says something to the effect of "i won''t go into detail here, but they are great for rotating matrices around an arbitrary axis", or is so deep into math theory that i can''t decipher it enough to code it...
anyone have any ideas?

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
WarMachine
WarMachine
How bout doing what all the old games did?

Instead of rotating the camera, rotate the entire world the same degrees in the opposite direction.

This doesn''t require any more calculations than rotating the camera, since every point gets multiplied by your camera matrix.
So if you want to rotate the world instead of the camera, dump your camera matrix totally so you don''t do an extra matrix multiplication per vertex.
"You can get out of prison, but not out of the cemetery"
Mark Duffill
Mark Duffill
One problem we encountered with camera rotations using eulers was the order of application. If you do it x,y,z order you end up not having a useful pitch rotation. i.e. a free flying camera will act odd. The order we use seems to work for all cases we have used it for :-

#define ANGCOSSIN(ang, cvar, svar) \
{ \
cvar = COSF(ang); \
svar = SINF(ang); \
}

#define ANGCOSSIN3(angx, cvarx, svarx, angy, cvary, svary, angz, cvarz, svarz) \
{ \
ANGCOSSIN(angx, cvarx, svarx); \
ANGCOSSIN(angy, cvary, svary); \
ANGCOSSIN(angz, cvarz, svarz); \
}

//------------------------------------------------------------------------------
// r = rotation, radians, t = translation, s = scale
RotTransMatrix(const VECTOR *r,const VECTOR *t, const VECTOR *s, D3DMATRIX *m)
{
r32 cx,cy,cz,sx,sy,sz;

/////////////////////////////////////////////////////////
ANGCOSSIN3(r->x, cx, sx, r->y, cy, sy, r->z, cz, sz);
/////////////////////////////////////////////////////////
m->m[0][0] = (cy*cz) * s->x;
m->m[1][0] = (sx*sy - cx*cy*sz) * s->y;
m->m[2][0] = (cx*sy + cy*sx*sz) * s->z;
m->m[3][0] = 0.0f;
/////////////////////////////////////////////////////////
m->m[0][1] = (sz) * s->x;
m->m[1][1] = (cx*cz) * s->y;
m->m[2][1] = (-(cz*sx)) * s->z;
m->m[3][1] = 0.0f;
/////////////////////////////////////////////////////////
m->m[0][2] = (-(cz*sy)) * s->x;
m->m[1][2] = (cy*sx + cx*sy*sz) * s->y;
m->m[2][2] = (cx*cy - sx*sy*sz) * s->z;
m->m[3][2] = 0.0f;
/////////////////////////////////////////////////////////
m->m[3][0] = t->x;
m->m[3][1] = t->y;
m->m[3][2] = t->z;
m->m[3][3] = 1.0f;
}
This produces the world space matrix. You''ll have to transpose it to use it as a view matrix.

Note you may have to swap the row column order if its upside down.



[The views stated herein do not necessarily represent the view of the company Eurocom ]

Eurocom Entertainment Software
www.Eurocom.co.uk

The Jackal

Topic Locked

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

Sign in to reply to this topic.