【发布时间】:2017-01-14 16:08:17
【问题描述】:
我一直在尝试为 Libgdx 中的矩形查找/创建碰撞检测,但我似乎无处可去。我有一个名为 bucket 的矩形,宽度和高度为 64,还有一个名为 wall 的矩形,宽度和高度为 64。我正在努力做到这一点,以便玩家不会穿过矩形,并且可以在粘在墙上的同时继续移动而没有逐步通过或随机传送。我的方法在有 1 个块时有效,但当有多个块时它就会中断并且不起作用。
我知道这种方法很丑,但它只是实验
private void checkCollisions(Rectangle bucket, Wall wall){
if(bucket.overlaps(wall.getRectangle())){
if(bucket.x > wall.getRectangle().x - 64 && bucket.x < wall.getRectangle().x - 55){
//collision with right side
bucket.x = wall.getRectangle().x - 64;
}
if(bucket.x < wall.getRectangle().x + 64 && bucket.x > wall.getRectangle().x + 55){
//collision with left side
bucket.x = wall.getRectangle().y + 64;
}
if(bucket.y < wall.getRectangle().y + 64 && bucket.y > wall.getRectangle().y + 55){
//collision with top side
bucket.y = wall.getRectangle().y + 64;
}
if(bucket.y > wall.getRectangle().y - 64 && bucket.y < wall.getRectangle().y - 55){
//collision with bottom side
bucket.y = wall.getRectangle().y - 64;
}
}
}
如果有人能指出我正确的方向或分享一些对我有帮助的代码,我将不胜感激。
【问题讨论】:
标签: libgdx collision-detection rectangles