In my project i have an array of Ring objects, that the actor can collide with (to “collect” them).
I’ve managed to collide the actor with the rings and that collision adds an entry to the CollisionHandlerQueue.
But now i must identify the ring so i can delete it when the actor “collects it”.
So, i tried adding a task like this:
self.collTrav.traverse(render)
for i in range(self.queue.getNumEntries()):
entry = self.queue.getEntry(i)
# i have 5 rings at the moment
# this will become dynamic later
for i in range (0, 5):
if self.ring[i].colNode == entry.getIntoNode():
print "Collided with ring " + self.ring[i].index
else:
print "Collided, cant identify"
But all it prints is “Collided, cant identify” when i enter the ring’s collision sphere.
So comparing self.ring[i].colNode with entry.getIntoNode() is wrong.
What should i be comparing?
Thank you in advance.
This is my Ring class:
from direct.showbase.Loader import Loader
from pandac.PandaModules import *
class Ring():
def __init__(self, x, y, index):
self.model = loader.loadModel("assets/models/ring/torus")
self.model.setX(x)
self.model.setY(y)
self.model.setHpr(90, 0, 90)
self.model.setColor(255, 255, 0)
self.model.setScale(1, 1, 1)
self.index = index
self.colSphere = CollisionSphere(0, 0, 0, 1)
self.colNode = self.model.attachNewNode(CollisionNode("ring_" + str(index)))
self.colNode.node().addSolid(self.colSphere)
self.colNode.show()
Yeah, that works flawlessly. I’m now trying to understand the tag approach, as you suggested.
Ok, here you set the tag for the first ring as ‘ring0’.
And now here you are looking for the ‘ring’ tag. Shouldn’t it be ‘ring0’? How does it work?
Since you’re already helping me with this part, i would like to ask you another thing:
Now that i’ve identified the ring i collided into, how can i “remove” it from the game?
I was thinking that i should just remove the collision from it (so it becomes a ghost) and taking it off from render (so it doesn’t even show up).
By the way, sometimes when the character is colliding, because the character moves really fast, the collision is not even detected (at least not printed in the console).
My collision test:
def collisionTest(self, task):
self.collTrav.traverse(render)
for i in range(self.queue.getNumEntries()):
entry = self.queue.getEntry(i)
# Rings
tagStr=entry.getIntoNodePath().getTag("ring")
if tagStr != "":
print "Collided with ring " + tagStr
# todo: code to delete the ring
# todo: score++
return task.cont