Create collision box and test for collision

Create collision box and test for collision

Postby LavaCreeperKing » Thu May 05, 2016 10:21 pm

I need to know how I can make a collision box for my playerclass and then be able to detect collisions with it. I need to be able to know when ever it collides with an object(blockclass) what side collided with the object. So if the top collided with an object I can prevent the player from jumping, if the left or right side collides with an object I can stop it from moving and if the bottom of the player is colliding with and object I can make the game know that the player is on a platform and should not be pulled down by gravity. How do I do this?
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Pleas I need help

Postby LavaCreeperKing » Sun May 08, 2016 5:41 pm

Pleas someone help me. I have tried to post this question on the game development section but there is no activity there and I need help. I need to now how to detect collisions between sprite objects because every thing I try it does not work. Every thing I try results in it detection a collision when the player is not touching the object and I don't under stand what I am doing that makes this happen. Can someone pleas show me how to detect collisions between sprites?
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Re: Create collision box and test for collision

Postby micseydel » Sun May 08, 2016 6:14 pm

This is not the kind of thing you should be starting a new thread for, so I've merged them. Most contributors here read the whole forum, since the volume is small, and if they actively avoid a specialized section it's probably for a good enough reason that muddying other forums isn't going to help (I tend to avoid the GUI and game dev sections since I have no familiarity with the frequently used modules there). You can bump your thread one time after 24 hours, since people may have passed over it thinking others would answer, but generally if it's not getting replies then it's not a good question. For this post, I imagine it's the latter of the two.

Even if you've done so already, re-read the forum rules with particular attention to asking good questions. You show no effort here. There's no code, there's no indication that you've Googled it, and the question itself sounds very Google-able. Adding to the apparent lack of effort and consideration for helping us to help you, you didn't even link to the thread you were saying didn't get a reply. You'll get a ton more out of this forum by taking the time to ask good questions, and briefly putting yourself in the shoes of someone who lacks your context but wants to help.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Create collision box and test for collision

Postby Yoriz » Sun May 08, 2016 7:29 pm

Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Create collision box and test for collision

Postby LavaCreeperKing » Sun May 08, 2016 9:39 pm

Alright here is an example of what I am trying to do.(By the way I am not just going to need to know how to do it for rects but also circles triangles and other shapes I am trying to make a physics game in python.) So in my code I have a class that I can for now define blocks then I put them into a list and check to see if the player collides with them and I have searched on google and every thing that I tried did not work it just prints collided over and over even though the player is not touching any of the blocks.
Code: Select all

class BlockClass(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("block.png")
        self.rect = self.image.get_rect()

class PlayerClass(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("player.png")
        self.rect = self.image.get_rect()

pygame.init()
player = PlayerClass()
obj1 = BlockClass(280, 430)
obj2 = BlockClass(410, 430)
blocks = pygame.sprite.Group()
blocks.add(obj1)
blocks.add(obj2)
)


Code: Select all
while True:
   
    for block in blocks:
        if pygame.sprite.collide_rect(player, block):
            print('collided')
   

I have tried several things like this and it just is not working it is getting a collision even though the player is not toughing a block. And I have tried with only one block
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Re: Create collision box and test for collision

Postby Ofnuts » Mon May 09, 2016 7:21 am

Learn to code with simple, testable units.

1) write a function that takes a rectangle (4 points, or center+corner plus width/height) and determines if a 5th point is inside or outside the rectangle. Test it properly, with a test point in: the eight rectangles that surround your rectangle, plus exactly on the the 4 sides, plus exactly the 8 extensions of the 4 sides, plus of course inside the rectangle and on its 4 corners. Don't go further until you get this one 100% correct.

2) write a function that takes a rectangle as above, a second rectangle, and determines if they touch/intersect, by computing the coordinates of the 4 corners of the 2nd rectangle, and checking if any is inside the first rectangle. If you tested 1) correctly, any incorrect behavior comes from that new code.

3) write a function that takes a list of rectangles plus another rectangle, and determines if that rectangle touches or intersects any in the list, by iterating the list and calling the previous function with one rectangle from the list and the test rectangle.

Note: functions above can of course be methods of the adequate objects.
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Re: Create collision box and test for collision

Postby LavaCreeperKing » Mon May 09, 2016 4:21 pm

I'm sorry, how am I supposed to define these points for the object? I have tried something sorta like this as I show below. It works just fine for if I only have one block(except sometimes if you move just right the player can pass through the block)but when I add another block to the game it does not seem to work properly. Is the code that I have here anything like what you are telling me to do because it sounds like you want me to go into player and block class and do something like self.c1 = [cordx,cordy] self.c1[x,y] ect... Then I would check to see if one point on the player it at the same location as a point on the block. But if the player and block are different sizes or the player collides with the block in between the two block points that is where you have to check to see if the player points are in range of the block points but when I did if (player.c2 in range (obj1.c1,obj1.c3)) it just gave me some error. Pleas check the code below and tell me if that is something like what you are telling me to do. Also pleas don't get mad and say that I did not listen to a word you said. I did read your post and I just want to know if one of the things that I have already tried is or is not what you are telling me to do.

Code: Select all
for block in blocks:
        if (x in range (block.cordx-40,block.cordx+20)) and (y in range(block.cordy,block.cordy+20)):
            #do stuff
        if (x in range (block.cordx,block.cordx+21)) and (y in range(block.cordy,block.cordy+20)):
            #do stuff
        if (x in range (block.cordx-15,block.cordx+20)) and (y in range(block.cordy-40,block.cordy)):
            #do stuff
            check_fall = True
        if check_fall == True:
            if (x not in range (block.cordx-15,block.cordx+20)) and (y in range(block.cordy-40,block.cordy)):
                #do stuff
LavaCreeperKing
 
Posts: 103
Joined: Wed Sep 02, 2015 11:31 pm

Re: Create collision box and test for collision

Postby Ofnuts » Mon May 09, 2016 11:53 pm

I'm telling you to write three functions/methods, where each function uses the previous one. Not an unmanageable mess that tries to do everything from scratch.

A rectangular object is defined by four values, either:
  • (xMin, yMin, xMax,yMax)
  • (x,y,width,height) where x,y are the coordinate of some point (usually, top left corner, but it could also be the center, or whatever makes things simple)
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987


Return to Game Development

Who is online

Users browsing this forum: No registered users and 3 guests