Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (780)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (857)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [3D] rotate camera around an object up and down from my perspective  (Read 9241 times)
0 Members and 1 Guest are viewing this topic.
Offline TotalSpelNerd

Senior Newbie

Exp: 3 years

« Posted 2014-02-05 20:54:50 »

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 Cheesy

Hope you could help and thanks in advance.
Offline quew8

JGO Knight

Medals: 53

« Reply #1 - Posted 2014-02-05 23:03:15 » 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.
Offline TotalSpelNerd

Senior Newbie

Exp: 3 years

« Reply #2 - Posted 2014-02-06 17:29:34 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline quew8

JGO Knight

Medals: 53

« Reply #3 - Posted 2014-02-06 19:11:05 »


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. 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.
Offline TotalSpelNerd

Senior Newbie

Exp: 3 years

« Reply #4 - Posted 2014-02-06 19:41:39 »

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 Tongue. I will read through that when I have the time
Offline quew8

JGO Knight

Medals: 53

« Reply #5 - Posted 2014-02-06 20:12:16 »

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.
Offline TotalSpelNerd

Senior Newbie

Exp: 3 years

« Reply #6 - Posted 2014-02-07 14:39:32 »

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?
Offline quew8

JGO Knight

Medals: 53

« Reply #7 - Posted 2014-02-08 14:34:54 »

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.
Pages: [1]
  ignore  |  Print  

hadezbladez (732 views)
2018-11-16 13:46:03

hadezbladez (370 views)
2018-11-16 13:41:33

hadezbladez (722 views)
2018-11-16 13:35:35

hadezbladez (185 views)
2018-11-16 13:32:03

EgonOlsen (2381 views)
2018-06-10 19:43:48

EgonOlsen (2535 views)
2018-06-10 19:43:44

EgonOlsen (1471 views)
2018-06-10 19:43:20

DesertCoockie (2135 views)
2018-05-13 18:23:11

nelsongames (1932 views)
2018-04-24 18:15:36

nelsongames (2614 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!