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:

I guess that isn’t possible like that cause your FPS camera is just a single coordinate and a collision would only occur if the point is really inside one of the Triangles of your object which won’t be the case i think. The fact that the camera movement is with sin and cos makes it be coordinates like this for example: 0.53423f etc. for each coordinate which will absolutely never be 100% on a Triangle. Checking collisions between a Face and some moving Point isn’t that easy, but i guess if you save the Old Coordinate and the new Coordinate of the Camera and check if a line between those Coordinates touches a Triangle, that would work better no?

I got a deja-vu feeling I replied here already, but how or what is the best way to do it? What should I use instead of triangles? My OBJ loader is kind of depending on it.

Yeah, the triangles are not the Problem, the Camera is. Checking for a Collision with a point is kinda sensless cause it won’t appear, but checking for the Line between previous camera point and new Camera point might work i guess. Cause when that line crosses a Triangle the Camera had a collision with it. Have a look at this