2
\$\begingroup\$

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;
    }



}
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

The problem comes from the fact that you do your collision detection before moving. Just move the line this.detectarColisoes(...); to the end of the actualizar() method and that will fix the problem.

Please note that naming your variables in english is a good practice especially if you want to get help from this site, as reading portuguese names is not helping most of us.

\$\endgroup\$
5
  • \$\begingroup\$ after i did it it keeps happening \$\endgroup\$
    – Jão
    Commented Jan 23, 2017 at 16:36
  • \$\begingroup\$ You should make a few changes in your code to make it work, but the process should be the following : 1 - Process the moves in X and Y directions, 2 - Check if there is a collision and change X and Y accordingly \$\endgroup\$ Commented Jan 23, 2017 at 16:47
  • \$\begingroup\$ Olivetti invented the first personal computer, unluckily italian is not the defaul language for programs :P \$\endgroup\$ Commented Jan 23, 2017 at 16:53
  • \$\begingroup\$ Italian really? I was thinking spanish or portuguese \$\endgroup\$ Commented Jan 23, 2017 at 16:55
  • \$\begingroup\$ first,i worked a little in my code and now its working,thanks. second,was portuguese . \$\endgroup\$
    – Jão
    Commented Jan 24, 2017 at 21:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .