I have been making a 3d game engine in java and I have been stuck on this problem for a while now.
So the objects are loaded in from a wavefront .obj format and the faces are triangulated.
I need to test whether 2 "GameObjects" are intersecting and I am currently just making a "bounding cube" around each triangulated face. I know it is not the right way to do it, but its just an easy way of doing it that I will change later.
The problem is that this does not work AT ALL. No output at all, they just never collide. I have tried everything, I just dont know what else to do.
The code is fairly straightforward, but maybe Im doing something wrong. Please let me know if you see anything I could change.
Here is the code:
So the objects are loaded in from a wavefront .obj format and the faces are triangulated.
I need to test whether 2 "GameObjects" are intersecting and I am currently just making a "bounding cube" around each triangulated face. I know it is not the right way to do it, but its just an easy way of doing it that I will change later.
The problem is that this does not work AT ALL. No output at all, they just never collide. I have tried everything, I just dont know what else to do.
The code is fairly straightforward, but maybe Im doing something wrong. Please let me know if you see anything I could change.
Here is the code:
public static boolean intersecting(GameObject obj0, GameObject obj1)
{
Model mod0=obj0.model;
Model mod1=obj1.model;
//Loop through the faces of the first object's model
for (int i = 0; i < mod0.faces.size(); i++)
{
Face f1 = mod0.faces.get(i);
Vector3f mMod0;
mMod0 = getSlope(f1);
//Loop through the faces of the second object's model
for(int j = 0; j < mod1.faces.size(); j++)
{
Face f2 = mod1.faces.get(j);
Vector3f mMod1;
mMod1 = getSlope(f2);
//If the slope of the 2 faces are the same, they dont intersect
if(mMod0.x == mMod1.x && mMod0.y == mMod1.y && mMod0.z == mMod1.z)
;
//The slope of the faces are different,
//so it is still possible that they intersect
else
{
Vector3f pa1 = f1.points[0];
Vector3f pa2 = f1.points[1];
Vector3f pa3 = f1.points[2];
Vector3f pb1 = f2.points[0];
Vector3f pb2 = f2.points[1];
Vector3f pb3 = f2.points[2];
//Mins and maxs of each face added to the object's current
//position, to be used in main part of algorithm
float minXA = Math.min(Math.min(pa1.x, pa2.x), pa3.x)+obj0.position.x;
float minYA = Math.min(Math.min(pa1.y, pa2.y), pa3.y)+obj0.position.y;
float minZA = Math.min(Math.min(pa1.z, pa2.z), pa3.z)+obj0.position.z;
float maxXA = Math.max(Math.max(pa1.x, pa2.x), pa3.x)+obj0.position.x;
float maxYA = Math.max(Math.max(pa1.y, pa2.y), pa3.y)+obj0.position.y;
float maxZA = Math.max(Math.max(pa1.z, pa2.z), pa3.z)+obj0.position.z;
float minXB = Math.min(Math.min(pb1.x, pb2.x), pb3.x)+obj1.position.x;
float minYB = Math.min(Math.min(pb1.y, pb2.y), pb3.y)+obj1.position.y;
float minZB = Math.min(Math.min(pb1.z, pb2.z), pb3.z)+obj1.position.z;
float maxXB = Math.max(Math.max(pb1.x, pb2.x), pb3.x)+obj1.position.x;
float maxYB = Math.max(Math.max(pb1.y, pb2.y), pb3.y)+obj1.position.y;
float maxZB = Math.max(Math.max(pb1.z, pb2.z), pb3.z)+obj1.position.z;
//TODO make new algorithm
///TEMPORARY BAD ALGORITHM///
//If any of the points from face2
//are inside the "bounding box" of
//face1, @return true
if(pb1.x > minXA && pb1.x < maxXA){
if(pb1.y > minYA && pb1.y < maxXA){
if(pb1.z > minZA && pb1.z < maxZA)
{
return true;
}
}
}
if(pb2.x > minXA && pb2.x < maxXA){
if(pb2.y > minYA && pb2.y < maxXA){
if(pb2.z > minZA && pb2.z < maxZA)
{
return true;
}
}
}
if(pb3.x > minXA && pb3.x < maxXA){
if(pb3.y > minYA && pb3.y < maxXA){
if(pb3.z > minZA && pb3.z < maxZA)
{
return true;
}
}
}
//TODO make new algorithm
///TEMPORARY BAD ALGORITHM///
//If any of the points from face1
//are inside the "bounding box" of
//face2, @return true
if(pa1.x > minXB && pa1.x < maxXB){
if(pa1.y > minYB && pa1.y < maxYB){
if(pa1.z > minZB && pa1.z < maxZB)
{
return true;
}
}
}
if(pa2.x > minXB && pa2.x < maxXB){
if(pa2.y > minYB && pa2.y < maxYB){
if(pa2.z > minZB && pa2.z < maxZB)
{
return true;
}
}
}
if(pa3.x > minXB && pa3.x < maxXB){
if(pa3.y > minYB && pa3.y < maxYB){
if(pa3.z > minZB && pa3.z < maxZB)
{
return true;
}
}
}
}
}
}
return false;
}