Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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  
  LWJGL collision with map and camera.  (Read 617 times)
0 Members and 1 Guest are viewing this topic.
Offline synok

Senior Newbie


Medals: 1



« Posted 2013-10-27 14:59:07 »

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;

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  
56  
57  
58  
59  
60  
61  
62  
63  
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;

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  
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,  Smiley
Offline Seiya02

JGO Coder


Medals: 4
Projects: 4
Exp: 5 years


No Text Here :x


« Reply #1 - Posted 2013-10-27 18:26:39 »

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?
Offline synok

Senior Newbie


Medals: 1



« Reply #2 - Posted 2013-10-27 23:10:53 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Seiya02

JGO Coder


Medals: 4
Projects: 4
Exp: 5 years


No Text Here :x


« Reply #3 - Posted 2013-10-28 10:44:59 »

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

CopyableCougar4 (15 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (38 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!