3

So I've been working on a game and the biggest problem so far is that we cant get the player to collide with any of the objects on the field. Instead they go right through the tree. Can anyone tell me why? Here is the code I tried using for collision detection:

for tree in treelist:


    if self.player.rect.x == tree.rect.x:
        self.player.rect.x == self.player.rect.x - 2
    if self.player.rect.x == tree.rect.x + tree.rect.width:
        self.player.rect.x == self.player.rect.x + 2

    if self.player.rect.y == tree.rect.y:
        self.player.rect.y == self.player.rect.y - 2
    if self.player.rect.y == tree.rect.y + tree.rect.height:
        self.player.rect.y == self.player.rect.y + 2

Is this sound in theory or am I going about it all wrong?

Artemis
  • 2,553
  • 7
  • 21
  • 36
Logan Henry
  • 63
  • 1
  • 10
  • Here are a complete [top-down view example](https://stackoverflow.com/a/45017561/6220679) and a basic [platformer](https://stackoverflow.com/a/48069743/6220679). – skrx May 31 '18 at 08:29

2 Answers2

3

Are you using pygame? If so, there is the built in colliderect function (documentation here).

If not:

  • Here's a relatively in depth discussion, designed with platform games in mind.
  • This is just the barebones 2-d collision detection for axis aligned (i.e. not rotated) rectangles

Keep in mind that objects moving very quickly can make collision detection very difficult, as they move "past" the object within the span of one frame! There are more advanced techniques to deal with this, but for now, try to make your collision boxes relatively large, and things move relatively slow.

Edit: Oops, my first sentence sounds rude. I just somehow forgot what the title of this post was...

nchen24
  • 502
  • 2
  • 9
  • Thank you! The main problem we had when we tried to use collidedetect was that we couldent get the player to react appropriately. Because the game is top down we could never get the player to move back correctly – Logan Henry Dec 11 '14 at 00:25
  • Move back = move down on the screen? Or move after a collision? – nchen24 Dec 11 '14 at 01:05
  • It's a top down game. Whenever the player runs into a tree it goes right through the tree. We could detect the collision but when we did we couldent get the player to 'bounce back' from the tree – Logan Henry Dec 11 '14 at 01:11
  • It's probably best to have multiple collision boxes. This is almost always necessary for platform games, as you want the player to stop horizontal movement when colliding with a wall, but obviously not when their "feet" touch the ground. When you say bounce, do you actually mean that he should bounce off? This kind of behavior is a little tricky, since you need to establish a few frames of "bouncing back," it's not enough to only test the one frame of collision. – nchen24 Dec 11 '14 at 01:55
2

Almost sound theory. You are only checking if the player rectangle position is exactly the same as the boundary of the tree rectangle position. That will rarely be the case. You need to check for the player being in between the tree's boundaries as well, more like this:

# check if player is overlapping the tree
if tree.rect.x <= self.player.rect.x <= tree.rect.x + tree.rect.width:
    # decide on how to move player away from the tree depending on previous frame
    # if player is moving forward, send them backwards from tree
    if self.player.rect.prev_x < self.player.rect.x:
        self.player.rect.x -= 2
    # otherwise send them forwards from tree
    else:
        self.player.rect.x += 2

The exact logic of the overlap check and the reaction if overlapped is completely up to you, but this sort of thing should work. Note that you will need to keep track of the player's position in the previous frame otherwise you won't know which direction they hit the tree from. You can do so like this:

self.player.rect.prev_x = self.player.rect.x
self.player.rect.x += player_velocity
101
  • 8,514
  • 6
  • 43
  • 69