WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 01-13-2011, 05:51 AM
Zword Zword is offline
Member
 
Join Date: Jan 2011
Posts: 6
moving object forward backward

When the mouse is pointed at the object I want to move it depending on wich key i press. A normal int number works but example +=0.5 does not work.

Code:


Code:
cube = vizshape.addCube()

def moveForward():
	object = viz.pick()
	if object.valid():
		pos += 10
		object.setPosition([pos],0,0)
		print 'move backward'

vizact.onkeydown( 'w', moveForward )

def moveBackward ():
	object = viz.pick()
	if object.valid():
		pos += 10
		object.setPosition([pos],0,0)
	print 'move forward'
	
vizact.onkeydown( 's', moveBackward )
Reply With Quote
  #2  
Old 01-13-2011, 12:17 PM
masaki masaki is offline
Member
 
Join Date: Jan 2008
Posts: 63
hello,

the setPosition command accepts lists in [x,y,z] format so try object.setPosition([pos,0,0]). if you're trying to go forward and backward, that's normally along the z-axis so you might want to change it to ([0,0,pos]).

Masaki
Reply With Quote
  #3  
Old 01-16-2011, 05:43 AM
Zword Zword is offline
Member
 
Join Date: Jan 2011
Posts: 6
That is not working. This is the error I get:

Traceback (most recent call last):
File "C:\Program Files (x86)\WorldViz\Vizard30/python\vizact.py", line 3019, in __onkeydown
self._callGroup(self.__keydownmap[key])
File "C:\Program Files (x86)\WorldViz\Vizard30/python\vizact.py", line 2971, in _callGroup
val = e.call(arg)
File "C:\Program Files (x86)\WorldViz\Vizard30/python\vizact.py", line 2794, in _callStatic
return func(*args,**kwargs)
File "Test wall.py", line 39, in moveForward
pos += 0.5
UnboundLocalError: local variable 'pos' referenced before assignment


My code:

Code:
def moveForward():
	object = viz.pick()
	pos += 0.5
	if object.valid():
		object.setPosition([pos,0,0])
		viz.callback(viz.COLLIDE_BEGIN_EVENT,oncollide)

vizact.onkeydown( 'w', moveForward
Reply With Quote
  #4  
Old 01-18-2011, 01:08 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
You need to assign a value to pos before you add to it:
Code:
import viz
import vizshape
import vizact
viz.go()

cube = vizshape.addCube()
cube.setPosition([0,1.5,3])

pos = 3

def moveForward():
    global pos
    object = viz.pick()
    if object.valid():
        pos += 0.5
        object.setPosition([0,1.5,pos])
        print 'move forward'

vizact.onkeydown( 'w', moveForward )

def moveBackward ():
    global pos
    object = viz.pick()
    if object.valid():
        pos -= 0.5
        object.setPosition([0,1.5,pos])
    print 'move backward'
    
vizact.onkeydown( 's', moveBackward )
Reply With Quote
  #5  
Old 01-20-2011, 09:21 AM
Zword Zword is offline
Member
 
Join Date: Jan 2011
Posts: 6
Tnx it worked. Tnx for your time
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
retrieve Object names Geoffrey Vizard 11 12-11-2009 05:26 AM
Attaching sound to a moving object GiudiceLab Vizard 6 08-21-2009 09:52 AM
moving and object by mouse but don't know how to stop the movement nlfrnassimi Vizard 8 04-26-2009 08:23 AM
Moving view with object Xliben Vizard 2 07-25-2005 06:36 PM


All times are GMT -7. The time now is 10:20 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC