Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 1881 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.
Online quew8

JGO Coder


Medals: 33



« Reply #1 - Posted 2014-02-05 23:03:15 »

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.
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
Online quew8

JGO Coder


Medals: 33



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

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.
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
Online quew8

JGO Coder


Medals: 33



« 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?
Online quew8

JGO Coder


Medals: 33



« 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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org 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‑gaming.org
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!