Hey Leute,
ich bin grade dabei, ein kleines Spiel zu programmieren, habe jetzt aber ein Problem bei der Kollisionsabfrage des Spielers mit seiner Umgebung. Der Spieler glitcht nämlich manchmal in die Blöcke des Bodens, sodass noch genau der oberste Pixel des Spielers nicht im Boden ist. Sonst funktioniert die Kollisionserkennung fehlerfrei. Hier der Code der Kollisionsabfrage:
Und hier noch eine Methode, die ich aufrufe:
LG Triton171
ich bin grade dabei, ein kleines Spiel zu programmieren, habe jetzt aber ein Problem bei der Kollisionsabfrage des Spielers mit seiner Umgebung. Der Spieler glitcht nämlich manchmal in die Blöcke des Bodens, sodass noch genau der oberste Pixel des Spielers nicht im Boden ist. Sonst funktioniert die Kollisionserkennung fehlerfrei. Hier der Code der Kollisionsabfrage:
Java:
//Returns true if the player has intersects with any block
public synchronized boolean hasIntersectsWithBlock(){
//The player's hitBox
Rectangle hitBox = this.hitBox;
//The block the player's position (top left corner) is in
Point playerBlock = manager.getBlockPositionInGame( (int) getPosX(), (int) getPosY());
//HitBox for any block (size is always the same, position can be changed for every block
Rectangle blockHitBox = new Rectangle();
//Adjust the size of the block hitBox
blockHitBox.setSize(BlockInfo.SIZE, BlockInfo.SIZE);
//Loop through all the blocks which have to be checked for intersects
for(int i = (int) playerBlock.getX(); i < (int) playerBlock.getX()+Player.numberOfIntersectBlockChecksX; i++){
for(int k = (int) playerBlock.getY(); k < (int) playerBlock.getY()+Player.numberOfIntersectBlockChecksY; k++){
//Check if the block is not air (ID 0=air)
if(manager.level.blockIDs[i][k]!=0){
//Adjust the location of the block hitbox
blockHitBox.setLocation(i*BlockInfo.SIZE, k*BlockInfo.SIZE);
//Check for intersects
if(hitBox.intersects(blockHitBox)) return true;
}
}
}
return false;
}
Und hier noch eine Methode, die ich aufrufe:
Code:
//Returns the coordinates of the block in which the given virtual point in the game is in
public Point getBlockPositionInGame(int posX, int posY){
int blockPosX = (int) posX/BlockInfo.SIZE;
int blockPosY = (int) posY/BlockInfo.SIZE;
return new Point(blockPosX, blockPosY);
}
LG Triton171