Hey guys. I need your help for my SAGL Platformer, I am using the same, working code from Super Doctor Who but in this run it seems to not be running for some reason.
First off, I check sideways collision using this:
protected boolean collidingWithWallRight(){
if(!collidable)
return false;
return game.chunkManager.isBlockAtPixelCollidable(x+(width/2)-1,y+(height)) && game.chunkManager.isBlockAtPixelCollidable(x+(width/2)-1,y-(height/2)) ;
}
protected boolean collidingWithWallLeft(){
if(!collidable)
return false;
return game.chunkManager.isBlockAtPixelCollidable(x-(width/2),y-(height)) && game.chunkManager.isBlockAtPixelCollidable(x-(width/2),y+(height/2)) ;
}
protected boolean collidingWithWall() {
if(!collidable)
return false;
return game.chunkManager.isBlockAtPixelCollidable(x-(width/2)+4,y-(height)) && game.chunkManager.isBlockAtPixelCollidable(x-(width)+4,y+(height/2)) || game.chunkManager.isBlockAtPixelCollidable(x+(width/2)-4,y-(height/2)) && game.chunkManager.isBlockAtPixelCollidable(x+(width/2)-4,y+(height/2)) ;
}
and I get the blocks and if there where they should or shouldnt be by:
public Block getBlockAtPixel(int x, int y) {
try{
x = x/Block.blockSize.width;
y = y/Block.blockSize.height;
final Point cpos = new Point((int)(x/Chunk.xa), (int)(y/Chunk.ya));
final Chunk chunk = chunks[cpos.x][cpos.y];
Point lpos = new Point(x%Chunk.xa, y%Chunk.ya);
return chunk.blocks[lpos.x][lpos.y];
} catch(Exception e){
return null;
}
}
public Boolean blockAtPixelExists(int x, int y){
return getBlockAtPixel(x,y) != null;
}
public Boolean isBlockAtPixelCollidable(int x, int y) {
if(blockAtPixelExists(x,y))
return getBlockAtPixel(x,y).isCollidable();
return false;
}
and then in the entity’s tick I call this to make sure they stay out of walls if they get into one:
if(collidingWithWallRight()){
x-=1;
}
if(collidingWithWallLeft()){
x+=1;
}
and in the Players class I call this to move the player:
if(KeyManager.keyPressed(Key.LEFT) && !collidingWithWall() && !collidingWithWallLeft()){
walk(-walkSpeed);
} else if(KeyManager.keyPressed(Key.RIGHT) && !collidingWithWall() && !collidingWithWallRight()){
walk(walkSpeed);
}
The walk method contains no collision detection.
Its so weird. I can sometimes walk through walls and sometimes I get stuck and sometimes I cant walk through walls. I can never walk through walls that are 2 blocks high, otherwise I can most of the time.
Anyone see something odd and can point me in the correct direction?