Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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  
  Camera class  (Read 1714 times)
0 Members and 1 Guest are viewing this topic.
Offline Porlus

Junior Member





« Posted 2012-06-10 02:27:59 »

Hi all, I'm a bit confused on how to manage my camera class and was hoping that someone could set me straight.
At the moment I have a camera class that has an array of 16 floats that I feed into a floatbuffer and multiply with the current modelview matrix in order to perform transformations. Whenever I want to perform a translation I add the forward vector of the matrix to the origin (position of the camera) multiplied by an arbitrary value and when I want to perform a rotation around the Y axis I create a new matrix and set the fields as follows:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
rotMat[0] = Math.toRadians(Math.cos(yRot));
rotMat[1] = 0.0f;
rotMat[2] = Math.toRadians(Math.sin(yRot));
rotMat[3] = 0.0f;

rotMat[4] = 0.0f;
rotMat[5] = 1.0f;
rotMat[6] = 0.0f;
rotMat[7] = 0.0f;
   
rotMat[8] = -Math.toRadians(Math.sin(yRot));
rotMat[9] = 0.0f;
rotMat[10] = Math.toRadians(Math.cos(yRot));
rotMat[11] = 0.0f;

rotMat[12] = 0.0f;
rotMat[13] = 0.0f;
rotMat[14] = 0.0f;
rotMat[15] = 1.0f;


Then I multiply this by the float array containing the current camera transformation and then multiply that by the modelview matrix with glMultMatrix. I'm not really sure if this is correct and I'm just making a silly mistake somewhere or this is completely wrong. Can anyone give me an explanation of how I would do this?

Thanks,

Paul
Offline Roquen
« Reply #1 - Posted 2012-06-10 10:20:42 »

first off: angles are measured in radians...so you want either:  Math.cos(Math.toRadians(X)) assuming X in degrees or Math.cos(X) if already in radians (and likewise for sin).  WRT: Translations...did depends on in what space the translation is occurring...if considered global..then you just shove the values into the 4th column or row (depending on convention you've chosen) and assuming your offsetting from the chosen origin.
Offline Porlus

Junior Member





« Reply #2 - Posted 2012-06-10 15:05:13 »

Yeah this will be in global space and I've converted the sin cos and into radians first. Does it need to be the other way around?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Porlus

Junior Member





« Reply #3 - Posted 2012-06-10 18:17:43 »

Perhaps that was too specific a question. ;x Could somebody give me a brief overview of exactly the sort of transformations that take place in implementing a first person view of a scene?
Offline Nate

JGO Kernel


Medals: 128
Projects: 3
Exp: 14 years


Esoteric Software


« Reply #4 - Posted 2012-06-10 22:43:51 »

Not sure this will help, but here's a class (derived from the jME FlyByCam class) that manipulates the camera for a jME-based FPS:
http://pastebin.com/raw.php?i=N1wc6w7T
That takes a camera and adds mouse rotation for the FPS. The only other camera manipulation is setting the location of the camera to the location of the player. I need to add something to stop the camera from going upside down when looking too far up or down.

If you care about the actual camera, maybe the Camera code from libgdx would help:
https://code.google.com/p/libgdx/source/browse/trunk/gdx/src/com/badlogic/gdx/graphics/Camera.java
It doesn't do much but manage the matrices. The subclasses have more interesting stuff.

Offline Porlus

Junior Member





« Reply #5 - Posted 2012-06-11 06:23:33 »

Well, it's 5:25am, but I got it working. Smiley So I'm happy.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2012-06-11 08:44:15 »

Yeah this will be in global space and I've converted the sin cos and into radians first. Does it need to be the other way around?
You actually didn't do it first, but last. It might be written earlier in the code, but it happens last.

Before I confuse you, this is what you wanted to do:
1  
2  
3  
double yRotDegrees = ...;
double yRotRadians = Math.toRadians(yRotDegrees);
double yRotSin = Math.sin(yRotRadians);

when we move the statements on 1 line, you'll see that when we replace the variables with the calculations, we end up with:
1  
2  
3  
4  
   double yRotDegrees = ...;
-  double yRotRadians = Math.toRadians(yRotDegrees);
-  double yRotSin = Math.sin(yRotRadians);
+  double yRotSin = Math.sin(Math.toRadians(yRotDegrees));

while you did:
1  
2  
   double yRotDegrees = ...;
   double yRotSin = Math.toRadians(Math.sin(yRotDegrees));


This is fundamental to programming / math / logic, so I advice you to look back at your original code and try to really understand why it doesn't work - otherwise you'll find yourself in a heap of trouble, basically incapable of getting much done Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Porlus

Junior Member





« Reply #7 - Posted 2012-06-11 09:32:46 »

Yeah, that was a schoolboy error. I scrapped that about 6 hours ago. Tongue I seem to have a basic mock up of what I want here. Thanks for the replies btw.
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.

xsi3rr4x (26 views)
2014-04-15 18:08:23

BurntPizza (21 views)
2014-04-15 03:46:01

UprightPath (37 views)
2014-04-14 17:39:50

UprightPath (19 views)
2014-04-14 17:35:47

Porlus (35 views)
2014-04-14 15:48:38

tom_mai78101 (61 views)
2014-04-10 04:04:31

BurntPizza (119 views)
2014-04-08 23:06:04

tom_mai78101 (219 views)
2014-04-05 13:34:39

trollwarrior1 (186 views)
2014-04-04 12:06:45

CJLetsGame (193 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!