PDA

View Full Version : how can I stop an action?


nlfrnassimi
02-10-2009, 10:26 PM
I've solved the problem of selecting and moving an object but now I can't deselect the object.

when I choose my first object and move it, it works properly but when i select my second object the first one moves also. how can i stop the first one?

this is the code i wrote:

import viz

viz.go()

viz.add('tut_ground.wrl')
viz.clearcolor(0.5,0.5,1)

soccerball1 = viz.add('soccerball.ive')
soccerball2 = viz.add('soccerball.ive')
soccerball3 = viz.add('soccerball.ive')

soccerball1.translate(-1,1.5,3)
soccerball2.translate(0,1.5,3)
soccerball3.translate(1,1.5,3)

arrow = viz.add('arrow.wrl')
arrow.scale(.1,.1,.1)
arrow.visible(viz.OFF)

def mouseclick(button):
if button == viz.MOUSEBUTTON_LEFT:
ball = viz.pick()
if ball.valid():
vizact.whilekeydown( viz.KEY_UP, ball.translate, 0, vizact.elapsed(1), 0, viz.REL_PARENT )
vizact.whilekeydown( viz.KEY_DOWN, ball.translate, 0, vizact.elapsed(-1), 0, viz.REL_PARENT )
vizact.whilekeydown( viz.KEY_RIGHT, ball.translate, vizact.elapsed(1), 0, 0, viz.REL_PARENT )
vizact.whilekeydown( viz.KEY_LEFT, ball.translate, vizact.elapsed(-1), 0, 0, viz.REL_PARENT )

pos = ball.get(viz.POSITION)
pos[1] +=.2
arrow.translate(pos)
arrow.visible(viz.ON)



viz.callback(viz.MOUSEDOWN_EVENT,mouseclick)

viz.mouse(viz.OFF)

moooh
02-11-2009, 01:54 AM
vizact.whilekeydown will be added as events that will listen to the specified keystrokes. When you click the first ball, the events for the first ball will be created. When you click the second ball the events for the second ball will be added to the chain of events that listen to KEY_UP etc. This already includes the events of the first ball since you are creating new events instead of replacing the old.

The ball variable is also in local scope in the mouseclick function so any change you make to it (the assignment of the new ball through ball = viz.pick() for instance) will not affect the global ball variable.

What you should do is move the vizact statements out of the function and replace the second argument with a function of your own. This function could handle the translation for you. This way you'll only have 1 event for each key.


change the mouseclick function to only handle the picking of a ball

def mouseclick(button):
global ball #global reference to the ball
if button == viz.MOUSEBUTTON_LEFT:
pickObj = viz.pick()
if pickObj.valid():
ball = pickObj #assign the current chosen ball to the global variable


and outside of this function add the event registers now with a call to your function instead with the pressed key as the third argument.

vizact.whilekeydown( viz.KEY_UP, myTranslate, viz.KEY_UP )
vizact.whilekeydown( viz.KEY_DOWN, myTranslate, viz.KEY_DOWN )
vizact.whilekeydown( viz.KEY_RIGHT, myTranslate, viz.KEY_RIGHT )
vizact.whilekeydown( viz.KEY_LEFT, myTranslate, viz.KEY_LEFT )


and create the translate function


def myTranslate(key):
global ball
if not ball: #no ball selected, don't try to translate
return
else:
if key == viz.KEY_UP: #up is being pressed
ball.translate #... add the rest of the up translation of the ball here
elif key == viz.KEY_DOWN:
ball.translate # ... add the rest of the down translation of the ball here
elif key == viz.KEY_LEFT:
ball.translate # ... add the rest of the left translation of the ball here
elif key == viz.KEY_RIGHT:
ball.translate # ... add the rest of the right translation of the ball here



This way you will only translate the currently chosen ball as the key events will always call the same myTranslate function regardless of which ball is chosen.

nlfrnassimi
02-11-2009, 05:54 PM
Thanks for your help. Still I'm having problem with giving value to translate. when I assign value to it, the ball suddenly disappears.

Please check the code.

import viz

viz.go()

ground = viz.add('tut_ground.wrl')
viz.clearcolor(0.5,0.5,1)

soccerball1 = viz.add('soccerball.ive')
soccerball2 = viz.add('soccerball.ive')
soccerball3 = viz.add('soccerball.ive')

soccerball1.translate(-1,1.5,3)
soccerball2.translate(0,1.5,3)
soccerball3.translate(1,1.5,3)

arrow = viz.add('arrow.wrl')
arrow.scale(.1,.1,.1)
arrow.visible(viz.OFF)

def mouseclick(button):
global ball
if button == viz.MOUSEBUTTON_LEFT:
pickObj = viz.pick()
if pickObj.valid():
ball = pickObj
ground.disable(viz.PICKING)


def myTranslate(key):
global ball
if not ball:
return
else:
if key == viz.KEY_UP:
ball.translate(0,1)
elif key == viz.KEY_DOWN:
ball.translate()
elif key == viz.KEY_RIGHT:
ball.translate()
elif key == viz.KEY_KP_LEFT:
ball.translate()

vizact.whilekeydown(viz.KEY_UP,myTranslate,viz.KEY _UP)
vizact.whilekeydown(viz.KEY_DOWN,myTranslate, viz.KEY_DOWN)
vizact.whilekeydown(viz.KEY_RIGHT,myTranslate, viz.KEY_KP_RIGHT)
vizact.whilekeydown(viz.KEY_LEFT,myTranslate, viz.KEY_KP_LEFT)


viz.callback(viz.MOUSEDOWN_EVENT,mouseclick)


viz.mouse(viz.OFF)

nlfrnassimi
02-11-2009, 06:24 PM
I have solved the problem of assigning value to translate, the only problem left is that the right ball jumps when I try to move it but the other 2 works perfect.

import viz

viz.go()

ground = viz.add('tut_ground.wrl')
viz.clearcolor(0.5,0.5,1)
ground.disable(viz.PICKING)

soccerball1 = viz.add('soccerball.ive')
soccerball2 = viz.add('soccerball.ive')
soccerball3 = viz.add('soccerball.ive')

soccerball1.translate(-1,1.5,3)
soccerball2.translate(0,1.5,3)
soccerball3.translate(1,1.5,3)


def mouseclick(button):
global ball
if button == viz.MOUSEBUTTON_LEFT:
pickObj = viz.pick()
if pickObj.valid():
ball = pickObj



def myTranslate(key):
global ball
if not ball:
return
else:
if key == viz.KEY_UP:
ball.translate(0,0.01,0,ball)
elif key == viz.KEY_DOWN:
ball.translate(0,-0.01,0,ball)
elif key == viz.KEY_RIGHT:
ball.translate(0.01,0,0,ball)
elif key == viz.KEY_LEFT:
ball.translate(-0.01,0,0,ball)

vizact.whilekeydown(viz.KEY_UP,myTranslate,viz.KEY _UP)
vizact.whilekeydown(viz.KEY_DOWN,myTranslate, viz.KEY_DOWN)
vizact.whilekeydown(viz.KEY_RIGHT,myTranslate, viz.KEY_RIGHT)
vizact.whilekeydown(viz.KEY_LEFT,myTranslate, viz.KEY_LEFT)


viz.callback(viz.MOUSEDOWN_EVENT,mouseclick)


viz.mouse(viz.OFF)

moooh
02-13-2009, 12:59 AM
That is odd. The 3 balls are dealt with in the exact same way so having only 1 of them not behaving as intended is strange. In what way the third ball jumping? The only thing I can see about your code is that you have accidently put a space in the last argument of viz.KEY_UP in the whilekeydown statement.