How To Rotate/Position a Camera at a Point

I need to translate my camera around the X axis of a part, while also looking down and rotating around it.

I need to be explained to the equations I use and why I should use them.

Can anyone help me?

Here is a code base that I understand…



Vec3 camera = new Vec3(0, 0, 0);
Vec3 object = new Vec3(0, 0, 0);

//TODO: Set camera away from the camera and pans around it when pressing left or right key
//TODO: Set camera rotation to look at the object at all times
//TODO: Add zooming functionality


So like a third person camera?

Quick guide to your average “lookat” camera matrix:

You treat your matrix as a set of 4 columns, X, Y, Z, W, where each column is a 4-component vector.

The 4th component of the X, Y and Z matrices is 0. The 4th component of the W matrix is 1.

The first 3 components of the Z vector are a 3-dimensional vector representing the direction the camera is facing.
The first 3 components of the Y vector are a 3-dimensional vector representing the “up” direction of the camera (which direction will be ‘up’ on the screen).

The first 3 components of the X vector are a 3-dimensional vector representing the left/right (depending on your coordinate system) direction of the camera, ie: what direction is left/right on the screen. This vector can be derived from the previous two vectors through the cross product operation.

The first three components of the W vector are the position of the camera.

Therefore, set the W column/vector to the location of the camera.
Set the Z column/vector to the direction the camera is facing ((target - camera).normalize())
Use various vector math trickery (which I can’t remember off the top of my head) to get the Y column.
Set the X column to the cross product of the Z and Y vectors.

To pan, you translate the target point.
To zoom, either move the camera closer to the target, or decrease the FOV (they will create somewhat different effects).
To rotate, you move the camera around the target in a circle.

I was looking for some trig.

I am imagining two circles cutting the object in half in the X and Y axis.

I need to translate on the lines of the Y line so that at 90, my character would be facing the top of the object… while 0 would be looking straight at it from it’s front. I need to translate around the same way with the X axis.

I know nothing about trig, but I got on khanacademy and breezed through all the geometry portion and learned what sin, cos, tan and asin, acos, and atan (sin^-1) are and what opposite, adjacent, and hypotenuse are. I learned that aX is for finding missing angles, while X is for finding missing side lengths.

You could use JOML’s math library and had this done by now :slight_smile:

… Please. Other thread problems are not for here.

http://mywiki.wooledge.org/XyProblem

I, along with other members of the forum, are perfectly happy to help you learn this stuff if you’re willing to put in the effort. But we’re not going to waste time solving all your problems when other libraries (in this case, JOML) have already done that.

@Hydroque

What have you done to try and solve this problem yourself? You seem to have a “zero effort and high reward” attitude to life. Here are some solutions to your problem.

  • People have suggested using JOML, its totally ok to not use this (your choice to do this yourself). But its an open source library and you can therefore look at the code and learn from what is being done. If you don’t understand things you can post actual questions

here is a URL that you were too lazy to find yourself

  • You can put your exact question in to your favourite search engine, here is a URL that you were too lazy to type in yourself

https://www.google.com/search?q=How+To+Rotate/Position+a+Camera+at+a+Point

There are pages of results for lots of different situations, each different situation requires a slightly different approach, Hopefully one of them matches what you want.

People are much more willing to help you if you have tried to solve the problem yourself, you might fail epically at it, but its the fact you have tried that will spur people on to help.

p.s. creating two empty vectors doesn’t count as trying ;D

The two vectors denoted how my style of coding was.

As I stated in places on the forums, I am learning trig.

An appropriate answer to my question would be: In order to rotate around a point, you need to find the angles of the pitch, yaw, and rotate accordingly. Use right triangles and trig to determine the angles or missing sides of the triangles.
Or: Could you specify what you are trying to achieve?
Or: What have you tried?
Not: You don’t seem to try anything. Use JOML, or bash the forums. (which obviously I am doing, but don’t like to do)
Who am I to talk about appropriateness on the forums though. :point: :point: :point:

I’ve already visited JOML and that was for determining how matrices were structured. My issue has nothing to do with matrices. Although JOML might have a lookat function, I am learning how to do it for myself and stuff. :point: (and stuff)

Google is hard to search through and this community has applied game developers with more knowledge than I have. Of course I am going to ask a question and assume they know everything about what I don’t know that I want. ;D

Although, whether I got that or not depends on how I asked the question.

HeroesGameDev said

[quote]To rotate, you move the camera around the target in a circle.
[/quote]
Which hinted that. I began looking for answers maybe before maybe after. I don’t remember.

To add to the code base,

float rotation_around_player = 0;
float pitch = camera.getPitch();

These two give me my angles for looking up and down while translating in a circle around the player in both the X and Y axis, more or less. I am still writing out the formulas using right triangles and some geometry to determine stuff.

Great explanation, @HeroesGraveDev! :slight_smile:

Just one thing:

I wish it was so, because it would make life much easier, but I’m affraid it isn’t. :frowning:
The first three components of the fourth column do not tell the position of the camera.
They are instead awkwardly defined as:


rm30 = -leftX * eyeX - leftY * eyeY - leftZ * eyeZ;
rm31 = -upnX  * eyeX -  upnY * eyeY -  upnZ * eyeZ;
rm32 = -dirX  * eyeX -  dirY * eyeY -  dirZ * eyeZ;

So, those are instead the dot products of the right/left, the up and the direction vectors with the camera position.

Why is that so?

Compared with model transformations, where we want to translate some object to its world position and also rotate around its own local coordinate frame, it is indeed the case that the fourth column does denote the position of the object.
But not with a camera, because with a camera we want to rotate the whole world around the camera. So with a view/camera transformation we always want to rotate everything around the camera and not around the center of the coordinate system the camera is defined in.

So, the order of the transformations with object transformation is therefore: translate -> scale -> rotate
But with a camera, where we want to rotate around the camera position, it is: (scale) -> rotate -> translate

Also: In order to obtain the position of the camera when having a view/camera transformation matrix, you actually need to do this: https://github.com/JOML-CI/JOML/blob/master/src/org/joml/Matrix4f.java#L8069

In OpenGL a 4x4 matrix is used to represent a coordinate system. The first three components of the W vector are the origin. The other three vectors of the matrix represent the x, y and z axis. So if your matrix represents the coordinate system of the camera (relative to the world coord. system) then W will be the camera position.
However if it’s the other way around and the matrix represents the world coord. system relative to the camera you can either invert that matrix and look at the W vector or you use the Matrix3f.origin method. Both gives you the same result.
There is nothing awkward about it. But it can be confusing to try and keep track of which coord. system is relative to what and which system you are currently using.

And keeping the order of transformations straight is not very difficult. Btw. if you have a scale and a rotation their order doesn’t matter.

Let’s say we have three transformations A, B and C. And I apply all 3 transformations relative to the world coord. system. i.e. A -> B -> C
Then that is mathematically equivalent to performing the transformations in opposite order C -> B -> A but always relative to the current transformed system.
For example if I want to rotate the camera and than translate it. And both operations should happen relative to the camera than I can instead do those operations in the world coord. system but reverse their order. So I first translate the camera and then rotate it and I don’t even have to change the translation vector or rotation quaternion.

Nice explanation! Especially the note that care must be taken which coordinate system the transformation is relative to. Like you said, this is at times extremely difficult even when concentrating hard. :slight_smile:
My statement that the last column does not represent the position of the camera was meant to be: It does not represent the world-space position of the camera, provided that this view/eye/camera transformation is used to transform from world-space to view-space, which is usually the case with OpenGL (and Direct3D).
You are right, that in order to obtain the world-space position from such a matrix, we have to invert the matrix.