Java平滑2D矩形碰撞?
大家好,我正在制作一个迷宫游戏,我正在尝试找出如何使 2D 碰撞变得平滑。目前,我想出了一些非常糟糕的碰撞代码,因为我还没有完成这部分,而且它有故障,有时会进入墙内,然后你就无法出去。
我的代码:
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;
}
}
}
}
}
}
什么是更好的碰撞方法?这是我将拥有的地图类型的图片:
编辑:
新的碰撞代码:
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;
}
}
}
}
}
感谢您给我的任何帮助。
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.
My code:
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:
EDIT:
New Collision code:
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));
}
Movement Code:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
Rectangle.createIntersection(Rectangle)
。如果玩家向正方向移动,只需从他的位置中减去所得
矩形
的宽度和高度,如果他向负方向移动,则将它们相加。不要将速度设置为零,这可能会导致播放器挂起。
EDIT
EDIT2
据我所知, keyPressed 仅在您按下某个键时触发,而不是在您释放该键时触发。尝试这样:
所以在每一帧中,您更新运动,移动玩家,然后检查碰撞。
You can use
Rectangle.createIntersection(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.
EDIT
EDIT2
As far as I know, keyPressed is only triggered when you press a key, and not when you release it. Try it like this:
So in every frame, you update the movement, move the player and then check the collision.