Collision detection problem again

I know there are at least 3 posts about this subject each week, but I after readind the mannual and searching the forum, I didn’t find anything that could help me.

I wrote this class bellow after reading a some examples.

import direct.directbase.DirectStart
from direct.task import Task
from direct.showbase.DirectObject import DirectObject
from direct.actor.Actor import Actor
from pandac.PandaModules import *
import sys, math


class Game(DirectObject):
    def __init__(self):
        self.setupGame()
        self.loadModel()
        self.setupKeyboard()
    
    def setupGame(self):
        base.disableMouse()
        base.camera.setPos(0, -5, 0)
        self.prevtime = 0
        self.isMoving = False
        self.staticPose = 13
        
    def loadModel(self):
        self.env = loader.loadModel("environment")
        self.env.reparentTo(render)
        self.env.setScale(0.25,0.25,0.25)
        self.env.setPos(0, 20, 0)

        self.actor = Actor("panda", {"walk":"panda-walk"})
        self.actor.setScale(.5, .5, .5)
        self.actor.reparentTo(render)
        self.actor.setPos(0, 20, 0)
        self.actor.pose("walk", self.staticPose)

        self.actor2 = Actor("panda", {"walk":"panda-walk"})
        self.actor2.setScale(.3, .3, .3)
        self.actor2.reparentTo(render)
        self.actor2.setPos(-5, 20, 0)
        self.actor2.pose("walk", self.staticPose)
                
        ###### Collision
        ct = CollisionTube(0, 0, 9, 0, 0, 3, 5.3)
        self.ctNode = self.actor.attachNewNode(CollisionNode("collisionNode"))
        self.ctNode.node().addSolid(ct)
        self.ctNode.show()

        ct2 = CollisionTube(0, 0, 8, 0, 0, 3, 5)
        self.ctNode2 = self.actor2.attachNewNode(CollisionNode("collisionNode2"))
        self.ctNode2.node().addSolid(ct2)
        self.ctNode2.show()

        self.pusher = CollisionHandlerPusher()
        self.pusher.addCollider(self.ctNode, self.actor)
 
        self.traverser = CollisionTraverser("trav")
        base.cTrav = self.traverser
        self.traverser.addCollider(self.ctNode, self.pusher)

 
        base.camera.setPos(self.actor.getX(), self.actor.getY() + 100, 10)
        base.camera.lookAt(self.actor)
        
    def setupKeyboard(self):
        self.keyMap = {"left":0, "right":0, "forward":0, "back":0, "clock":0, "aclock":0} 
        
        self.accept("escape", self.clean)
        
        self.accept("a", self.setKey, ["left",1])
        self.accept("d", self.setKey, ["right",1])
        self.accept("w", self.setKey, ["forward",1])
        self.accept("s", self.setKey, ["back",1])
        self.accept("q", self.setKey, ["aclock",1])
        self.accept("e", self.setKey, ["clock",1])
        
        self.accept("a-up", self.setKey, ["left",0])
        self.accept("d-up", self.setKey, ["right",0])
        self.accept("w-up", self.setKey, ["forward",0])
        self.accept("s-up", self.setKey, ["back",0])
        self.accept("q-up", self.setKey, ["aclock",0])
        self.accept("e-up", self.setKey, ["clock",0])

        taskMgr.add(self.move,"moveTask") 
        
    def setKey(self, key, value):
        self.keyMap[key] = value
    
    def clean(self):
        sys.exit()
    
    def move(self, task):
        elapsed = task.time - self.prevtime

        if (self.keyMap["left"] != 0):
            self.actor.setH(self.actor.getH() + elapsed * 300)
        
        if (self.keyMap["right"] != 0):
            self.actor.setH(self.actor.getH() - elapsed * 300)
        
        if (self.keyMap["forward"] != 0):
            self.actor.setY(self.actor, - elapsed * 25)
        
        if (self.keyMap["back"] != 0):
            self.actor.setY(self.actor, + elapsed*25)
        
        if (self.keyMap["aclock"] != 0):
            self.actor.setR(self.actor.getR() + elapsed * 100)
        
        if (self.keyMap["clock"] != 0):
            self.actor.setR(self.actor.getR() - elapsed * 100)

        if (self.keyMap["forward"] != 0) or (self.keyMap["left"] != 0) or (self.keyMap["right"] != 0) or (self.keyMap["back"] != 0):
            if self.isMoving is False:
                self.actor.loop("walk")
                self.isMoving = True
        else:
            if self.isMoving:
                self.actor.stop()
                self.actor.pose("walk", self.staticPose)
                self.isMoving = False


        if ((self.keyMap["aclock"] == 0) and (self.keyMap["clock"] == 0)):
            if (math.fabs(self.actor.getR()) <= 1 and math.fabs(self.actor.getR()) >= 0):
                self.actor.setR(0)
            elif (self.actor.getR() > 1):
                self.actor.setR(self.actor.getR() - 1)
            elif (self.actor.getR() < 1):
                self.actor.setR(self.actor.getR() + 1)

        base.camera.lookAt(self.actor)
        camvec = self.actor.getPos() - base.camera.getPos()
        camvec.setZ(0)
        camdist = camvec.length()
        camvec.normalize()
        
        if (camdist > 10.0):
            base.camera.setPos(base.camera.getPos() + camvec * (camdist - 35))
            camdist = 10.0
        if (camdist < 5.0):
            base.camera.setPos(base.camera.getPos() - camvec * (35 - camdist))
            camdist = 5.0

        self.prevtime = task.time
        return Task.cont 

game = Game()

run()

Collision is not working. The actor node doesn’t collide with the actor2 node. Can anyone tell me what I am doing wrong, or point to some post or page in the mannual that can help me?

Did you notice this error message in your program’s output?

And this quote in the manual:

Try using CollisionSpheres instead. :slight_smile:

David

I did not notice the error. I tried the sphere and everything was ok. I just thought the tube was the best geometry since the model had this kind of simmetry. Thanks David