problems with picking objects

Return to Scripting Issues

problems with picking objects

Postby Kutz » Tue Oct 04, 2005 2:47 pm

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?

Postby russ » Wed Oct 05, 2005 9:47 am

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.
Posts: 223
Joined: Wed May 04, 2005 5:32 pm

Postby Kutz » Thu Oct 06, 2005 8:34 am

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()

Postby Guest » Thu Oct 06, 2005 11:52 am

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.

Postby kutz » Thu Oct 06, 2005 12:13 pm

in code, how can I do this?

Postby russ » Thu Oct 06, 2005 3:34 pm

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.
Posts: 223
Joined: Wed May 04, 2005 5:32 pm

Postby kutz » Sat Oct 08, 2005 7:36 pm

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

Postby The_Sandman » Sun Oct 09, 2005 3:22 am

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

I think this is what you want to achieve.

Have fun!
Posts: 131
Joined: Sun Jun 05, 2005 2:18 pm

Postby russ » Tue Oct 11, 2005 3:34 pm

Oh, sorry. Right. The picker tutorial should help a lot.
Posts: 223
Joined: Wed May 04, 2005 5:32 pm

Return to Scripting Issues

Who is online

Users browsing this forum: No registered users and 1 guest