Jump to content
  • Advertisement
Sign in to follow this  

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.

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) {

if (a1 collided on bottom of a2) {

if (a1 collided on left of a2) {

if (a1 collided on right of 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;


Share this post

Link to post
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 >.>

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.

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!