how to make a camera

this is my current camera code.

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!!

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)

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

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

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!

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

here’s my code for a camera.

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?

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.

(

if(!ready)
            return;

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

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

back.rotateX(yaw);
back.rotateY(pitch);
back.rotateZ(roll);

It is a good way to get some gimbal lock… :frowning:

Ok
viewDir is Null
becauze:

    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

Quaternions solve this problem.

Quaternions are not enough. Sorry, it is not the title of the next James Bond ;D 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.

Thanks for sharing your knowledge with us.

didn’t know that, thanks for the info. :wink:

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

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.

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?

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
[/quote]
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?
[/quote]
There you have to put some values in too, or it does not know what you want from him.

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