Jump to content
  • Advertisement
Sign in to follow this  
codemastermm

3rd person mouse rotation

This topic is 4873 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

I am curious as to how one would would use the mouse as to when it moves, it rotates around an object, such as if you moved the mouse to the right a little, it'd rotate the view to the right around the object. Psuedo-code would be helpful as well as normal code. I am using SDL with OpenGL, of course, if anyone has code for them dealing with this.

Share this post


Link to post
Share on other sites
Advertisement
You mean a 3rd person camera?

You could swap the Position vector and Target vectors and pass those to Glu lookat. Although my implementation is a lot diferent.

pseudocode

1. Rotate your players forward, right and up vectors on an arbitray axis. Like your first person camera
2. After rotation, make another vector and copy the values of your forward vector to it.
3. Rotate that vector (on the right vector n-degrees, I use -10 to -20) and subtract that vector from your position vector.
4. Make that vector the camera position to pass to gluLookat.
5. Set the camera target as the players position vector and pass to Glulookat.

Here's a demo with source if you want to see it in action:

glShooter

Controls:
WASD/ARROWS
Mouse move
Mouse Buttons
Space = shoot
1,2,3,4 = to change camera types
5 = show player axis

Share this post


Link to post
Share on other sites
i was thinking more along the lines of an object standing still and when you move your mouse, your view (or camera, if you want to say) rotates around it (sorta like in the Matrix, where Neo "freezes" and the camera seems to spin around him) - you were correct when you said a 3rd person camera, but i don't see how switching the target and position would achieve this? if you did that, it'd be like the opposite of what i'm attempting to achieve.

Share this post


Link to post
Share on other sites
What I did is as follows:

I have a float containing the rotation angle.

camera_position.x = player_pos.x + sin(rotation_angle) * camera_distance_from_player;
camera_position.y = player_pos.y + desired_height_of_camera;
camera_position.z = player_pos.z + cos(rotation_angle) * camera_distance_from_player;

Your camera look direction is the camera_position vector minus the player_pos vector.


When I moved the mouse left or right, it decreased or increased the rotation angle. deltaRotAngle (the change in rotation angle per frame) is dependent on the time elapsed for the last frame multiplied by the desired camera rotation speed in radians per second. You should manually wrap it to the sin/cos period so that you don't lose floating point accuracy as time goes on with multiple same-direction rotations.


Keep in mind that I haven't done 3rd person code like this for a long time... I may have the sin and cos switched around.

Share this post


Link to post
Share on other sites
Quote:
Original post by codemastermm
i was thinking more along the lines of an object standing still and when you move your mouse, your view (or camera, if you want to say) rotates around it (sorta like in the Matrix, where Neo "freezes" and the camera seems to spin around him) - you were correct when you said a 3rd person camera, but i don't see how switching the target and position would achieve this? if you did that, it'd be like the opposite of what i'm attempting to achieve.


The above can do that. Use the same algorithm to get the offset vector and use the spherical system (which can also be found in the demo) to rotate manually. I mean after subtracting, you can rotate the camera forward vector anyway you want.
;*)





Share this post


Link to post
Share on other sites
There was a smooth 3rd person camera tutorial on FireStorm's site (www.firestorm.go.ro). If that helps?

Share this post


Link to post
Share on other sites
i'm trying to use the mouse rather than the arrow keys, so it is definately a bit more tedious. I'm presuming that every change in the mouse's X and Y should be used to change the camera, such as if they move the mouse, i'll check for a change in X and the Y and move the camera accordingly? I hope I am correct in presuming this.

the only problem i have now is I still am not certain as to how to do this, as the tutorials and psuedo-code most of you have provided seem to be made for the keyboard keys rather than the mouse controls.

i have seen this - http://www.gamedev.net/reference/articles/article2160.asp
it somewhat helps, but i'm still very shaky on it, since it keeps using vTheta and hTheta and never really explains how it got them and such of that sort.

[Edited by - codemastermm on June 19, 2005 2:28:44 PM]

Share this post


Link to post
Share on other sites
Both relsoft and myself posted code that will work with the mouse as well as the keyboard. In fact, there really is no difference between the two when you look at it.

During a frame, if you find that the left key was pressed. Assign the rotation speed scalar a value of: -1 * frameTime * maxRotVelInRadiansPerSec


During a frame, you moved the mouse left. Assign the rotation speed scalar a value of: -mouseInfo.mickeys.x * frameTime * maxRotVelInRadiansPerSec
Since the mouse left and right are just changes in sign for the mickeys.x, you could do the same calculation all the time without a check for if the mouse was even moved. Just remove the negative symbol and make sure that line is executed every frame.

As you can see, there really is no big difference between the two. Both are input events, and both rotate the camera if used in conjunction with the code we posted.

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!