Skip to main content
gamedev.net
🔒 Locked

basic doubt in 3d

Started by tinu 2 replies 1k views
Original Post
tinu
tinu
While trying to orbit around an object by camera movement, To find the next position (while orbiting in x or y axes), Given an angle and a radius, i can use some simple trig like this X = cos(theta) * radius Y = sin(theta) * radius But When I orbit in both X and Y directions at a time, I think I need to assume a sphere How can I compute the next position in 3D ......
TheAdmiral
TheAdmiral
Make the shift to Euler Angles, Spherical coordinates or Quaternions. They each have their pros and cons.

Euler angles are rectilinear angular coordinates representing rotations about each of the three Cartesian axes. Each coordinate is 2p-periodic, which is handy, but representation of an orientation isn't unique and suffers from Gimbal lock. Alternative terminology for the three coordinates is 'Yaw, Pitch, Roll', and D3DX offers a helper function as such: D3DXMatrixYawPitchRoll.

(Unit) Spherical coordinates are simpler than Euler angles. Only two coordinates are required to represent an arbitrary orientation, corresponding to the two degrees of freedom on the sphere surface, providing uniqueness. However, the range of the two coordinates isn't equal (0-2p, 0-p) and periodicity is difficult to emulate. You are probably already familiar with spherical coordinates in the form of geocentric coordinates (longitude, latitude). The unit-spherical-to-Cartesian conversion goes like:

x = sin(q)cos(f)
y = sin(q)sin(f)
z = cos(q)

(0 < q < 2p, 0 < f < p)

Finally, quaternions are a three-dimensional complex representation of rotations. They are slightly tricker to get your head around, but offer the best performance and don't suffer from aperiodicity or Gimbal lock. I suggest you take a look at GameDev's Articles section for information on quaternions.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
tinu
tinu
Yes.I have shifted to quaternions.

To a Quaternion I have assigned an axis and an angle.

D3DXQUATERNION qt1;
qt1.w=25*0.0174;
qt1.x=0.0f;
qt1.y=1.0f;
qt1.z=0.0f;

I construct a rotation matrix out of the quaternion.

D3DXMatrixRotationQuaternion(&matRotate,&qt1);

Then I construct a look at matrix.

D3DXMatrixLookAtLH( &matView, &Right, &Look, &Up );

Then I multiply the rotation matrix to the view matrix.

g_pd3dDevice->SetTransform( D3DTS_VIEW, &(matView*matRotate));

This causes the rotation of the camera.
What should I do (to get the effect of orbiting around the object )to the quaternion.

[Edited by - tinu on March 29, 2007 11:08:19 PM]
TheAdmiral
TheAdmiral
To achieve rotation with (linear) respect to time, you'd need to periodically increment the angle in your render loop. Something like this:

const float ROTATION_SPEED = 1.0f; // In cycles per secondvoid Render(){    ...    static float view_angle = 0.0f;    static long last_tick_count = GetTickCount();        float elapsed_time = reinterpret_cast<float> (GetTickCount() - last_tick_count) / 1000.0f;    view_angle += elapsed_time * ROTATION_SPEED;    // Now update the quaternion and corresponding world-view matrix    ...}


Note that this will rotate the world about the origin, not the camera about the viewing point. I'm not sure if this is what you intended.
I don't think it's necessarily a good idea to separate the view matrix from the rotation in the way you are. Not least, at the end of the calculation, your view matrix won't contain the actual D3DTS_VIEW matrix.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.

Topic Locked

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

Sign in to reply to this topic.