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?
thanks
Kutz
 

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.
russ
 
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):

base.disableMouse()
base.camera.setPos(0,-30,20)
base.camera.setHpr(0,-30,0)

self.fundo = loader.loadModel("models/environment")
self.fundo.reparentTo(render)
self.fundo.setScale(0.25,0.25,0.25)
self.fundo.setPos(-8,42,0)


#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.pickerNode.setFromCollideMask(BitMask32.bit(1))

self.pickerRay = CollisionRay()
self.pickerNode.addSolid(self.pickerRay)

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

#carregando modelo
self.bola = loader.loadModel("models/smiley")
self.bola.reparentTo(render)
self.bola.setPos(0,0,1)

self.jack = loader.loadModel('models/jack')
self.jack.reparentTo(render)
self.jack.setPos(2,0,1)
self.jack.setHpr(180,0,0)

#criando a colisão
self.bolaNode = CollisionNode('bola')
self.bolaNode.addSolid(CollisionSphere(0,0,0,1.1))
self.bolaNP = self.bola.attachNewNode(self.bolaNode)
self.bolaNP.show()


self.bola.node().setIntoCollideMask(BitMask32.bit(1))
self.bola.node().setTag('bolinha','1')

self.jackNode = CollisionNode('jack')
self.jackNode.addSolid(CollisionSphere(0,0,0,1.1))
self.jackNP = self.jack.attachNewNode(self.jackNode)
self.jackNP.show()

self.jack.node().setIntoCollideMask(BitMask32.bit(1))
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
self.picker.traverse(render)
if self.pq.getNumEntries() > 0:
self.pq.sortEntries()
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()
run()
Kutz
 

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.
Guest
 

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

in code, how can I do this?
kutz
 

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.
russ
 
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
kutz
 

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

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

I think this is what you want to achieve.

Have fun!
The_Sandman
 
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.
russ
 
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