# Rotate around player.

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

## Recommended Posts

Hi, I have an Question about how to rotate the view round the player like in all the 3D rpg games. How can I calculate and get it Rotate round the player. I have this vectors. Camera (x,y,z) Player (x,y,z) Anyone good at math maybe can help me out here. I have try to do it myself but it dosen't get perfect. thx.

##### Share on other sites
A professional 3rd-person camera implementation can be pretty involved - quaternions, slerping, collision detection with the world, etc. There's a good article on the subject in one of the first two GPG books that would probably be very helpful to you.

I'm not quite sure what you're asking, though. You say you have the player and camera positions. Are you trying to get the camera to point at the player? Or to move in some specific way?

For starters, I'll just tell you that the camera's look-at vector is:

Normalize(playerpos - camerapos);

Unless you're looking straight up or down, 0, 1, 0 (or 0, 0, 1) would work as an 'up' vector for submission via a function such as gluLookAt().

So maybe that will get you started. But you might explain a little more about what you're trying to do.

##### Share on other sites
jyk:

Hi, I am trying to get the camera to rotate around the camera and point at the player. to get the 3D feeling in the character when I play, not only have a freezing camera that follows the player if you know what I mean.

the problem is the math in my head, how to calculate the rotation.

##### Share on other sites
Use gluLookAt() or whatever its DX equivalent is. You can do Gramm-Schmidt orthonormalization by yourself if you like, but there's really no reason to.

##### Share on other sites
To rotate around a point in 2D.

newcampos.x = currentcampos.x += cos(angle) * distance;
newcampos.y = currentcampos.y += sin(angle) * distance;

where distance is the distance between your player and your camera.

3D is an extension of this.

newcampos.x = currentcampos.x + cos(pitch) * distance * sin(heading);
newcampos.y = currentcampos.y + sin(pitch) * distance;
newcampos.z = currentcampos.z + cos(pitch) * distance * sin(heading);

Set your camera position to newcampos, and your lookat vector to the object you are looking at.

This is using euler angles and heavy handed sin/cos, the method mentioned by jyk would be more efficient, but maybe you do understand this easier.?

To stop the rigid look of your camera being locked behind your player you can add a slight offsets to your angles, (or camera position vector) As the player rotates left have the camera swing in the opposite direction a little faster than the player moves, use some dampening to the camera will slow down fairly soon after the player stops moving.

A very simple example for you...

if (playerRotate) { camRotSpeed++; camAngle+=camRotSpeed;};if (camRot>0){ camRotSpeed--;}UpdateCamera();

Hope this helps :-)

##### Share on other sites
Ok, I would start with just getting and keeping the camera in an appropriate place, such as above and behind the player and pointed at the player. Other things such as smooth damping and ease in-ease out will be more complicated - again, I recommend taking a look at that GPG article.

To do the former, you need to choose a position for your camera (such as [0, 1, -2] in a y-up world, scaled accordingly), and then transform that position into world space using the player's matrix. Then, use the equation I mentioned earlier and gluLookAt() or equivalent to make the camera look at the player.

If you're not into matrices yet, you can figure out the same thing in a more crude way - but you at least need to know the direction vector of your player. With that information, the camera position could be something like:

camerapos = playerpos - behindistance * playerdirection;
camerapos.y += cameraheight;

Does that make sense?

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633725
• Total Posts
3013559
×