What kind of objects can I calculate collisions on?

9 views
Skip to first unread message

Alejandro Castellanos

unread,
Jun 30, 2011, 3:28:44 AM6/30/11
to Rabbyt
I have a problem in that I'm trying to use Rabbyt to calculate aab
collisions for different objects but so far, I can only seem to be
able to get it done using rabbyt objects (sprites). I searched for a
bit and I found a discussion title "only check a certain part of a
sprite for collisions " where Matthew Marshall wrote the following
thing:

"Another option: You aren't limited to passing sprites to the
collision
function. Instead of passing your character sprite, create any object
with top, left, bottom, right attributes that correspond to the shape
you want. "

So I did the creating of the object using a generic object class that
extracts the top, left, bottom, right attributes from common a pyglet
sprite. I used the following code for the class:

class OB(object):
def __init__(self, sprite):
self.left = (sprite.x-sprite.width/2, sprite.y+sprite.height/
2)
self.top = (sprite.x+sprite.width/2, sprite.y+sprite.height/2)
self.right = (sprite.x+sprite.width/2, sprite.y-sprite.height/
2)
self.bottom = (sprite.x-sprite.width/2, sprite.y-sprite.height/
2)

And to define the object I used:
OB = OB(pysq); where 'OB' is an object and 'psyq' is the pyglet
sprite.

The following function was called at every iteration of the program to
calculate whether or not the collisions happened:


def aab_collision_tst(single, lista):
if rabbyt.collisions.aabb_collide_single(single, lista):
print "COLLIDE"
else:
print "NOCOLLIDE"

in the form of this line:
aab_collision_tst(esprite, colist)

where 'colist = [Square1,Square2,OB]'; where 'esprite', 'Square1', and
'Square2' are a rabbyt sprites.

Sadly, it is not working at all except when I take out 'OB' from
colist and work with rabbyt objects only.

Does anyone have any thoughts or suggestions?


Matthew Marshall

unread,
Jun 30, 2011, 8:49:12 PM6/30/11
to rab...@googlegroups.com
You're close :-)

In your OB class you're using tuples for each attribute. The
attributes are expected to be numbers:

class OB(object):
def __init__(self, sprite):

self.left = sprite.x-sprite.width/2
self.top = sprite.y+sprite.height/2
self.right = sprite.x+sprite.width/2
self.bottom = sprite.y-sprite.height/2

That should work.

(BTW, if you haven't noticed, sprites have left, top, etc. attributes as well.)

Hope that helps :-)

MWM

> --
> You received this message because you are subscribed to the Google Groups "Rabbyt" group.
> To post to this group, send email to rab...@googlegroups.com.
> To unsubscribe from this group, send email to rabbyt+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rabbyt?hl=en.
>
>

Alejandro Castellanos

unread,
Jul 1, 2011, 9:06:11 AM7/1/11
to Rabbyt
Man, I feel like such a dummy...

I guess I misunderstood the documentation when I saw how the
attributes of the Quad primitive were given in things I misunderstood
to be tuples to represent points.

I literally had to just erase stuff like you showed me and make sure
the x,y coordinates of my object were at the 'center' of it.

Thank you very much, you really got me out of a jam.

My intent was to sort of mix data from both Rabby and Pyglet sprites,
as I intended on having stuff that moves around (Rabbyt sprites), and
general map-stuff for a static background (batched Pyglet sprites) and
that way I can already work with the pre-existing objects I already
have. I was already working with my own aab collisions (probably quite
crappy) but I imagined that it would be quite a mess if I wanted to
calculate lots of 'em later on in the long run if the scene got too
hectic. Now I can go back to getting my tile engine working properly,
this time without much python overhead :-) It may be a bit sad to
ditch a bunch of the collision code I already had, but I can now focus
on other checks, and collision response.

I had already wanted to get started using Rabbyt some time before but
for some reason or another I always kept putting it away, but what
made its worth really sink in was the collision detection.

Again, thanks a bunch!
Reply all
Reply to author
Forward
0 new messages