Brian Baker
Brian Baker

Reputation: 9

Java OpenGL 3D Collision

Everything is running perfect, but I cant find any way to implement collision.

I have already made a bounding box collision system however, I'm also in need of a perfect collision system (collision based per vertex).

Here is my attempt at collision using a hitbox mesh the outlines the real mesh:

String[] faceName = entity.getHitboxFaces(); //Gets all the faces from the mesh******
Vector3f[] verts = entity.getHitboxVertices(); //Gets all the vertices******

//Compiles each cube into a list******

for (int i = 0; i < faceName.length;) {
    List < Vector3f > tmpVerts = new ArrayList < Vector3f > ();

    for (int z = 0; z < 6; z++) {
        String[] currentFace = faceName[i].split(",");
        tmpVerts.add(verts[Integer.parseInt(currentFace[0]) - 1]);
        tmpVerts.add(verts[Integer.parseInt(currentFace[1]) - 1]);
        tmpVerts.add(verts[Integer.parseInt(currentFace[2]) - 1]);
        tmpVerts.add(verts[Integer.parseInt(currentFace[3]) - 1]);
        i++;
    }

    xmin = 0;
    ymin = 0;
    zmin = 0;
    xmax = 0;
    ymax = 0;
    zmax = 0;
    //Gets the bounding boxes of each cube******
    for (Vector3f v: tmpVerts) {
        if (v.x < xmin) {
            xmin = v.x;
        }
        if (v.y < ymin) {
            ymin = v.y;
        }
        if (v.z < zmin) {
            zmin = v.z;
        }
        if (v.x > xmax) {
            xmax = v.x;
        }
        if (v.y > ymax) {
            ymax = v.y;
        }
        if (v.z > zmax) {
            zmax = v.z;
        }
    }
    //Scales it to the objects scale******
    xmin *= scale;
    ymin *= scale;
    zmin *= scale;
    xmax *= scale;
    ymax *= scale;
    zmax *= scale;
    //Checks if the point is in the cube******
    if (px >= xmin + (entity.getPosition().x) && px <= xmax + (entity.getPosition().x) && py >= ymin + (entity.getPosition().y) && py <= ymax + (entity.getPosition().y) && pz >= zmin + (entity.getPosition().z) && pz <= zmax + (entity.getPosition().z)) {
        hit = true;
    }

}

I would much rather not have a hitbox system so, if there is another (working) way, please tell me.

Example of my problem.

enter image description here

Upvotes: 0

Views: 556

Answers (1)

reden
reden

Reputation: 1003

Normally, what you do is to create a low-poly version of the model called collision mesh (but your model seems to be fairly low-poly so it might work as it is). Then, one way of doing it is using ray casting. Send a ray (or several) from you character and see if they first

  1. Intersect the bounding volume of the other object. This is so you can save more expensive calculations.
  2. If it does, you see if it intersects the actual (collision) mesh.

If raycasting is not enough, you can do intersect checks on the actual meshes or bounding volume vs mesh. This is more expensive and complex to implement. There seems to be a trove of resources here, though: Object/Object Intersection

Upvotes: 1

Related Questions