My own collision detection... trouble

My own collision detection... trouble

Postby BapeNAsians » Sun Jul 27, 2014 9:49 pm

I'm building pixel collision detection for a small game. The code that is posted test if points of objects are inside an area respresented as the second object.

I have a new concept to test it out, but I was wondering if you could look over this and tell me why its wrong or bad. I noticed if the length of an object is beyond the width of the 2nd object its point is thenout of the area but still overlapsresulting in the software thinking the collision didn't happen.

objA and objB are just pygame.Rects.

Code: Select all
def collide_obj(objA, objB = None, objList = None):
   if objB is not None:
      if objA.w > objB.w:
         objAw = objB.w
         print "in w"
         if objA.h > objB.h:
            objAh = objB.h
            print "in h"
            if objA.top <= objB.top + objB.h and objA.top >= \
            objB.top and objA.left <= objB.left + objB.w and \
            objA.left >= objB.left\
            or\
            objA.top + objAh >= objB.top and objA.top + objAh <= objB.top + objB.h\
            and objA.left + objAw > objB.left and objA.left + objAw <= \
            objB.left + objB.h \
               return True
            else:
               return False


some of the it tests was just to see if it could work. Imagine this code without those if tests if you want to get the gist of what this collision detection is all about. Thanks.

The first few if test was a non working fix that acknowledgedd that some widths or lengths was longer so the point was outside of the region detecting for collisions.
BapeNAsians
 
Posts: 41
Joined: Fri Jan 03, 2014 4:25 am

Re: My own collision detection... trouble

Postby DrakeMagi » Mon Jul 28, 2014 12:33 am

pygame has it own rect collide. http://www.pygame.org/docs/ref/rect.html
Code: Select all
rect.colliderect(anotherrect)

if you want pixel collision. Look up pygame.mask.
http://www.pygame.org/docs/ref/mask.html

simple rect collide code. Still better use built in one.
Code: Select all
def rect_collide(a, b):
    if ( ((a.x >= b.x and a.x < b.x + b.w) or (b.x >= a.x and b.x < a.x + a.w)) and
         ((a.y >= b.y and a.y < b.y + b.h) or (b.y >= a.y and b.y < a.y + a.h)) ):
         return True
    return False
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 109
Joined: Sun May 12, 2013 8:36 pm

Re: My own collision detection... trouble

Postby BapeNAsians » Mon Jul 28, 2014 1:10 am

thanks but that looks like my collision code exactly. I'm not using built in one ...well... I can't think of a reason.
BapeNAsians
 
Posts: 41
Joined: Fri Jan 03, 2014 4:25 am

Re: My own collision detection... trouble

Postby freddyhard » Tue Jul 29, 2014 1:16 pm

at a quick look i think you have some syntax errors. you have
objAw and objAh
when they should be
objA.w and objA.h

otherwise i would recommend using sprite.get_rect() returns a rectangle of the grpahic image sprite. if colliderect() returns True i then used .overlap() function to test for pixel collisions. look at the laser class in Galaxians to see how i set it up.
freddyhard
 
Posts: 36
Joined: Wed Jul 16, 2014 9:32 pm

Re: My own collision detection... trouble

Postby BapeNAsians » Tue Jul 29, 2014 5:32 pm

freddyhard wrote:at a quick look i think you have some syntax errors. you have
objAw and objAh
when they should be
objA.w and objA.h

otherwise i would recommend using sprite.get_rect() returns a rectangle of the grpahic image sprite. if colliderect() returns True i then used .overlap() function to test for pixel collisions. look at the laser class in Galaxians to see how i set it up.


Well for the sprite.get_rect() or even surface.get_rect(), I only use a surface for the screen and pygame.Rects so there isn't a need for obtaining rects for sprites. The reason I use objAh is because I test to see if the length of objA.w is greater than the length of objB.w. If its' greater than at some points in the game it won't work to test for collision because it is longer than objB.w and at the same time won't mean that objA.left is in the rect too, therefore I do some math to make it smaller. Basically if objA is much bigger than objB like the display surface is bigger than a rect of 50, 50.
BapeNAsians
 
Posts: 41
Joined: Fri Jan 03, 2014 4:25 am

Re: My own collision detection... trouble

Postby freddyhard » Tue Jul 29, 2014 7:11 pm

sorry i thought it was a typing error. so objAw and objAh are newly defined variables within the function. why do you not use objB.w and objB.h in the next IF statement instead of the two new variables?

ADDITIONAL:
you could create a rectangle using pygame.Rect() with something like
objA_rect = pygame.Rect(objA.left, objA.top, objA.w, objA.h)
objB_rect = pygame.Rect(objB.left, objB.top, objB.w, objB.h)

then you can use
objA_rect.colliderect(objB_rect)

maybe?
freddyhard
 
Posts: 36
Joined: Wed Jul 16, 2014 9:32 pm

Re: My own collision detection... trouble

Postby BapeNAsians » Tue Jul 29, 2014 7:59 pm

Yeah I'm fully aware of the the pygame.Rect functionality. It just doesn't work how I want it to. I've settled for a more basic approach like DrakeMagi typed in. I will work on it later when I'm in need.

The reason I use those is because I want the length to stay in between objB.x and objB.x + objB.w and same for the height or "y".

If x,y is lower than obj2 x,y and length/width is greater than obj2 x+w and obj2 y+h ... the test becomes useless. Think of a big box rect and a smaller one sitting right on top of it... it won't test properly because the coordinates. But as I'm writing this DrakeMagis solution is what I needed.

I don't know if I'm even explaining it remotely close to what you can understand as I can't express what I"m trying to do lol.
BapeNAsians
 
Posts: 41
Joined: Fri Jan 03, 2014 4:25 am

Re: My own collision detection... trouble

Postby freddyhard » Tue Jul 29, 2014 8:45 pm

in the completed script (chasing game) i submitted i do rectangle collisions with every object to see if they fall within the viewport area.

the viewport area is a rectangle of size 800x560
the people are rectangles of size 50x50
the map (sections) are rectangles of size 1000x1000

for either the people or the map to be drawn they must pass this test

(boxA.right > boxB.left and boxA.left < boxB.right) and (boxA.bottom > boxB.top and boxA.top < boxB.bottom)

where boxB is the viewport and boxA is either the people or the map. so this test works when boxA is smaller or larger than boxB.

this might be of help, but you are right i do not follow what you are trying to do. i am a student so, i probably don't have what it takes to write code.
i'll stop interfering with this thread now - lol
freddyhard
 
Posts: 36
Joined: Wed Jul 16, 2014 9:32 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests