三维网格碰撞Java

三维网格碰撞Java,java,3d,lwjgl,collision,mesh,Java,3d,Lwjgl,Collision,Mesh,我似乎无法让3D碰撞正常工作,当我运行引擎时,它认为我总是与某个东西发生碰撞。下面是发生碰撞时执行某些操作的循环 Camera cam = new Camera(); Obj obj = new Obj(); Collision c = new Collision(obj); for(int i=0;i<obj.triangles.size();i+=3) { if(c.triPntCol(obj.triangles.get(i), obj.triangles.get(i+1),o

我似乎无法让3D碰撞正常工作,当我运行引擎时,它认为我总是与某个东西发生碰撞。下面是发生碰撞时执行某些操作的循环

Camera cam = new Camera();
Obj obj = new Obj();
Collision c = new Collision(obj);

for(int i=0;i<obj.triangles.size();i+=3) {
   if(c.triPntCol(obj.triangles.get(i), obj.triangles.get(i+1),obj.triangles.get(i+2), new Vector3f(cam.x,cam.y,cam.z))==true){
      cam.x=cam.px;
      cam.y=cam.py;
      cam.z=cam.pz;
   }
}
摄像头摄像头=新摄像头();
Obj Obj=新Obj();
碰撞c=新的碰撞(obj);

对于(int i=0;i=0&&v>=0&&u+vIf)如果您只是想让它工作,而不是真正理解数学,那么您可能想看看Bullet。它是C++的物理引擎,但有一个名为JBullet的端口。通过使用调试器逐步完成这一过程,您学到了什么?
public boolean triPntCol(Vector3f a,Vector3f b,Vector3f c,Vector3f p ){//Triangle point collision.

    Vector3f v0=new Vector3f(c.x-a.x,c.y-a.y,c.z-a.z);
    Vector3f v1=new Vector3f(b.x-a.x,b.y-a.y,b.z-a.z);
    Vector3f v2=new Vector3f(p.x-a.x,p.y-a.y,p.z-a.z);

    float dot00= (v0.x*v0.x)+(v0.y*v0.y)+(v0.z*v0.z);
    float dot01= (v0.x*v1.x)+(v0.y*v1.y)+(v0.z*v1.z);
    float dot02= (v0.x*v2.x)+(v0.y*v2.y)+(v0.z*v2.z);
    float dot11= (v1.x*v1.x)+(v1.y*v1.y)+(v1.z*v1.z);
    float dot12= (v1.x*v2.x)+(v1.y*v2.y)+(v1.z*v2.z);

    float inv=1/((dot00*dot11)-(dot01*dot01));

    float u=((dot11*dot02)-(dot01*dot12))*inv;
    float v=((dot00*dot12)-(dot01*dot02))*inv;

    if(u>=0 && v>=0 && u+v<1){
        return true;
    }
    else return false;
}
public List<Vector3f> v = new ArrayList<Vector3f>();
public List<Vector3f> vn = new ArrayList<Vector3f>();
public List<Vector2f> vt = new ArrayList<Vector2f>();
public List<Face> face = new ArrayList<Face>();

public ArrayList color = new ArrayList<>();
public ArrayList colorFace = new ArrayList<>();

public ArrayList<Vector3f> triangles = new ArrayList<Vector3f>();

public Texture texture;


public Obj(){

}

public void load(File f,File mtl) throws IOException{//Gets data from obj and mtl files.
    BufferedReader reader = new BufferedReader(new FileReader(f));
    BufferedReader reader2 = new BufferedReader(new FileReader(mtl));
    String line;
    String line2;
    String tag=null;
    String tag2=null;
    while((line = reader.readLine()) !=null){
        if(line.startsWith("v ")){
            float x = Float.valueOf(line.split(" ")[1]);
            float y = Float.valueOf(line.split(" ")[2]);
            float z = Float.valueOf(line.split(" ")[3]);
            v.add(new Vector3f(x,y,z));
        }
        else if(line.startsWith("vn ")){
            float x = Float.valueOf(line.split(" ")[1]);
            float y = Float.valueOf(line.split(" ")[2]);
            float z = Float.valueOf(line.split(" ")[3]);
            vn.add(new Vector3f(x,y,z));    
        }
        else if(line.startsWith("vt ")){
            float x = Float.valueOf(line.split(" ")[1]);
            float y = Float.valueOf(line.split(" ")[2]);
            vt.add(new Vector2f(x,1-y));

        }
        else if (line.startsWith("f ")){
            Vector3f vIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[0]),Float.valueOf(line.split(" ")[2].split("/")[0]),Float.valueOf(line.split(" ")[3].split("/")[0]));
            Vector3f nIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[2]),Float.valueOf(line.split(" ")[2].split("/")[2]),Float.valueOf(line.split(" ")[3].split("/")[2]));
            Vector3f tIndices = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[1]),Float.valueOf(line.split(" ")[2].split("/")[1]),Float.valueOf(line.split(" ")[3].split("/")[1]));

            face.add(new Face(vIndices,nIndices,tIndices,texture.getTextureID()));
            colorFace.add(tag);
            colorFace.add(Float.valueOf(line.split(" ")[1].split("/")[0]));
            colorFace.add(Float.valueOf(line.split(" ")[2].split("/")[0]));
            colorFace.add(Float.valueOf(line.split(" ")[3].split("/")[0]));
        }
        else if (line.startsWith("usemtl ")){
            //use what follows usemtl as a tag 
                tag = line.split(" ")[1];


            //System.out.println(tag);
            while((line2 = reader2.readLine()) !=null){
             if (line2.startsWith("newmtl ")){
                 tag2 = line2.split(" ")[1];
             }

            else if(line2.startsWith("Kd ")){

                    float x = Float.valueOf(line2.split(" ")[1]);
                    float y = Float.valueOf(line2.split(" ")[2]);
                    float z = Float.valueOf(line2.split(" ")[3]);
                    color.add(tag2);
                    color.add(x);
                    color.add(y);
                    color.add(z);

                }

            else if(line2.startsWith("map_Kd ")){
                texture = TextureLoader.getTexture("JPG", ResourceLoader.getResourceAsStream("res/"+line2.split(" ")[1]/*.split("\\\\")[10]*/)); 
                    System.out.println(line2.split(" ")[1]/*.split("\\\\")[6]*/);

                }

            }
        }

    }
    reader.close(); 
    reader2.close();

}