ddming000
ddming000

Reputation: 161

How to use cocos2d's collision_model

Recently, I used cocos2d with Python to make a game. But I had trouble with the collision_model. I read the documentation but I still can't use the CollisionManager to add the CollidableSprite as in the example.

In fact, I had to change the ActorSprite to CollidableSprite to avoid an Error. What could the problem be?

import cocos.euclid as eu
import cocos.collision_model as cm

class CollidableSprite(cocos.sprite.Sprite):
    def __init__(self, image, center_x, center_y, radius):
        super(ActorSprite, self).__init__(image)
        self.position = (center_x, center_y)
        self.cshape = cm.CircleShape(eu.Vector2(center_x, center_y), radius)

class ActorModel(object):
    def __init__(self, cx, cy, radius):
        self.cshape = cm.CircleShape(eu.Vector2(center_x, center_y), radius)

The documentation showed this example initialising the collidable sprite. But where does the ActorSprite come from? I have to change it to CollidableSprite to make the class work. And I state a collision manager to add the sprites. But it returns None when I call the function CollisionManeger.known_objs().

 def __init__(self):
    super(page,self).__init__()
    self.collision_manager = CollisionManager()
    self.collision_manager.add(self.sprite1)
    self.collision_manager.add(self.sprite2)
    print self.collision_manager.known_objs()

So, is there something wrong with the documentation? Or have I misunderstood it somehow?

Upvotes: 3

Views: 738

Answers (1)

user2373682
user2373682

Reputation:

It looks like the example should indeed initialize a CollidableSprite instead of an ActorSprite.

CollisionManager class is just an interface. It doesn't do anything. There are two implementations in collision_model: CollisionManagerBruteForce and CollisionManagerGrid. So you can use for example:

self.collision_manager = CollisionManagerBruteForce()

but note that CollisionManagerGrid is more efficient.

Upvotes: 2

Related Questions