Java 3D LWJGL collision -


i making 3d java game lwjgl library, , wondering how add collision detection, player not go through models.

i using obj models. here objloader class, loads models:

package renderengine;  import java.io.bufferedreader; import java.io.file; import java.io.filenotfoundexception; import java.io.filereader; import java.util.arraylist; import java.util.list;  import models.rawmodel;  import org.lwjgl.util.vector.vector2f; import org.lwjgl.util.vector.vector3f;  public class objloader {       public static rawmodel loadobjmodel(string filename, loader loader){         filereader fr = null;         try {             fr = new filereader(new file("res/"+filename+".obj"));         } catch (filenotfoundexception e) {             system.err.println("couldn't load file!");             e.printstacktrace();         }         bufferedreader reader = new bufferedreader(fr);         string line;         list<vector3f> vertices = new arraylist<vector3f>();         list<vector2f> textures = new arraylist<vector2f>();         list<vector3f> normals = new arraylist<vector3f>();         list<integer> indices = new arraylist<integer>();         float[] verticesarray = null;         float[] normalsarray = null;         float[] texturearray = null;         int[] indicesarray = null;         try{             while(true){                 line = reader.readline();                 string[] currentline = line.split(" ");                 if(line.startswith("v ")){                     vector3f vertex = new vector3f(float.parsefloat(currentline[1]),                              float.parsefloat(currentline[2]), float.parsefloat(currentline[3]));                     vertices.add(vertex);                 }else if(line.startswith("vt ")){                     vector2f texture = new vector2f(float.parsefloat(currentline[1]),                              float.parsefloat(currentline[2]));                     textures.add(texture);                 }else if(line.startswith("vn ")){                     vector3f normal = new vector3f(float.parsefloat(currentline[1]),                              float.parsefloat(currentline[2]), float.parsefloat(currentline[3]));                     normals.add(normal);                 }else if(line.startswith("f ")){                     texturearray = new float[vertices.size() * 2];                     normalsarray = new float[vertices.size() * 3];                     break;                 }             }              while(line != null){                 if(!line.startswith("f ")){                     line = reader.readline();                     continue;                 }                 string[] currentline = line.split(" ");                 string[] vertex1 = currentline[1].split("/");                 string[] vertex2 = currentline[2].split("/");                 string[] vertex3 = currentline[3].split("/");                  processvertex(vertex1, indices, textures, normals, texturearray, normalsarray);                 processvertex(vertex2, indices, textures, normals, texturearray, normalsarray);                 processvertex(vertex3, indices, textures, normals, texturearray, normalsarray);                 line = reader.readline();             }              reader.close();         }catch(exception e){             e.printstacktrace();         }         verticesarray = new float[vertices.size()*3];         indicesarray = new int[indices.size()];           int vertexpointer = 0;         for(vector3f vertex:vertices){             verticesarray[vertexpointer++] = vertex.x;             verticesarray[vertexpointer++] = vertex.y;             verticesarray[vertexpointer++] = vertex.z;         }         for(int i=0;i<indices.size(); i++){             indicesarray[i] = indices.get(i);         }         return loader.loadtovao(verticesarray, texturearray, normalsarray, indicesarray);     }      private static void processvertex(string[] vertexdata, list<integer> indices,              list<vector2f> textures,              list<vector3f> normals, float[] texturearray, float[] normalsarray){          int currentvertexpointer = integer.parseint(vertexdata[0]) - 1;         indices.add(currentvertexpointer);         vector2f currenttex = textures.get(integer.parseint(vertexdata[1]) - 1);         texturearray[currentvertexpointer*2] = currenttex.x;         texturearray[currentvertexpointer*2+1] = 1 - currenttex.y;         vector3f currentnorm = normals.get(integer.parseint(vertexdata[2])-1);         normalsarray[currentvertexpointer*3] = currentnorm.x;         normalsarray[currentvertexpointer*3+1] = currentnorm.y;         normalsarray[currentvertexpointer*3+2] = currentnorm.z;     }  } 

thanks!

you have few options here. easiest create axis-aligned bounding box (aabb) around object; can done algorithmically finding minimum , maximum values each axis. applications work fine, isn't precise. it's worth noting, though, if 2 aabbs not intersect, objects don't intersect either; can use fact early-exit collision-checking algorithm.

in addition bounding boxes, game engines employ other basic types of bounding volumes, such bounding spheres. detecting if point in sphere simple checking if distance between center of sphere , point less or equal radius of sphere. investigate bounding volume wikipedia page other types. can approximate true boundaries of object creating compound bounding volumes -- is, bounding volumes composed of several simpler volumes, spheres, boxes, cylinders, etc. how unity game engine handles collision detection.

you might want investigate 2d collision detection algorithms, separating axis theorem example (see further reading section). these algorithms can scaled higher dimensions.

if seems complicated, i'd recommend picking prebaked solution, such bullet (java port). google around bit; you'll find fits use case. don't feel bad if feel overwhelmed; collision detection complicated subject backed decades of research.

further reading:


Comments

Popular posts from this blog

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -