problems with picking objects


i have a problem:

A and B are objects with collision sphere. I pick A, and when A overlap B, B is picked and A stop moving. the same happends when B overlap A. the object that I´m trying to move don´t lock in the mouse pointer.
where is the problem?


You should post some code so people will have a better sense of what you are trying to do and to see where the problem might be.


here is my code

import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
from direct.gui.DirectGui import *

#This is derived from the mathmatical of a plane, solved for a given point
def PointAtZ(z, point, vec):
return point + vec * ((z-point.getZ()) / vec.getZ())

class World (DirectObject):
def init(self):


self.fundo = loader.loadModel("models/environment")

#criando as colisoes necessárias
self.picker = CollisionTraverser()
self.pq = CollisionHandlerQueue()
#criando colisoes do raio do mouse
self.pickerNode = CollisionNode('mouseray')
self.pickerNP = camera.attachNewNode(self.pickerNode)

self.pickerRay = CollisionRay()

#adicionando traverser
self.picker.addCollider(self.pickerNode, self.pq)

#carregando modelo
self.bola = loader.loadModel("models/smiley")

self.jack = loader.loadModel('models/jack')

#criando a colisão
self.bolaNode = CollisionNode('bola')
self.bolaNP = self.bola.attachNewNode(self.bolaNode)


self.jackNode = CollisionNode('jack')
self.jackNP = self.jack.attachNewNode(self.jackNode)

self.jack.node().setTag('bolinha', '2')

#ver se ja tem algo clicado
self.dragging = False

self.pickedObj = False

#criando task e handlers
self.mouseTask = taskMgr.add(self.mouseTask, 'mouseTask')
self.accept("escape",    sys.exit)
self.accept("mouse1",    self.pega)
self.accept("mouse1-up", self.solta)

#fim do init

def mouseTask(self,task):
self.pickedObj = False

#ver se o mouse está disponível
if base.mouseWatcherNode.hasMouse():
mpos = base.mouseWatcherNode.getMouse()
#dar a posição do raio de colisao de acordo com a posição do mouse
self.pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY())

#mudar a posição do objeto clicado
if self.dragging is not False:        
  nearPoint = render.getRelativePoint(camera, self.pickerRay.getOrigin())
  nearVec = render.getRelativeVector(camera, self.pickerRay.getDirection())
  self.i.setPos(PointAtZ(.9, nearPoint, nearVec))

#fazer a colisao
if self.pq.getNumEntries() > 0:
  self.pickedObj= self.pq.getEntry(0).getIntoNodePath()

  self.i = self.pickedObj.getParent()	  
return Task.cont

#fim do task

def pega(self):
if (self.pickedObj is not False and self.i):
self.dragging = self.i

def solta(self):
self.dragging = False

w= World()


one way to make sure you can guarantee click and drag behavior is to use some global variable called “self.dragging” or whatever, and assert that variable on a mouse event, and set it false on a mouse-up event. Then only enable a collision if self.dragging = false.

This makes it so you can drag one item at a time.


in code, how can I do this?


I think I understand what you are looking for now. You want to change the object you are dragging when it collides with another object.

What you want to do is create another smaller collision sphere for each object that you are dragging. This new collision sphere will collide with the new collision spheres in other objects, so set the bitmasks for all of the new collison spheres the same. You can use a CollisionHandlerEvent() to send messages with addInPattern() and addOutPattern() . Listen for the inPattern event with self.accept() in your world class. You can get the into node’s name with the “%in” pattern string (look in the manual on how to do this). When the two objects collide, ignore the inPattern, and switch your self.i to the object that was collided into. Wait until the objects are not colliding anymore ( listen for the outPattern that you have created ) and then listen for the inPattern again. Its a little confusing, and there are some details you will need to work out, but I hope it helps.


sorry, but this is not what I want to do, this is the problem that I want to resolve. I want to keep the object that I´m moving, not change the selection.
thanks again for the help and for the pacient
sorry my english becouse I´m brazilian and I don´t speak english very well


kutz, check out the sample located at panda3D-vers/samples/Feature-Tutorials--Picking/

I think this is what you want to achieve.

Have fun!


Oh, sorry. Right. The picker tutorial should help a lot.