简体   繁体   中英

LWJGL 3D collision Detection

I have a promblem with my 3D collision detection, here is the code:

    private void checkHit() {

   if(ishitable){   

    if(pos.x-xscale < TimeToKill.player.position.x){
        if(TimeToKill.player.position.x > pos.x){
            if(TimeToKill.player.position.y < pos.y){
                if( TimeToKill.player.position.y > pos.y-yscale){
                    if( TimeToKill.player.position.z > pos.z-zscale){

                       if(TimeToKill.player.position.z < pos.z){
                       System.out.println("HIT!" + pos + TimeToKill.player.position);
            }

        }}
    }

}}
}

So it doesen´t works fine, it says HIT!, just when I am standing next to a Entity (it is a cube, so nothing big).

Your second condition is wrong. First line:

pos.x - xscale < TimeToKill.player.position.x

Second line is equivalent to:

pos.x < TimeToKill.player.position.x

So you need to flip the comparison. You should really refactor this into a hitTest method or something. Your current code is really verbose and confusing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM