Impossible to make player collide with map structure !

Started by Yuri6037, September 15, 2014, 17:17:35

Previous topic - Next topic

Yuri6037

Hello all,

Again, still on my 3D Shoot Game inspired from S4League but without the "s4client.exe has stopped working...", i'm trying to make my player coliding with map strcuture. The map structure on my game would be only some blocks with a defined width, height and float coords (x, y, z).

I tried everything but i can't get the player to colide with blocks on X and Z at one time. I need to choose if i want my player coliding on X axis or Z axis but can't get both axis detection.

Here is my code :
    private void checkColisions(){
        /*Note : My engine Player class coord system is working differently than Minecraft or some other games. 
        Indead everything is inversed (example y + 2 should be y - 2 to make player moving up of 2 units)
        Same for X coord and Z coord.
        */
        //position = Vector3f of the player position in world.
        float x = thePlayer.position.x;
        float z = thePlayer.position.z;
        float y = thePlayer.position.y;

        for (Block b : mapStructure){
            if (b != null) {                
                if (x <= (b.x * -1) && x >= ((b.x * -1) - b.width)) {
                    if (z <= (b.z * -1) && z >= ((b.z * -1) - b.width)) {
                        
                        //Just a test to be sure engine knows how to kill a player : yes he knows ! I can die oh yay !!
                        //thePlayer.decreaseHealth(0.001F);

                        if(x <= (b.x * -1) && x > ((b.x * -1) - 0.1)){
                            thePlayer.position.x = (b.x * -1);
                        } else if (x >= ((b.x * -1) - b.width) && x > (((b.x * -1) - b.width) - 0.1)){
                            thePlayer.position.x = ((b.x * -1) - b.width);
                        }

                        if (z <= (b.z * -1) && z > ((b.z * -1) - 0.1)){
                            thePlayer.position.z = (b.z * -1);
                        } else if (z >= ((b.z * -1) - b.width) && z > (((b.z * -1) - b.width) - 0.1)){
                            thePlayer.position.z = ((b.z * -1) - b.width);
                        }

                    }
                }
            }
        }
    }

Yuri6037

Nobody !

Nobody has ever tried to make any type of 3D colision detection ??

Wicpar

first you may want to check y too and add a depth to the blocks (since it is a 3D game) then you may want to be shure the bosition of the player is at the most bottom point of the model, then you may want to do:

if ( player collides on x axis || player collides on y axis || player collides on z axis){
put the player on the most most close posssible point on the collided axis(es)
}

if you want more accuracy, you may wanna check collision not with a point but a box on the player, so if you collide it with a plane, there is less chances to fall through

if you have plenty of blocks you may wanna divide the space in big cube (like chunks) and detect only collision in the 27 closest chunks with the player and then check distance and compare it with the bounding sphere (if (distance from 2 things < bounding sphere distance from first object + the one from the other){check for collision}


if you use multithreading you may want to have your collision and movement thread fusionned(or in constant communication)

Yuri6037

There's only one problem : How can i put the player on the most close point on the collided axis(es) ?

abcdef

There are plenty of 3D collision tutorial web sites out there (its a well known subject), and there are some very good libraries. Have a look a JBullet (as an example).

The secret to good collision detection is making sure you understand what you are checking and its hard to tell from your code. A few questions you need to ask yourself.

What shape are the two collision objects?
Are you treating the axes as planes?
How do you treat the shapes after rotations?
Are you working out axis aligned boxes for your shapes? or any other boundary shapes to make things easier?

Once you have this you can then explain to the public which scenario you are testing and all the facts that would make helping you easier. The question you are asking is not really LWJGL related so you might want to asking on the JGO site.

Stuuupiiid

If you're going to use cubes you have two possibilities:

1st: AABBs (google it). Only works if those cubes are not going to rotate.

2nd: OBBs and the "seperating axis theorem" (google it).
Yeah, my name is funny :P