Pygame Platformer Collision Detection

Pygame Platformer Collision Detection

Postby ChristianCareaga » Wed Dec 04, 2013 1:32 am

Okay, so i have been trying to get the collision detection for my platformer working I think I'm close but cant quite get it how i want it

here is my code for collision:

Code: Select all
for block in self.game.solid_blocks:
                if self.rect.colliderect(block):
           
                    self.bottomcheck = self.rect.bottom >= block.rect.top and self.rect.bottom <= block.rect.bottom and self.checkCollision()
                    self.rightcheck = self.rect.right >= block.rect.left and self.rect.right <= block.rect.right and self.rect.bottom >= block.rect.bottom and self.rect.top <= block.rect.top and self.checkCollision()
                    self.leftcheck = self.rect.left <= block.rect.right and self.rect.right >= block.rect.right and self.rect.bottom >= block.rect.bottom and self.rect.top <= block.rect.top and self.checkCollision()
                    self.topcheck = self.rect.top <= block.rect.bottom and self.rect.top >= block.rect.top and self.checkCollision()

                    if self.topcheck:
                        self.jumping = False
                        self.touch = True
                        self.rect = last

                    if self.bottomcheck:
                        self.rect.bottom = block.rect.top+1
                        self.touch = False
                        self.jumping = False

                    if self.rightcheck and not self.bottomcheck:
                        self.touch = True
                        self.rect = last
 
                    if self.leftcheck and not self.bottomcheck:
                        self.touch = True
                        self.rect = last


the player is stopping when it hits the sides and floor and walls but I'm having a problem with collision while jumping

when the player jumps onto the wall he stops and falls and that works fine but when he is jumping and going left or right and he touches the top he stops and falls just like on the walls, this is because i made it so that the x axis can only change if self.touch is false, if i make self.touch = True when he hits the ceiling then it works but them he can stick to walls

if you want to test it or look at the rest of the code:
https://github.com/C-Dubb/Flatformer

Please help I've been stuck for awhile
ChristianCareaga
 
Posts: 52
Joined: Sat Jun 22, 2013 9:54 am

Re: Pygame Platformer Collision Detection

Postby Mekire » Wed Dec 04, 2013 10:18 am

I tried to work with your collision code above but ended up ditching it and inserting my own platformer collision code.

One main thing I want to say is... organization. You have too many functions doing way too many things. Your player update function should be split up completely. Have a function that figures out what the x and y velocity of the player are. Have a function that figures out the player direction. Function(s) that do collision. Don't try to throw them all together in one monster update function.

Also, I see you are trying to use a time delta to update your actors. The purpose of this technique is to maintain constant speed gameplay despite frame rate. Every actor must use this delta when moving. If some use it and others don't it defeats the point.

Anyway, other than the collision I tried to stay true to your code. There is honestly a lot I would still change.

Anyway, take a look here:
https://github.com/Mekire/Flatformer

-Mek
User avatar
Mekire
 
Posts: 1009
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Pygame Platformer Collision Detection

Postby ChristianCareaga » Wed Dec 04, 2013 3:13 pm

Mekire wrote:I tried to work with your collision code above but ended up ditching it and inserting my own platformer collision code.

One main thing I want to say is... organization. You have too many functions doing way too many things. Your player update function should be split up completely. Have a function that figures out what the x and y velocity of the player are. Have a function that figures out the player direction. Function(s) that do collision. Don't try to throw them all together in one monster update function.

Also, I see you are trying to use a time delta to update your actors. The purpose of this technique is to maintain constant speed gameplay despite frame rate. Every actor must use this delta when moving. If some use it and others don't it defeats the point.

Anyway, other than the collision I tried to stay true to your code. There is honestly a lot I would still change.

Anyway, take a look here:
https://github.com/Mekire/Flatformer

-Mek


Yeah my update method got a little out of hand, thanks for the help Mek! do you think my code looks better than it used too?
ChristianCareaga
 
Posts: 52
Joined: Sat Jun 22, 2013 9:54 am

Re: Pygame Platformer Collision Detection

Postby Mekire » Wed Dec 04, 2013 3:43 pm

It looks like you have come quite a way since your Star Dodger game. What happened to the rpg project you and a friend were working on?

-Mek
User avatar
Mekire
 
Posts: 1009
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Pygame Platformer Collision Detection

Postby ChristianCareaga » Thu Dec 05, 2013 2:04 am

Mekire wrote:It looks like you have come quite a way since your Star Dodger game. What happened to the rpg project you and a friend were working on?

-Mek


We have been really busy with school, so we are doing some smaller projects atm, We will probably start again in the summer!
ChristianCareaga
 
Posts: 52
Joined: Sat Jun 22, 2013 9:54 am


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests