Public Group

# Right place to put collision detection logic

This topic is 3077 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

##### Share on other sites
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 >.>

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633660
• Total Posts
3013221
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!