Java平滑2D矩形碰撞? [英] Java Smooth 2D Rectangle collision?
问题描述
大家好,我正在制作一个迷宫游戏,我正在尝试弄清楚如何使 2D 碰撞变得平滑.目前我有一些非常糟糕的碰撞代码,因为我还没有完成这部分,所以我想到了一些非常糟糕的碰撞代码,它有问题,有时会进入墙内然后你无法出去.
Hey guys i'm making a maze game and I'm trying to figure out how to make a 2D collision smooth. Currently I have some really bad collision code that I thought up since I haven't done this part yet and it is glitchy and sometimes goes inside the wall then you can't get out.
我的代码:
public void checkCollision() {
Rectangle player_rectangle = new Rectangle(player.getX(),player.getY(),32,32);
for(Wall wall : walls) {
Rectangle wall_rectangle = new Rectangle(wall.getX(), wall.getY(), 32,32);
if(player_rectangle.intersects(wall_rectangle)) {
if(player.xspeed == 1) {
player.xspeed = 0;
player.x -= 1.2;
} else {
if(player.xspeed == -1) {
player.xspeed = 0;
player.x += 1.2;
}
else
if(player.yspeed == 1) {
player.yspeed = 0;
player.y -= 1;
} else {
if(player.yspeed == -1) {
player.yspeed = 0;
player.y += 1;
}
}
}
}
}
}
进行碰撞的更好方法是什么?这是我将拥有的地图类型的图片:
What is a better way to do the collision? here is a picture of the types of maps I will have:
新的碰撞代码:
if (player_rectangle.intersects(wall_rectangle)) {
Rectangle intersection = (Rectangle) player_rectangle.createIntersection(wall_rectangle);
if (player.xspeed > 0) {
player.x -= intersection.getWidth();
}
if (player.yspeed > 0) {
player.y -= intersection.getHeight();
}
if (player.xspeed < 0) {
player.x += intersection.getWidth();
}
if (player.yspeed < 0) {
player.y += intersection.getHeight();
}
Print(Integer.toString(intersection.width) + ", " + Integer.toString(intersection.height));
}
移动代码:
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT) {
xspeed = -1;
} else {
if(key == KeyEvent.VK_RIGHT) {
xspeed = 1;
} else {
if(key == KeyEvent.VK_UP) {
yspeed = -1;
} else {
if(key == KeyEvent.VK_DOWN) {
yspeed = 1;
}
}
}
}
}
感谢您给我的任何帮助.
Thanks for any help you can give me.
推荐答案
你可以使用Rectangle.createIntersection(Rectangle)
.
如果玩家向正方向移动,只需从他的位置减去结果Rectangle
的宽度和高度,如果他向负方向移动,则添加它们.
If the player moves to a positive direction, just subtract the width and height of the resulting Rectangle
from his position, if he moves to a negative, add them.
不要将速度设置为零,这可能会导致播放器挂起.
Don't set the speed to zero, this could cause the player to hang.
编辑
if (playerRectangle.intersects(wallRectangle) {
Rectangle intersection = (Rectangle) playerRectangle.createIntersection(wallRectangle);
if (player.xspeed > 0) {
player.x -= intersection.getWidth();
}
if (player.yspeed > 0) {
player.y -= intersection.getHeight();
}
if (player.xspeed < 0) {
player.x += intersection.getWidth();
}
if (player.yspeed < 0) {
player.y += intersection.getHeight();
}
}
EDIT2
据我所知,keyPressed 仅在您按下某个键时触发,而不会在您松开时触发.像这样尝试:
As far as I know, keyPressed is only triggered when you press a key, and not when you release it. Try it like this:
private boolean up, down, left, right;
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KEyEvent.VK_UP) {
up = true;
}
...
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KEyEvent.VK_UP) {
up = false;
}
...
}
// Call this in the mainthread
public void updateMovement() {
if (up) {
player.xspeed = 1;
}
...
}
因此,在每一帧中,您都要更新运动、移动玩家,然后检查碰撞.
So in every frame, you update the movement, move the player and then check the collision.
这篇关于Java平滑2D矩形碰撞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!