LWJGL collision with map and camera.

Hello,

I have a camera in my project. Basically it is based upon lloyd goodall’s FPS camera found here: http://www.lloydgoodall.com/lwjgl-fpcamera.shtml, but I will post my code anyway;

public class Camera {
	public Vector3f position = null;
	private float yaw = 0.0f;
	private float pitch = 0.0f;
	//private long dtime = System.currentTimeMillis();
	boolean jumping = false;
	boolean falling = false;
	
	public Camera(float x, float y, float z){
		position = new Vector3f(x,y-3.5f,z+20.0f);

	}
	
	public void yaw(float amount){
		yaw += amount;
	}
	
	public void pitch(float amount){
		if(amount <=180 && amount >= -180){
			pitch -= amount;	
		}
	}
	
	public void walkForward(float distance){
		position.x -= distance * (float)Math.sin(Math.toRadians(yaw));
		position.z += distance * (float)Math.cos(Math.toRadians(yaw));
	}
	
	public void walkBackwards(float distance){
		position.x += distance * (float)Math.sin(Math.toRadians(yaw));
		position.z -= distance * (float)Math.cos(Math.toRadians(yaw));
	}
	public void strafeLeft(float distance){
		position.x -= distance * (float)Math.sin(Math.toRadians(yaw-90));
		position.z += distance * (float)Math.cos(Math.toRadians(yaw-90));
	}
	
	public void strafeRight(float distance){
		position.x -= distance * (float)Math.sin(Math.toRadians(yaw+90));
		position.z += distance * (float)Math.cos(Math.toRadians(yaw+90));
	}
	
	public void jump(float distance){
		position.y -= distance * (float)Math.sin(Math.toRadians(yaw));
	}

	public void lookThrough(){
		// Pitch up.
		if(pitch >= -50){
			glRotatef(pitch,1.0f,0.0f,0.0f);
		}else if (pitch <= -50){
			pitch+= (8) + (Math.sin(8));
		}
		// Pitch down.
		if(pitch <= 50){
			glRotatef(pitch,1.0f,0.0f,0.0f);
		}else if (pitch >= 50){
			pitch-= (8) - (Math.sin(8));
		}
		glRotatef(yaw, 0.0f, 1.0f, 0.0f);
		glTranslatef(position.x, position.y, position.z);
	}
}

And I have loaded a map with a custom OBJ loader. The entire map is just one OBJ file. I did this with;

public void loadModel(String model){
		Model m = null;
		try{
			m = OBJLoader.loadModel(new File(model));
		}catch(FileNotFoundException e){
			e.printStackTrace();
		} catch(IOException e){
			e.printStackTrace();
		}
		glTranslatef(0, 0, -20);
		for(Face face : m.faces){
			glBegin(GL_TRIANGLES);
	        Vector3f n1 = m.normals.get((int) face.normal.x - 1);
	        glNormal3f(n1.x, n1.y, n1.z);
	        Vector3f v1 = m.vertices.get((int) face.vertex.x - 1);
	        glVertex3f(v1.x, v1.y, v1.z);

	        Vector3f n2 = m.normals.get((int) face.normal.y - 1);
	        glNormal3f(n2.x, n2.y, n2.z);
	        Vector3f v2 = m.vertices.get((int) face.vertex.y - 1);
	        glVertex3f(v2.x, v2.y, v2.z);

	        Vector3f n3 = m.normals.get((int) face.normal.z - 1);
	        glNormal3f(n3.x, n3.y, n3.z);
	        Vector3f v3 = m.vertices.get((int) face.vertex.z - 1);
	        glVertex3f(v3.x, v3.y, v3.z);
	        glEnd();
		}
	}

This is based upon 2 other classes, but I am sure you get the idea.

Now as I descibed earlier, this OBJ model is the entire map, and I want collision between the camera and the map. What is the easiest way to do this?

EDIT: I noticed the OBJLoader had a loadModel function too. Not to be confused with the function above.

Thanks, :slight_smile: