Right place to put collision detection logic

Started by
0 comments, last by boogyman19946 13 years, 9 months ago
I have a method say
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!
Advertisement
I would put the collision detection in the moving functions of the w/e.

And I would make the player move itself. After all, the motion applies to the player and if you're calling the collision detection from within the player's moving function, it's ultimately that function that determines where the player will be positioned.

Just my 2 cents >.>

Yo dawg, don't even trip.

This topic is closed to new replies.

Advertisement