Jump to content
  • Advertisement
Sign in to follow this  

Rotate camera around the world

This topic is 5176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a little problem to do with rotating the world; I have 2 objects in the middle of my world (the earth rotating around the moon). I have been told that instead of rotating the camera around the world it is easier to keep the camera at a fixed position and rotate the world. So when I move the mouse around in my program, the world gets rotated to where I am moving to. In my display method I have a glRotate(newMouseX, newMouseY, newMouseZ) , but I do not know how to calculate the new positions. I know I have to use Trigonometry. And the values that get updated are newMouseX and newMouseZ. Any help with calculating / implementing this would be much appreciated(prefer Java) Regards Rick.

Share this post

Link to post
Share on other sites
I'm not an expert, but I think you need to use matrixes with Push and Pop. I'll check in my OpenGL book now if you like.

Yup, if you have OpenGL Game Programming by Kevin Hawkins and Dave Astle, its on page 77.

You have to use three matrixes to rotate your world. You must do them in the order of z axis, y axis, and then x axis or it will look weird.

X Rotation Matrix:
| 1 0 0 0 |
| 0 cos # -sin # 0 |
| 0 sin # cos # 0 |
| 0 0 0 1 |

Y Rotation Matrix:
| cos # 0 sin # 0 |
| 0 1 0 0 |
| -sin # 0 cos # 0 |
| 0 0 0 1 |

Z Rotation Matrix:
| cos # -sin # 0 0 |
| sin # cos # 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |

or you could just use this formula for the end results:
a = rotation on the z axis
b = rotation on the y axis
c = rotation on the x axis
x = current x
y = current y
z = current z

x' = x * [(cos a) * (cos b)] + y * [(cos a) * (sin b) * (sin c) - (sin a) * (cos c)] + z * [(cos a) * (sin b) * (sin c) - (sin a) * (sin c)]

y' = x * [(cos a) * (cos b)] +
y * [(cos a) * (sin b) * (sin c) - (cos a) * (cos c)] +
z * [(cos a) * (sin b) * (sin c) - (cos a) * (sin c)]

z' = -x * (sin a) + z * [(cos b) * (sin c)]

hope that helps you!

[Edited by - samoz on August 21, 2004 4:03:47 PM]

Share this post

Link to post
Share on other sites
1.set the mouse position in the middle of the screen
2.at randering time check if the mouse is in the same position
if it is do nothing,else
a)calculate the distance on the x axis and divide it by half of the SCREEN_WIDTH value(800X600 ,divide aprox by 500 you will adjust it in the end)this will be the angle arround Y axis,if it's greater than 1.0f make it 1.0f ,don't want full loop
b)do the same on the screen y axis to get the angle arround x;
3)Compute the rotation matrix using xangle and yangle.
the player stays in the same point but the point he's looking at changes.
|1 0 0|
Rx=|0 cos(angleX) sin(angleX)|
|0 -sin(angleX) cos(angleX)|

Ry=|cos(angleY) 0 sin(angleY)|
| 0 1 0
|-sin(angleY) 0 cos(angleY)|

***in OpenGL gluLookAt(eye.x,eye.y,eye.z, lookat.x,lookat.y,lookat.z, 0,1,0)specifies where the player's eyes are and where he's looking at while(0,1,0) is the vector raising from our world(it doesn't metter right now)****
So we must change the point(lookat.x,lookat.y,lookat.z)

M=Rx*Ry multiply M with the point above to get the new point the player is looking at and use gluLookAt or a similar function to make the update .

This is the simplest approach i know although i might have missed some details but in theory it should work fine lets see if in practice does i made this kind of thing only once so i'm not very sure.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!