Java 实现像素完美碰撞-Libgdx
我刚开始通过libgdx框架学习游戏编程,我正处于开发的碰撞检测阶段。我做了一个非常简单的游戏,有一些基本的边界框碰撞检测系统。但是,我想实现像素完美碰撞,以提高精度 我将展示一些我认为对帮助您理解正在发生的事情很重要的代码片段 将创建一个二维数组,以定义屏幕上瓷砖的位置:Java 实现像素完美碰撞-Libgdx,java,libgdx,collision-detection,Java,Libgdx,Collision Detection,我刚开始通过libgdx框架学习游戏编程,我正处于开发的碰撞检测阶段。我做了一个非常简单的游戏,有一些基本的边界框碰撞检测系统。但是,我想实现像素完美碰撞,以提高精度 我将展示一些我认为对帮助您理解正在发生的事情很重要的代码片段 将创建一个二维数组,以定义屏幕上瓷砖的位置: int[][] map = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0
int[][] map = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,1,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
我的主游戏类中的create()
方法将一名玩家和三个实体添加到Entity
类型的ArrayList
@Override
public void create () {
batch = new SpriteBatch();
tileTexture = new Texture("block.png");
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
// add some entities including a player
entities.add(new Player(this, 100, 150, 20, 20, 120.0f, new Texture("player.png")));
entities.add(new Entity(this, 50, 150, 20, 20, 120.0f, new Texture("enemy.png")));
entities.add(new Entity(this, 200, 200, 20, 20, 120.0f, new Texture("enemy.png")));
entities.add(new Entity(this, 180, 50, 20, 20, 120.0f, new Texture("enemy.png")));
}
render()方法
在游戏运行时绘制平铺贴图和实体。另一个名为Entity
的类保存特定实体的数据(可以是块/播放器)。数据可以是该实体的x
和y
位置
// draw tile map
// go over each row bottom to top
for(int y = 0; y < mapHeight; y++) {
// go over each column left to right
for(int x = 0; x < mapWidth; x++) {
// tile
if(map[x][y] == 1) {
batch.draw(tileTexture, x * tileSize, y * tileSize);
}
}
}
// draw all entities
for(int i = entities.size() - 1; i >= 0; i--) {
Entity e = entities.get(i);
batch.draw(e.texture, e.x, e.y);
}
代码行e.tileCollision(map[x][y],x,y,newX,newY,direction)此方法中的code>调用实体类中的tileCollision()
方法,该方法打印块与平铺碰撞的位置
要检查实体之间的冲突,我们有以下方法:
public boolean entityCollision(Entity e1, Direction direction, float newX, float newY) {
boolean collision = false;
for(int i = 0; i < entities.size(); i++) {
Entity e2 = entities.get(i);
// we don't want to check for collisions between the same entity
if(e1 != e2) {
// axis aligned rectangle rectangle collision detection
if(newX < e2.x + e2.width && e2.x < newX + e1.width &&
newY < e2.y + e2.height && e2.y < newY + e1.height) {
collision = true;
e1.entityCollision(e2, newX, newY, direction);
}
}
}
return collision;
}
public boolean entityCollision(实体e1、方向、浮点newX、浮点newY){
布尔冲突=假;
对于(int i=0;i
注意:绿色块可以在实体上移动,但不能穿过平铺。这是因为行e1.entityCollision(e2,newX,newY,direction)
调用类Entity
中允许绿色块移动的entityCollision()
方法
这种类型的碰撞检测似乎是基本且低效的(时间复杂度为O(n^2)
)
在这种情况下,如何实现像素完美碰撞
附加问题:如果我想提高效率,我可以使用什么碰撞检测系统来消除不必要的检查
public boolean entityCollision(Entity e1, Direction direction, float newX, float newY) {
boolean collision = false;
for(int i = 0; i < entities.size(); i++) {
Entity e2 = entities.get(i);
// we don't want to check for collisions between the same entity
if(e1 != e2) {
// axis aligned rectangle rectangle collision detection
if(newX < e2.x + e2.width && e2.x < newX + e1.width &&
newY < e2.y + e2.height && e2.y < newY + e1.height) {
collision = true;
e1.entityCollision(e2, newX, newY, direction);
}
}
}
return collision;
}