碰撞处理程序在 libgdx 中检测得太早
Collision handler detecting too early in libgdx
我创建了一个二维布尔数组来检测我的碰撞。在我的玩家碰撞箱的每个角上都有四个检测器,碰撞只是说如果左边的点是真的,你就不能向左移动,反之亦然。右侧工作完美,但左侧在我的玩家真正到达阻止他的平台之前阻止了我的玩家。这是一张显示正在发生的事情的图片(地上还有另外两个命中框,命中框是红色的)
这里是碰撞处理程序的代码
public void run() {
/*If entity can fall i.e not on a platform*/
if(entity instanceof CollidableObject){
/*Each line is stating that if the current position.x + 1 is true and the y coordinate is true (in grid of course) then
* there must be something there and so stop falling
*/
if(/*mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x)][(int) (((CollidableObject) entity).getGridPositionLeft().y - 1)] || */
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + 1)][(int) (((CollidableObject) entity).getGridPositionLeft().y)] ||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + 1)][(int) (((CollidableObject) entity).getGridPositionLeft().y)]/* ||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionRight().x + 3)][(int) (((CollidableObject) entity).getGridPositionLeft().y )]*/){
entity.canFall(false);
entity.canJump(true);
}else{
entity.canFall(true);
entity.canJump(false);
}
}
if(entity instanceof CollidableObject){
/*Each line is stating that if the current position.y + 1 is true and the x coordinate is true (in grid of course) then
* there must be something there and so stop moving right
*/
if(mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + (int) (((CollidableObject) entity).getDimensions().x / MapGrid.TILE_SIZE))]
[(int) (((CollidableObject) entity).getGridPositionLeft().y + 1)] ||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + (int) (((CollidableObject) entity).getDimensions().x / MapGrid.TILE_SIZE))]
[(int) (((CollidableObject) entity).getGridPositionLeft().y + 2)] ||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + (int) (((CollidableObject) entity).getDimensions().x / MapGrid.TILE_SIZE))]
[(int) (((CollidableObject) entity).getGridPositionLeft().y + 3)]||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + (int) (((CollidableObject) entity).getDimensions().x / MapGrid.TILE_SIZE))]
[(int) (((CollidableObject) entity).getGridPositionLeft().y + 4)]){
entity.canMoveRight(false);
}else
entity.canMoveRight(true);
if(entity instanceof CollidableObject){
/*Each line is stating that if the current position.x is true and the y coordinate is true (or +1 , +2 because
* of the height of the player) then
* there must be something there and so stop moving left
*/
if(mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x)]
[(int) (((CollidableObject) entity).getGridPositionRight().y + 1)] ||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x)]
[(int) (((CollidableObject) entity).getGridPositionLeft().y + 2)] ||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x)]
[(int) (((CollidableObject) entity).getGridPositionLeft().y + 3)]){
entity.canMoveLeft(false);
}else
entity.canMoveLeft(true);
}
}
总体问题是玩家不会离开,因为它检查得太快了,如果不尽快检查,玩家就会在平台中途结束,所以我做错了什么?!非常感谢任何帮助!
答案是在检查左侧碰撞时从右下角而不是左下角进行检查,因为向左移动比向右移动时更新速度更快。这是因为要在向右移动时更新网格位置,您必须在网格位置更新之前一直穿过单元格(它在点击单元格的右上侧时更新)=。如果你向左移动,你会知道这一点,你会立即点击单元格的右上部分并导致即时更新。从右边开始更新,更新一个单元格的时间会更长,这正是获得解决方案所需要的。
我创建了一个二维布尔数组来检测我的碰撞。在我的玩家碰撞箱的每个角上都有四个检测器,碰撞只是说如果左边的点是真的,你就不能向左移动,反之亦然。右侧工作完美,但左侧在我的玩家真正到达阻止他的平台之前阻止了我的玩家。这是一张显示正在发生的事情的图片(地上还有另外两个命中框,命中框是红色的)
这里是碰撞处理程序的代码
public void run() {
/*If entity can fall i.e not on a platform*/
if(entity instanceof CollidableObject){
/*Each line is stating that if the current position.x + 1 is true and the y coordinate is true (in grid of course) then
* there must be something there and so stop falling
*/
if(/*mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x)][(int) (((CollidableObject) entity).getGridPositionLeft().y - 1)] || */
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + 1)][(int) (((CollidableObject) entity).getGridPositionLeft().y)] ||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + 1)][(int) (((CollidableObject) entity).getGridPositionLeft().y)]/* ||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionRight().x + 3)][(int) (((CollidableObject) entity).getGridPositionLeft().y )]*/){
entity.canFall(false);
entity.canJump(true);
}else{
entity.canFall(true);
entity.canJump(false);
}
}
if(entity instanceof CollidableObject){
/*Each line is stating that if the current position.y + 1 is true and the x coordinate is true (in grid of course) then
* there must be something there and so stop moving right
*/
if(mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + (int) (((CollidableObject) entity).getDimensions().x / MapGrid.TILE_SIZE))]
[(int) (((CollidableObject) entity).getGridPositionLeft().y + 1)] ||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + (int) (((CollidableObject) entity).getDimensions().x / MapGrid.TILE_SIZE))]
[(int) (((CollidableObject) entity).getGridPositionLeft().y + 2)] ||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + (int) (((CollidableObject) entity).getDimensions().x / MapGrid.TILE_SIZE))]
[(int) (((CollidableObject) entity).getGridPositionLeft().y + 3)]||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + (int) (((CollidableObject) entity).getDimensions().x / MapGrid.TILE_SIZE))]
[(int) (((CollidableObject) entity).getGridPositionLeft().y + 4)]){
entity.canMoveRight(false);
}else
entity.canMoveRight(true);
if(entity instanceof CollidableObject){
/*Each line is stating that if the current position.x is true and the y coordinate is true (or +1 , +2 because
* of the height of the player) then
* there must be something there and so stop moving left
*/
if(mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x)]
[(int) (((CollidableObject) entity).getGridPositionRight().y + 1)] ||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x)]
[(int) (((CollidableObject) entity).getGridPositionLeft().y + 2)] ||
mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x)]
[(int) (((CollidableObject) entity).getGridPositionLeft().y + 3)]){
entity.canMoveLeft(false);
}else
entity.canMoveLeft(true);
}
}
总体问题是玩家不会离开,因为它检查得太快了,如果不尽快检查,玩家就会在平台中途结束,所以我做错了什么?!非常感谢任何帮助!
答案是在检查左侧碰撞时从右下角而不是左下角进行检查,因为向左移动比向右移动时更新速度更快。这是因为要在向右移动时更新网格位置,您必须在网格位置更新之前一直穿过单元格(它在点击单元格的右上侧时更新)=。如果你向左移动,你会知道这一点,你会立即点击单元格的右上部分并导致即时更新。从右边开始更新,更新一个单元格的时间会更长,这正是获得解决方案所需要的。