Jump to content
  • Advertisement
Sign in to follow this  
racumin

Right place to put collision detection logic

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

If you intended to correct an error in the post then please contact us.

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 Block
void 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 Player
void collideBottom(Actor a) {
//i know that i collided on something, i need to move myself
myPositionY = a.getPositionY() + a.getHeight() + myHeight / 2;
}



Thanks!

Share this post


Link to post
Share on other sites
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 >.>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!