void computeCollision(Actor a1, Actor a2) { //determine if a1 and a2 are colliding if (a1 collided on top of a2) { a2.collideTop(a1); a1.collideBottom(a2); } if (a1 collided on bottom of a2) { a1.collideTop(a2); a2.collideBottom(a1); } if (a1 collided on left of a2) { a2.collideLeft(a1); a1.collideRight(a2); } if (a1 collided on right of a2) { a2.collideRight(a1); a1.collideLeft(a2); }}
Now if a1 is the Player and a2 is a Block, when Player collides with Block, the Player will be moved outside of the Block. My question is, where do I put the logic in it? Assume Block and Player extends the Actor class. PositionY is located in the center of the rectangular actor.
Does the Block push away the Player like this?
//method in Blockvoid collideTop(Actor a) { //move all actors colliding with me to the nearest top a.setPositionY(myPositionY + myHeight + a.getPositionY());}
or does the Player know where to place itself?
//method in Playervoid collideBottom(Actor a) { //i know that i collided on something, i need to move myself myPositionY = a.getPositionY() + a.getHeight() + myHeight / 2;}
Thanks!