hey there guys, I have a problem figuring out the are of some triangles within a box, and thought id ask here first as you guys are pretty awesome when it comes to this sort of stuff!
so recently I have been looking into 3D game development from scratch using the LWJGL library for Java, and I am currently trying to create a collision detection system using bounding boxes around things, so the way I am currently going about it is I am testing whether a point/vector of my characters bounding box is entering another bounding box, and I will do this for all the points on my bounding box to ensure that there is no collision an the target location.
so I have managed to get it to work, well it sort of works, but at certain points of the box it doesn't work, and I cant find the problem, here is the code to my collision detection:
public boolean collide(Cube collider, float tX, float tY, float tZ) {
boolean check;
//first check the collision of players collider vertices
check = testArea(this.collider.rotFBL, collider, tX, tY, tZ);
if (check) {
return true;
}
check = testArea(this.collider.rotFBR, collider, tX, tY, tZ);
if (check) {
return true;
}
check = testArea(this.collider.rotBBR, collider, tX, tY, tZ);
if (check) {
return true;
}
check = testArea(this.collider.rotBBL, collider, tX, tY, tZ);
if (check) {
return true;
}
//next check the targets collider vertices
//check = testArea(collider.rotFBL, this.collider, tX, tY, tZ);
//if (check) return true;
//check = testArea(collider.rotFBR, this.collider, tX, tY, tZ);
//if (check) return true;
//check = testArea(collider.rotBBR, this.collider, tX, tY, tZ);
//if (check) return true;
//check = testArea(collider.rotBBL, this.collider, tX, tY, tZ);
//if (check) return true;
//if all checks don't return true then there is no collision
return false;
}
tX ect. are the target coordinates, and what that is doing is sending the points to be individually checked to see whether they are inside the target box, also note the rotFBl etc are the rotated points named acordingly, for example rotFBL is rotatedFrontBottomLeft, next the method that checks if the point is inside the area:
private boolean testArea(Vector3f vector, Cube collider, float tX, float tY, float tZ) {
float lArea = Math.abs(collider.rotFBR.x * (collider.rotBBR.z - (vector.z + tZ)) +
collider.rotBBR.x * ((vector.z + tZ) - collider.rotFBR.z) +
(vector.x + tX) * (collider.rotFBR.z - collider.rotBBR.z)) / 2;
float rArea = Math.abs(collider.rotFBL.x * (collider.rotBBL.z - (vector.z + tZ)) +
collider.rotBBL.x * ((vector.z + tZ) - collider.rotFBL.z) +
(vector.x + tX) * (collider.rotFBL.z - collider.rotBBL.z)) / 2;
float tArea = Math.abs(collider.rotBBL.x * (collider.rotBBR.z - (vector.z + tZ)) +
collider.rotBBR.x * ((vector.z + tZ) - collider.rotBBL.z) +
(vector.x + tX) * (collider.rotBBL.z - collider.rotBBR.z)) / 2;
float bArea = Math.abs(collider.rotFBL.x * (collider.rotFBR.z - (vector.z + tZ)) +
collider.rotFBR.x * ((vector.z + tZ) - collider.rotFBL.z) +
(vector.x + tX) * (collider.rotFBL.z - collider.rotFBR.z)) / 2;
float fullArea = lArea + bArea + rArea + tArea;
float fullBoxArea = collider.xScale * collider.zScale;
//System.out.println("full area = " + fullArea);
//System.out.println("box area = " + fullBoxArea);
if (fullArea > fullBoxArea) {
return false;
}
return true;
}
now what im doing here is dividing the square into 4 triangle, where the point of the triangle is the target position, then what i do is calculate the area of my square, and the area of all the triangles, if the area of the calculated triangles is greater than the square then the position is ouside the box, if it is equal then it is is inside.
to be honest im not sure what could be wrong, as i believe i have calculated the areas properly, i seem to slightly glitch inside it if i spend a while rubbing up against it, also i tested it walking ontop of the box and there seemed to be a fall through point just around the centre next to each face, so im guessing that when the target coordinate was at that point it lets me glitch through
any insight would be really helpful with this as its driving me crazy!
i can also provide the source code if anyone wants to look deeper into it, im using eclipse as my workspace