Collision detection, and picking

Hi all,

I have been going through the collision detection and picking segment of the tutorials. Must the collision traveser be self.cTrav and must the picker be self.picker all the time?

[color=green]Yep. That it would. We’ve been having some troubles with them for now, but those are the only things you can use. ^,^;

Actually, no, that’s not true. You can call the collision traverser anything you like. By convention, there is one collision traverser that is run automatically, and that is the one that you store in base.cTrav. If you don’t want to store your collision traverser in base.cTrav, then you have to start your own task to run it every frame. The documentation in the manual, specifically the “collision traversers” page, talks about this in more detail.

To use the Picker class, you have to create an instance of the Picker class. It doesn’t matter what name you call this instance. It can be self.picker, or self.dooby, or self.dingleberry if you prefer. You can have as many Picker instances as you like.

David

[color=green]Well that could be one reason we are having some problems with collision detecting.

Ok, due to the fact that this seems the most recent thread with the picking thingie, here my 2 cents/big problem.

What do I intend? Create a surface where avatars can move using a lerp from their local position to a position I give them by clicking with the mouse at the desired position. I don’t want to pick the whole surface. I just want to provide the position I get from the cursor over to the lerp (what I am going to implement later. For now I would be happy with printing this out)

Whats the problem? After mixing 2 collision tutorials and “Cloaks” help in the IRC Channel I got this whole thing partial working. However, I never manage to keep getting constantly coordinates - or coords at all.

So maybe one of you could be as kind and take a look into this where I made my mistake?
Anyways, here is the code.

import direct.directbase.DirectStart
from direct.showbase import DirectObject
from pandac.PandaModules import *


class Picker(DirectObject.DirectObject):
   def __init__(self, intoNodePath):
      self.position=None
      base.disableMouse()
      self.intoNodePath=intoNodePath
      self.picker= CollisionTraverser()
      self.queue=CollisionHandlerQueue()
      self.pickerNode=CollisionNode('mouseRay')
      self.pickerNP=camera.attachNewNode(self.pickerNode)
      self.pickerNode.setFromCollideMask(GeomNode.getDefaultCollideMask())
      self.pickerRay=CollisionRay()
      self.pickerNode.addSolid(self.pickerRay)
      self.picker.addCollider(self.pickerNode, self.queue)
      self.accept('mouse1', self.printMe)

   def getObjectHit(self, mpos): #mpos is the position of the mouse on the screen
      self.pickerRay.setFromLens(base.camNode, mpos.getX(),mpos.getY())
      self.picker.traverse(render)
      if self.queue.getNumEntries() > 0:
         self.queue.sortEntries()
         self.position=self.queue.getEntry(0).getSurfacePoint(self.intoNodePath)
         #print 'Position of the surface clicked: ',self.pos
      return None

   def getPickedObj(self):
         return self.pickedObj

   def printMe(self):
         self.getObjectHit( base.mouseWatcherNode.getMouse())
         print self.position


cm = CardMaker('Card')
card = render.attachNewNode(cm.generate())
myTex=loader.loadTexture("models/maps/smiley.rgb")
card.setTexture(myTex)
card.setHpr(0,-90,0)
card.setPos(0,0,0)
card.setScale(100,100,100)

base.cam.setPos(10,-2,2)

mousePicker=Picker(card)

run()

Help REALLY appreciated… I am starting giving up on this - really.
Thanks in advance,

Bigfoot29

Your problem is here:

Instead of that, do this instead:

This is because you parent your picker to the node ‘camera’, not to the node ‘base.cam’. When you move base.cam, you are moving it independently of camera, so the picker no longer lines up to where your camera is looking.

In fact, you should never move base.cam independently of camera anyway. The idea is supposed to be that base.cam is always in the same space as camera, unless you call base.oobe().

David

:open_mouth: You are right… that fixed it. Its working perfect now! :smiley:

Thanks & regards, Bigfoot29