I can’t wrap a sphere around my object, my code below:
self.object = loader.loadModel('models/ball.egg')
self.bounds = self.object.getBounds()
self.center = self.bounds.getCenter()
self.radius = self.bounds.getRadius()*1
self.cNode = CollisionNode( 'cellsphere' )
self.cNode.addSolid( CollisionSphere(self.center, self.radius) )
self.cellCollider = self.object.attachNewNode(self.cNode)
Well, what happens when you run the above code? It looks fine to me.
Note that it might be hard to see a sphere that has exactly the same radius as your ball.
radius isn’t my problem actually, the sphere that is a bit to the left, if I change self.object pos X to 0 it’s ok, if I change to 1 the sphere goes away to the right. That’s weird, don’t know what is happening.
if self.object X is at -0.1 self.center returns Point3(-1.348, 0.779, 0.171) the sphere is a bit to the left
if self.object X is at 0 self.center returns Point3(-1.248, 0.779, 0.171), ok! the sphere is exactly in center of the object
if self.object X is at 1 self.center returns Point3(-0.248, 0.779, 0.171) sphere is away to the right
Sounds to me like the origin of the model isn’t right. Did you make the model yourself?
I did, where is the origin of the model supposed to be?
The problem is using bounds.getCenter(). The bounding volume is in the space of the object’s parent, not the object itself, so it includes the offset you’ve applied to the object. When you then create your BoundingSphere with this offset, you’ve applied the same offset to the sphere; but then you attach the sphere as a child of your object, which applies the offset again. So your offset gets applied twice.
There are lots of ways to work around this. One easy thing to do is to simply move the object to (0, 0, 0) before you call getBounds(). Then you can move the object wherever you want afterwards.
But I am confused yet.
self.ralph = Actor("models/ralph",
self.avatarCollision = CollisionNode('avatarSphere')
self.avatarCollision.addSolid( CollisionSphere(0,0,-0.05, 0.9) )
self.sphereAvatar = self.ralph.attachNewNode(self.avatarCollision)
That code puts the sphere at the right place for the actor and why not if I type loader.loadModel() instead Actor? For loader.loadModel(), the sphere is at center of the screen and not at the center of the model.
the problem is solved, the center of my model wasn’t right!