Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  how to make a camera  (Read 5735 times)
0 Members and 1 Guest are viewing this topic.
Offline deepthought
« Posted 2010-09-08 01:01:23 »


this is my current camera code.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
package com.google.sites.lvgeeks.deepthought.utils.opengl;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
public class simplecam {

   public static void look(GL gl,GLU glu,int FOV,float aspectratio, int atx, int aty, int atz, int yaw, int pitch)
   {
     
      while(pitch>360||pitch<0)
      {
      if(pitch>360)
      {
         pitch-=360;
      }
      if(pitch<0)
      {
         pitch+=360;
      }
      }
     
      while(yaw>360||yaw<0)
      {
      if(yaw>360)
      {
         yaw-=360;
      }
      if(yaw<0)
      {
         yaw+=360;
      }
      }
     
      double yawrad = (Math.PI*yaw)/180;
      double pitchrad = (Math.PI*pitch)/180;
     
      int lookz=atz+(int)(30*Math.cos(yawrad));
      int lookx=atx+(int)(30*Math.sin(yawrad));
      int looky=aty+(int)(30*Math.tan(pitchrad));
     
     
      gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
     
      glu.gluPerspective(FOV, aspectratio, 0.5, 9000);
     
     
          glu.gluLookAt(atx, aty, atz, lookx, looky, lookz, 0, 1, 0);
     
     
        //System.out.println(lookx+" "+ looky+" "+ lookz);
       gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
   }
   
}


how can i add roll to my camera?

Thanks in advance!! 

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Online ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #1 - Posted 2010-09-08 10:14:27 »

The last three arguments to gluLookAt specify the camera's up vector. Alter this to roll the viewpoint.
For example, assume that your camera is at (0,0,0), and is looking at something at (0,0,1). When the camera is upright, the up vector is (0,1,0). With a 90 degree roll to the left, the up vector will be (-1,0,0)
Offline kulpae

Senior Newbie


Exp: 9 years



« Reply #2 - Posted 2010-09-08 10:47:54 »

I've made my fps camera based on this tutorial:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=Quaternion_Camera_Class

maybe that'll help you Wink

edit: here is my cam (first code snippet): http://www.uraniumlane.net/posts/2
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline deepthought
« Reply #3 - Posted 2010-09-09 00:41:26 »

where can i get  the Vector3f and Quaternion classes? i'm using plain JOGL, not lwjgl.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline deepthought
« Reply #4 - Posted 2010-09-09 00:59:53 »

never mind. a copy of lwjgl came with jake2. i'll just use the classes from that.

wait a moment. i don't see anything to rotate the camera around it's Z axis!

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline deepthought
« Reply #5 - Posted 2010-09-09 02:16:02 »

ooooohhh, this should work  azerdark.wordpress.com/2010/01/09/fps-camera-algorithm/.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline deepthought
« Reply #6 - Posted 2010-09-09 18:05:10 »

here's my code for a camera.


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
package com.google.sites.lvgeeks.deepthought.utils.opengl.bettercam;

import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;

public class Awesomecam {

   CamBackEnd back;
   boolean ready=false;
   public Awesomecam()
   {
      back= new CamBackEnd();
      ready=true;
   }
   
   public void look(GL gl,GLU glu,int FOV,float aspectratio, float atx, float aty, float atz, float yaw, float pitch,float roll)
   {
      if(!ready)
         return;
     
      back.reset();
      back.move(new Vector3(atx,aty,atz));
      back.rotateX(yaw);
      back.rotateY(pitch);
      back.rotateZ(roll);
      Vector3 at=back.getCameraPosition();
      Vector3 lookat=back.getCameraTarget();
      Vector3 headup=back.getUpVector();
     
       glu.gluLookAt(at.X, at.Y, at.Y, lookat.X, lookat.Y, lookat.Z, headup.X, headup.Y, headup.Z);
   
}
}




the cambackend class is just that camera class the form tah  link on the previous post. when i try to use the class i get a nullpointer exception from vectorutils. why is that?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Bananni

Senior Newbie




Belive the hype!


« Reply #7 - Posted 2010-09-09 21:18:16 »

where u get the error? could u tell us the code from that line?
uhm, u used the right package?
The code from the website seems to work, they juz forgot a ) somewhere @ the top.

(
1  
2  
if(!ready)
            return;

i dont think that u need that if u use:
Awesomecam cam = new Awesomecam();
cam.look(...);
)

Offline deepthought
« Reply #8 - Posted 2010-09-09 21:56:04 »

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
   at com.google.sites.lvgeeks.deepthought.utils.opengl.bettercam.VectorUtils.Vector3Multiplication(VectorUtils.java:38)
   at com.google.sites.lvgeeks.deepthought.utils.opengl.bettercam.CamBackEnd.rotateX(CamBackEnd.java:49)
   at com.google.sites.lvgeeks.deepthought.utils.opengl.bettercam.Awesomecam.look(Awesomecam.java:23)
   at engine.player.setview(player.java:52)
   at engine.WebWolf3d.display(WebWolf3d.java:163)
   at com.sun.opengl.impl.GLDrawableHelper.display(GLDrawableHelper.java:78)
   at javax.media.opengl.GLCanvas$DisplayAction.run(GLCanvas.java:435)
   at com.sun.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:194)
   at javax.media.opengl.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:412)
   at javax.media.opengl.GLCanvas.display(GLCanvas.java:244)
   at javax.media.opengl.GLCanvas.paint(GLCanvas.java:277)
   at javax.media.opengl.GLCanvas.update(GLCanvas.java:354)
   at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
   at sun.awt.RepaintArea.paint(RepaintArea.java:216)
   at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:301)
   at java.awt.Component.dispatchEventImpl(Component.java:4486)
   at java.awt.Component.dispatchEvent(Component.java:4240)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)


i  think that the vector Viewdir is null

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline gouessej
« Reply #9 - Posted 2010-09-10 10:41:18 »

1  
2  
3  
back.rotateX(yaw);
back.rotateY(pitch);
back.rotateZ(roll);

It is a good way to get some gimbal lock...  Sad

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bananni

Senior Newbie




Belive the hype!


« Reply #10 - Posted 2010-09-10 12:25:47 »

Ok
viewDir is Null
becauze:
1  
2  
3  
4  
5  
6  
7  
8  
9  
    public Vector3 getNormal()
    {
        float l = getLength();

        if(l == 0.0f)
            return null;

        return new Vector3(X / l, Y / l, Z / l);
    }

Ur camera says: viewDir = viewDir.getNormal();
But the values of viewDir are:
X=0
Y=0
Z=0
Now u look for the lenght of this vector, the lenght is 0.
So the method give u Null.
So your vector becomes Null.
Anyway, viewDir sounds like an direction, put in a direction or you will get a crappy view

Offline kulpae

Senior Newbie


Exp: 9 years



« Reply #11 - Posted 2010-09-10 16:37:52 »

1  
2  
3  
back.rotateX(yaw);
back.rotateY(pitch);
back.rotateZ(roll);

It is a good way to get some gimbal lock...  Sad

Quaternions solve this problem.
Offline gouessej
« Reply #12 - Posted 2010-09-10 22:05:21 »

Quaternions solve this problem.
Quaternions are not enough. Sorry, it is not the title of the next James Bond  Grin Quaternions + non-eulerian transforms solve this problem. Quaternions + eulerian transforms don't solve this problem. I wrote a very long report about that in French in 2006.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2010-09-10 22:46:35 »

I wrote a very long report about that in French in 2006.
Thanks for sharing your knowledge with us.

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

Senior Newbie


Exp: 9 years



« Reply #14 - Posted 2010-09-10 23:10:56 »

Quaternions are not enough. Sorry, it is not the title of the next James Bond  Grin Quaternions + non-eulerian transforms solve this problem. Quaternions + eulerian transforms don't solve this problem. I wrote a very long report about that in French in 2006.
didn't know that, thanks for the info.   Wink
Offline deepthought
« Reply #15 - Posted 2010-09-12 00:16:33 »

oooooookkkkkkkkaaaaaaaayyyyyyyyyy. english oops i mean java please. i just want to know how to make the camera. all this math is way over my head

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Bananni

Senior Newbie




Belive the hype!


« Reply #16 - Posted 2010-09-12 13:47:55 »

    Vector3 origViewDir = new Vector3(0,-.4f,-1);
    Vector3 origRightVector = new Vector3(1,0,0);
    Vector3 origUpVector = new Vector3(0,1,0);
    Vector3 origPosition = new Vector3(0,2,3);

... some values needed.
U cant look into the direction of no-direction in a System where are no axis.

Offline deepthought
« Reply #17 - Posted 2010-09-12 21:53:29 »

ok no exceptions, but the camera's staring in some random direction. how do i get it to look straight ahead at first like that trig cam i first had?  failing that, does jmonkey or xith3d have a good camera class i can rip off?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Bananni

Senior Newbie




Belive the hype!


« Reply #18 - Posted 2010-09-12 23:21:52 »

Do you know whats a Vector?
If yes, put the values in that u need.
Quote
but the camera's staring in some random direction
Thats becauze i put some random values in.

origPosition = Camera Position
origViewDir = View Direction
origViewDir+origPosition = Target Position

So if u have a target and u have a position for your camera you can easy calculate the view direction on this way:
Target Position - origPosition = origViewDir

Quote
does jmonkey or xith3d have a good camera class i can rip off?
There you have to put some values in too, or it does not know what you want from him.

Offline deepthought
« Reply #19 - Posted 2010-09-12 23:37:20 »

what about rightVector? it also causes nullpointer exceptions if i put zeroes in it.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Bananni

Senior Newbie




Belive the hype!


« Reply #20 - Posted 2010-09-13 15:11:40 »

 Sad
Well what axis do you think is rightVector representing?
A Vector with Zero Lenght cant point in a direction, so its not a vector.  Undecided
1 0 0
btw: rightVector was in my old post...

Offline gouessej
« Reply #21 - Posted 2010-09-14 13:17:59 »

oooooookkkkkkkkaaaaaaaayyyyyyyyyy. english oops i mean java please. i just want to know how to make the camera. all this math is way over my head
The answer is there (use Google Translate as it is in French):
http://jeux.developpez.com/faq/matquat/?page=transformations#Q34b
http://www.univie.ac.at/cga/faq/angles.html

Quote
The current rotations must respect the previous rotations to avoid gimbal lock to prevent any alignment of rotation axis which causes this problem. This problem doesn't depend on the mathematical way you choose to express rotations, it depends on the way to combine rotations. The problem comes from Euler's classical transform: R=Rx.Ry.Rz

Implicitely or explicitly you use it with matrices, quaternions, Euler or Cardan angles, ...

You should correct this transform and it works whatever you used to express your rotations:

R=R[R[R[Ox,a]Oy,b]R[Ox,a]Oz,c] . R[R[Ox,a]Oy,b] .R[Ox,a]

As far as I know, you want to use yaw, pitch and roll therefore you need to use this.

Offline deepthought
« Reply #22 - Posted 2010-09-17 14:18:35 »

now it works

   
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
     
      glu.gluPerspective(FOV, aspectratio, 0.5, 9000);
     
        gl.glMatrixMode(GL.GL_MODELVIEW);
           gl.glLoadIdentity();
         
       gl.glRotatef(-roll, 0, 0, 1);
     
          glu.gluLookAt(atx, aty, atz, lookx, looky, lookz, 0, 1, 0);

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline gouessej
« Reply #23 - Posted 2010-09-17 16:38:12 »

now it works

   
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
     
      glu.gluPerspective(FOV, aspectratio, 0.5, 9000);
     
        gl.glMatrixMode(GL.GL_MODELVIEW);
           gl.glLoadIdentity();
         
       gl.glRotatef(-roll, 0, 0, 1);
     
          glu.gluLookAt(atx, aty, atz, lookx, looky, lookz, 0, 1, 0);

Nice try. Maybe you could remove the call to glRotatef and turn the up vector passed to gluLookAt (by using sine and cosine). You don't use the roll to compute lookx, looky and lookz, you won't have any gimbal lock with this piece of source code.

However, your up vector is wrong. When I look at something above me, my up vector isn't (0,1,0). The up vector depends on yaw, pitch and roll. Then, as you combine 3 rotations, there is a risk of gimbal lock and you should use the formula that I suggested in order to compute this vector correctly.

Offline deepthought
« Reply #24 - Posted 2010-09-17 21:39:44 »

all i know (or care) is that this works.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Bananni

Senior Newbie




Belive the hype!


« Reply #25 - Posted 2010-09-18 20:10:35 »

but it would work better if you do like gouessej said  persecutioncomplex

Offline gouessej
« Reply #26 - Posted 2010-09-18 21:40:17 »

all i know (or care) is that this works.
No, it works only when yaw = 0 and pitch = 0, otherwise the up vector is wrong. It's obvious, you don't use yaw and pitch to compute your up vector (or you have forgotten to copy/paste a piece of source code doing it). You already use yaw and pitch to compute the eye point and the reference point; if you don't want to use my formula but you want a correct result, use at least the yaw or the pitch to compute the up vector (so that you take into account the 3 rotations but not altogether in order to avoid the gimbal lock).

I have forgotten something. The following conventions are used in several 3D engines including Ardor3D:
- the Euler yaw of rotation is called Bank, (rotation around x)
- the Euler roll of rotation is called Heading (rotation around y)
- the Euler pitch of rotation is called Attitude (rotation around z)
With these definitions, the pitch (you called this the roll) is useless to compute the reference point and the roll is useless to compute the up vector.
Maybe do this but with the convention used above:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
     
glu.gluPerspective(FOV, aspectratio, 0.5, 9000);
     
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();

//maybe invert the both lines below
gl.glRotatef(yaw, 1, 0, 0);
gl.glRotatef(-pitch, 0, 0, 1);      
glu.gluLookAt(atx, aty, atz, lookx, looky, lookz, 0, 1, 0);

Offline deepthought
« Reply #27 - Posted 2010-09-19 16:18:36 »

Then, as you combine 3 rotations, there is a risk of gimbal lock and you should use the formula that I suggested in order to compute this vector correctly.

what code should i use to compute this vector in java?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 743
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #28 - Posted 2010-09-19 16:37:27 »

I think the problem here is that beginners don't understand nor experience the problem. My advise would simply be to ignore the problem. Quaternions are a 'big thing' which require some motivation to dig into. As long as it works, it will seem like a lot of unnecessary complex math with exactly the same end result as one has now.

Sooner or later, with nested rotations, the problem will become obvious, and the developer will actually want to invest the time to fix it.

A nice start would be to try to see how lookAt works, and how the rotations are calculated. Once you understand what happens underneath, you can see how to integrate that '-roll rotation over the Z axis' into the equation.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline gouessej
« Reply #29 - Posted 2010-09-19 17:32:24 »

I think the problem here is that beginners don't understand nor experience the problem. My advise would simply be to ignore the problem. Quaternions are a 'big thing' which require some motivation to dig into. As long as it works, it will seem like a lot of unnecessary complex math with exactly the same end result as one has now.
That's why I suggested him a simpler solution using almost the same source code.

what code should i use to compute this vector in java?
It requires the use of quaternions. As you seem to be a beginner, I suggested you another solution that simply uses the source code you provided with only a small difference:
1  
2  
3  
4  
//maybe invert the both lines below (they use the NASA convention, not yours)
gl.glRotatef(yaw, 1, 0, 0);
gl.glRotatef(-pitch, 0, 0, 1);      
glu.gluLookAt(atx, aty, atz, lookx, looky, lookz, 0, 1, 0);

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

pw (24 views)
2014-07-24 01:59:36

Riven (23 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!