简体   繁体   English

二维碰撞检测LWJGL

[英]2D Collision Detection LWJGL

I'm new to LWJGL and need help with collision detection. 我是LWJGL的新手,并且需要碰撞检测方面的帮助。 Below I have included the two java classes that I am working with. 下面,我包括了我正在使用的两个Java类。 The Ship.java class draws a square and then you can move the square around. Ship.java类绘制一个正方形,然后可以在周围移动正方形。 The Enemy.java displays red square. Enemy.java显示红色正方形。

I would like to have the ship collide with the enemy. 我想让这艘船与敌人相撞。 I am confused on what to do. 我对做什么感到困惑。

Ship.java Ship.java

package WallWars;


import static org.lwjgl.opengl.GL11.*;



import org.lwjgl.input.Keyboard;

public class Ship {

double x;
double y;
double spdX;
double spdY;
double directionLeft;

Enemy enemy = new Enemy();

public Ship(){

    x = 100;
    y = 100;

}



public void ShipLogic(){

    x += spdX;
    y += spdY;

    //Friction
    if(!Keyboard.isKeyDown(Keyboard.KEY_LEFT) && spdX > 0){
        spdX = spdX * 0.9;
    }
    if(!Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && spdX < 0){
        spdX = spdX * 0.9;
    }
    if(!Keyboard.isKeyDown(Keyboard.KEY_UP) && spdY < 0){
        spdY = spdY * 0.85;
    }
    if(!Keyboard.isKeyDown(Keyboard.KEY_DOWN) && spdY > 0){
        spdY = spdY * 0.85;
    }

    //Keyboard Input
    if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
        spdX = Math.max(-5, spdX - 1);
    }
    if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
        spdX = Math.min(5, spdX + 1);
    }
    if(Keyboard.isKeyDown(Keyboard.KEY_UP)){
        spdY = Math.max(4.5, spdY + 1);
    }
    if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
        spdY = Math.min(-4.5, spdY - 1);
    }

}

public void ShipCollisions(){


    //Wall Collisions
    if(x <= 0){
        spdX = 0;
        x = 0.1;
    }
    if(x >= 768){
        spdX = 0;
        x = 767.9;
    }
    if(y <= 0){
        spdY = 0;
        y = 0.1;
    }
    if(y >= 568){
        spdY = 0;
        y = 567.9;
    }

}

public void EnemyCollisions(){

    if(x > enemy.x + 60 + 32 && x < enemy.x){



    }

}


public void dShip(){

    ShipLogic();
    ShipCollisions();
    EnemyCollisions();

    glBegin(GL_QUADS);

        glColor3d(1, 1, 1);
        glVertex2d(x, y);
        glVertex2d(x + 32, y);
        glVertex2d(x + 32, y + 32);
        glVertex2d(x, y + 32);
    glEnd();


}



}

Enemy.java Enemy.java

package WallWars;

import static org.lwjgl.opengl.GL11.*;


public class Enemy {

double x;
double y;
double posX = x;
double posY = y;

public void dEnemy(double x, double y){

    glBegin(GL_QUADS);
        glColor3d(1, 0, 0);
        glVertex2d(x, y);
        glVertex2d(x + 32, y);
        glVertex2d(x + 32, y + 32);
        glVertex2d(x, y + 32);
    glEnd();

}

}

First of all you can use the Rectangle class of the Java API. 首先,您可以使用Java API的Rectangle类。 It can store a position (x, y) and a width and a height. 它可以存储位置(x,y)以及宽度和高度。

Once all your objects (your ship and your enemy) use Rectangle , you can simply use the intersects method to know if two Rectangle collide. 一旦所有对象(您的船只和敌人)都使用Rectangle ,您就可以简单地使用intersects方法来确定两个Rectangle是否发生碰撞。

The problem you're facing is that you need to encapsulate your behaviour (logic) methods in a "run" (infinite) loop. 您面临的问题是您需要将行为(逻辑)方法封装在“运行”(无限)循环中。

Then, in your main loop if you detect a collision (the simple part) you need to do something about it. 然后,在主循环中,如果您检测到碰撞(简单的部分),则需要对其进行处理。 The first thing you can do is to stop moving your ship. 您可以做的第一件事是停止移动您的船。 Later you can try to avoid the obstacle by taking another path. 稍后,您可以尝试采取其他方法来避免障碍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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