1
\$\begingroup\$

when my character goes onto a box that I am trying to make him collide with he gets stuck. The way I have it configured is that at the end of every frame, the characters x and y values are stored in old x and old y, and if he is colliding with the box his x and y are reverted to old x and old y. This seems to work when walking into the box, but when the character jumps on the box he gets stuck and can't move. I think this has something to do with his x and y being infinitely pushed to oldX and oldY, but I don't know how to fix this. Thanks for the help!

 for(int x = 0; x < collidables.length; x++){
        if(collidables[x] instanceof QuestionBox){
            collidables[x].bottomCollision(mario);
        }


        if(mario.intersects(collidables[x].boxRectangle)){
            mario.setMarioX(mario.oldX);
            mario.setMarioY(mario.oldY);

        }
    }

        mario.oldX = mario.getMarioX();
        mario.oldY = mario.getMarioY();

Here is the code for what happens upon a collision. Thanks for the help!

\$\endgroup\$
1
\$\begingroup\$

Short Answer: you're going to have to treat "falling" and "moving" each as a separate action, each with the ability to be undone separately, instead of combining them into a single undo action.

Long Answer: Lets just simulate it out: o is current position, x is our saved position, _ is a wall, . is empty space.

  ...o.......
  ___________

okay, we're holding down the "move right" button, so lets move Mario right.

  ...x..o....
  ___________

okay, now gravity moves Mario down

  ...x.......
  ______o____

uh wait, Mario's intersecting the wall! lets move back to our old location

  ...o.......
  ___________

Wait... where did my "move right" go?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.