[3D] rotate camera around an object up and down from my perspective

So, I’m right now in the making of a model creating program for my future game/s, where I’d like to rotate my camera around an object (I know I can use 3d programs out there like blender but whats the fun in that?). Not that I think it matters but I’m just going to have voxel models.

But the problem I’m facing is making the camera rotate around a certain point in my program. So here is how I’m doing stuff for now:

First off all, I press the mouse wheel button and move up,down, left or right with the mouse. This should give the camera a rotation value. When I move the mouse in the X axis (in 2D space) I call the method Camera.rotateX(rotateAmount); and the same for the Y axis. The problem lies inside the rotateX method, how do I calculate the direction of the camera (rx,ry,rz) when I want to rotate (from my perspective) the x axis by an amount of degrees. If you didn’t catch what I’m trying to do, I’m trying to rotate a camera around a point like they do in solid-works or blender, except I don’t change the point where I’m rotating around (It’s always 0,0,0).

I’m in 11th grade (2nd year of high school?) so I haven’t really learned anything about 3d vectors (actually barely about 2D vectors, though I think I can handle them pretty easy)

Btw, I’ve tried finding an article, forum thread that brings up my kind of rotation but I can’t find one.

Do you need more info just ask instead of ignoring my problem, please :smiley:

Hope you could help and thanks in advance.

http://www.wildbunny.co.uk/blog/vector-maths-a-primer-for-games-programmers/ Have a read of this. It is where I initially learnt enough to get me through several months of intense graphics programming. Even though your problem is about rotating stuff, don’t skip to the rotation section. It’s all important if you don’t know about vector maths.

So I read through the Vectors, Rotation and Matrices, but I had a really hard time understanding matrices and the world space object space thing. So what I’m saying is that I still have no idea on how I could fix the camera problem I had before I’m feeling like I’m not even close to understanding the camera thing. And it doesn’t help that I don’t understand a lot of mathematical words since English isn’t my native language. The only thing I didn’t really get in the vector article was the perpendicular operation. Any advice on how I can understand this more or should I just give up on this 3D thing?

Sure.

A 4x4 matrix can be used to describe any transformation. The identity matrix, that is

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

is the “matrix equivalent of 1.” Which is to say any matrix or vector multiplied by the identity matrix comes out as the same vector or matrix.

If you multiply two matrices together each describing a transform, the result is a matrix describing the concatenation (which means the result of doing one after the other) of the two transforms.

A few transformation matrices:

Scaling by factor Sx, Sy, Sz

Sx 0 0 0
0 Sy 0 0
0 0 Sz 0
0 0 0 1

Translation by vector Tx, Ty, Tz

1 0 0 Tx
0 1 0 Ty
0 0 1 Tz
0 0 0 1

Rotation about X axis by angle theta.

1 0 0 0
0 cos(theta) sin(theta) 0
0 -sin(theta) cos(theta) 0
0 0 0 1

Rotation about Y axis by angle theta.

cos(theta) 0 -sin(theta) 0
0 1 0 0
sin(theta) 0 cos(theta) 0
0 0 0 1

Rotation about Z axis by angle theta.

cos(theta) sin(theta) 0 0
-sin(theta) cos(theta) 0 0
0 0 1 0
0 0 0 1

You don’t need to understand why these are what they are at this stage but later on it can help. How you use these matrices depends on what you’re using. I presume you’re using OpenGL so it depends on are you doing shaders or fixed function pipeline?

As for understanding all the various “spaces,” I advise you take a look at this site. http://glprogramming.com/red/chapter03.html. The terminology is always slightly different and it doesn’t really make sense until you define it in terms of the maths and or the usage.

Yes, I’m using opengl (LWJGL) and from what I understand I’m using fixed function pipeline, I haven’t implemented any shaders (for now I don’t really know what they are, but I can find that out myself)

Woah, that’s a long article :P. I will read through that when I have the time

Yeah well that’s the the chapter on viewing from an old edition of the red book (which is sort of the official guide to OpenGL. Sometimes called the OpenGL Superbible). You only need to read the “Camera Analogy” bit.

If your using fixed function, then you have to upload the matrix with the glLoadMatrix() function. First you’ll want to set the matrix mode to GL_MODELVIEW with glMatrixMode() then you will have to store the matrix in a FloatBuffer. Then you call glLoadMatrix() and until you mess around with matrices again, all vertices will be transformed according to that matrix.

Also since you’re using LWJGL, there is a Matrix4f class which you can use to do all these matrix multiplications, transforms and storing in buffers.

ehm, could you take that 2nd paragraph and make it easier. I’ve only done opengl for a maximum of a month (with the help from YT videos mostly). I didn’t quite understand the thing with FloatBuffers (I’ve never used them). And what exactly do I need to have stored in the Float Buffer (how?). When do I do the glMatrixMode(), loadMatrix,store matrix and loadMatrix(), while I’m rotating or when I’m done rotating?

The fixed function pipeline maintains several matrices (actually several stacks of matrices but…). First of all you need to tell OpenGL which matrix you want to be working with. You should use the modelview matrix for modelling stuff so you call glMatrixMode(GL_MODELVIEW) and then every subsequent matrix command will affect the modelview matrix.

The way Java bindings of OpenGL transfer data to the native OpenGL, is via direct buffers. That is to say subclasses of java.nio.Buffer which are direct. Don’t bother understanding what “direct” means. Just remember to always create Buffers with the LWJGL BufferUtils class. You are going to want to store a matrix, which is 16 float values. So create a buffer with BufferUtils.createFloatBuffer(16); (those names might be slightly wrong but that’s the general idea). To actually put the matrix into the buffer - I’m not sure exactly how LWJGL Matrix4f does it (I don’t use it) but I’m sure you’ll figure it out.

Now all you have to do is send the buffer to OpenGL which is done with the glLoadMatrix() function. This will set the modelview matrix to the matrix you created. Then all vertices you draw will be transformed according to this matrix.