I am making a 2d platform game with a tiled map,i now avoid passing through the walls an the ground but when the character hit the ground it shakes vertically,and when i make it walk on a wall it shakes horizontally. the atualization code:
public void atualizar() {
this.detectarColisoes(mapa.getTilesAdjacentes(this.getXAbsoluto(),this.getYAbsoluto(),this.getXAbsoluto()+this.getWidth(),this.getYAbsoluto()+this.getHeight()));
if(movendo){
this.setVelX(direcao*5);
if(this.getVelX()>0 && colisaoLateral==1){
this.setVelX(0);
}
if(this.getVelX()<0 && colisaoLateral==-1){
this.setVelX(0);
}
}else if(this.getVelX()!=0){
if(this.getVelX()>0){
this.setVelX(this.getVelX()-1);
}else{
this.setVelX(this.getVelX()+1);
}
}
if(this.getXRelativo()>490 && this.getVelX()>0){
mapa.moveMapX(this.getVelX());
}
if(this.getXRelativo()<150 &&this.getVelX()<0){
mapa.moveMapX(this.getVelX());
}
if(estadoSalto==2 && this.getVelY()>0){
estadoSalto=1;
}
super.atualizar();
if(estadoSalto!=0){
this.gravidade();
}
System.out.println(estadoSalto);
}
the code for the collision detection:
public void detectarColisoes(ArrayList<Rectangle> adjTiles){
Rectangle chara=new Rectangle(this.getXAbsoluto(),this.getYAbsoluto(),this.getWidth(),this.getHeight());
if(adjTiles.isEmpty()){
if(estadoSalto!=2){
estadoSalto=1;
}
}
boolean caindo=true;
this.colisaoLateral=0;
for(Rectangle aux:adjTiles){
Rectangle ints=aux.intersection(chara);
if(!ints.isEmpty()){
if(ints.height<ints.width){
if(ints.y>chara.y ){
estadoSalto=0;
this.setVelY(0);
this.setY(this.getYAbsoluto()-ints.height);
nSaltos=1;
caindo=false;
}
}else{
if(ints.x>chara.x){
this.setX(this.getXAbsoluto()-ints.width+1);
this.colisaoLateral=1;
}else{
this.setX(this.getXAbsoluto()+ints.width-1);
this.colisaoLateral=-1;
}
}
}
}
if(caindo){
estadoSalto=1;
}
}