0
私の小さなゲームの種類のための衝突検出システムです。それは、プレーヤーがいつ壁にまっすぐ進むかを伝えることができます。しかし、プレーヤーが斜めになると、壁を通ってクリップします。問題がどこで発生しているのかは分かりません。対角線移動の衝突検出
以下のコードは、プレーヤを扱うクラスからのものである:
public void run()
{
running = true;
while(running)
{
//Get the key/s pressed
controls.update();
//Move down
if (controls.down)
{
movePlayerDown(this.thisPlayer.getSpeed());
}
//Move up
if (controls.up)
{
movePlayerUp(this.thisPlayer.getSpeed());
}
//Move right
if (controls.right)
{
movePlayerRight(this.thisPlayer.getSpeed());
}
//Move left
else if (controls.left)
{
movePlayerLeft(this.thisPlayer.getSpeed());
}
try
{
Thread.sleep(this.sleep);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public void movePlayerRight(int dx)
{
//Gets the tile at what would be the new coordinates of the player
Tile tile = currentLevel.getTile(this.thisPlayer.getX() + dx + this.thisPlayer.getWidth(), this.thisPlayer.getY());
//IF the tile is null or not solid then the player is actually moved
if (tile == null || !tile.isSolid())
{
this.thisPlayer.setX(this.thisPlayer.getX() + dx);
}
}
public void movePlayerLeft(int dx)
{
//Gets the tile at what would be the new coordinates of the player
Tile tile = currentLevel.getTile(this.thisPlayer.getX() - dx, this.thisPlayer.getY());
//IF the tile is null or not solid then the player is actually moved
if (tile == null || !tile.isSolid())
{
this.thisPlayer.setX(this.thisPlayer.getX() - dx);
}
}
public void movePlayerDown(int dy)
{
//Gets the tile at what would be the new coordinates of the player
Tile tile = currentLevel.getTile(this.thisPlayer.getX(), this.thisPlayer.getY() + dy + this.thisPlayer.getHeight());
//IF the tile is null or not solid then the player is actually moved
if (tile == null || !tile.isSolid())
{
this.thisPlayer.setY(this.thisPlayer.getY() + dy);
}
}
public void movePlayerUp(int dy)
{
//Gets the tile at what would be the new coordinates of the player
Tile tile = currentLevel.getTile(this.thisPlayer.getX(), this.thisPlayer.getY() - dy);
//IF the tile is null or not solid then the player is actually moved
if (tile == null || !tile.isSolid())
{
this.thisPlayer.setY(this.thisPlayer.getY() - dy);
}
}
コードの次のブロックがgetTile()メソッドである:
public Tile getTile(int x, int y)
{
for (int r = 0; r < worldTiles.length; r++)
{
for (int c = 0; c < worldTiles[r].length; c++)
{
if (worldTiles[r][c] != null)
{
int right = worldTiles[r][c].getX() + worldTiles[r][c].getWidth();
int bottom = worldTiles[r][c].getY() + worldTiles[r][c].getHeight();
if (x > worldTiles[r][c].getX() && x < right && y > worldTiles[r][c].getY() && y < bottom)
{
return worldTiles[r][c];
}
}
}
}
あなたは衝突検出コードを追加するのを忘れていますが、そこに問題があります。投稿してください。また、どのように斜めに動いていますか?上記のコードは、上下左右のコントロールのみを示しています。 – 11thdimension
ユーザーが上キーと右キーを保持している場合、プレーヤーは斜め方向に動きます。 – Hobbs2000
プログラムの観点から見ると、まだ 'up'キーと' right'キーの組み合わせです。速くて斜めに動いているように見えますが、速くてもCPUには関係ありません。つまり、衝突検出コードにエラーがあります。これは、 'up'の後に' right'キーが続くか、またはその逆を扱うことができません。 – 11thdimension