WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 02-12-2009, 11:21 PM
nlfrnassimi nlfrnassimi is offline
Member
 
Join Date: Feb 2009
Posts: 37
Unhappy moving and object by mouse but don't know how to stop the movement

my code is to select a ball and move it by mouse, I want to click a key and the ball will remain in that position, but have no idea how to do it.

please help

this is my code :


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)

viz.mouse(viz.OFF)

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


def mymouse(e):
global ball
if not ball:
return
else:
ball.setPosition((e.x-0.5)*3,e.y*3.5,3)


viz.callback(viz.MOUSEDOWN_EVENT,mouseclick)
viz.callback(viz.MOUSE_MOVE_EVENT,mymouse)
Reply With Quote
  #2  
Old 02-13-2009, 02:07 AM
moooh moooh is offline
Member
 
Join Date: Dec 2008
Posts: 19
You will just have to set the ball value to None and the ball should stay at its current position as your code already checks whether or not a ball is active before trying to move it.

You could alter the mouse down function to pick up the ball if none is selected and drop the ball if currently holding one, like this:

Code:
def mouseclick(button):
    global ball
    if button == viz.MOUSEBUTTON_LEFT:
        if not ball: #we are not holding a ball, try to pick one up
            pickObj = viz.pick()
            if pickObj.valid():
                ball = pickObj
        else: #we were holding a ball, drop it
            ball = None
or instead of changing the mousedown function you could make it so that pressing a key on the keyboard drops the ball:
Code:
def dropBall:
    global ball
    ball = None
vizact.onkeydown( 'd', dropBall )
This will drop the ball when the d key is pressed.
Reply With Quote
  #3  
Old 02-15-2009, 07:02 PM
nlfrnassimi nlfrnassimi is offline
Member
 
Join Date: Feb 2009
Posts: 37
mouse wheel problem

Thanks for your help, it works perfect. I have another problem.

I want to assign mouse wheel to be able to move my selected ball in depth. But I have no idea how to do it. Please help.
Reply With Quote
  #4  
Old 02-17-2009, 06:33 AM
DrunkenBrit DrunkenBrit is offline
Member
 
Join Date: Dec 2008
Location: England
Posts: 25
Maybe something along the lines of:

Code:
diffInZ = 3.0 # Value to add/subtract to Z position of selected ball

def onMouseWheel(dir):

    global ball # Same ball object selected from previous mouse move callback

        # Check 'ball' is valid/one selected from previous callback
               ballPos = ball.getPosition()
               addZ = -3.0            
        
               # If wheel is moving forward, make positive, else leave as negative
               if dir > 0:
                 addZ = 3.0
            
            ball.setPosition( ballPos[0], ballPos[1], ballPos[2] + addZ )

viz.callback(viz.MOUSEWHEEL_EVENT,onMouseWheel)
__________________
Software Developer
Virtalis Ltd.
www.virtalis.com
Reply With Quote
  #5  
Old 02-18-2009, 08:39 PM
nlfrnassimi nlfrnassimi is offline
Member
 
Join Date: Feb 2009
Posts: 37
Thanks for your great help. It worked great.

Just that when I move the ball in depth and then try to move the mouse the ball comes back to front. do you know how should I solve this problem?
Reply With Quote
  #6  
Old 02-19-2009, 01:35 AM
moooh moooh is offline
Member
 
Join Date: Dec 2008
Posts: 19
Yes at this line of code
Code:
ball.setPosition((e.x-0.5)*3,e.y*3.5,3)
you have coded to always put the ball at z-coord 3 when moving the mouse. If you want to get the z-coord unaltered by the regular mouse movement you will have to change it to use the z-value from the ball's current position
Code:
pos = ball.getPosition() #get the current position
ball.setPosition((e.x-0.5)*3,e.y*3.5, pos[2] )  #pos[2] is the z-coord for the current position
Reply With Quote
  #7  
Old 02-19-2009, 01:36 AM
DrunkenBrit DrunkenBrit is offline
Member
 
Join Date: Dec 2008
Location: England
Posts: 25
Are you resetting the Z position of the selected ball in the mouse movement callback which moves the ball's X and/or Y position?
__________________
Software Developer
Virtalis Ltd.
www.virtalis.com
Reply With Quote
  #8  
Old 02-20-2009, 12:48 AM
nlfrnassimi nlfrnassimi is offline
Member
 
Join Date: Feb 2009
Posts: 37
Thanks for your help mooh. it works perfect.
Reply With Quote
  #9  
Old 04-26-2009, 08:23 AM
nasr nasr is offline
Member
 
Join Date: Apr 2009
Posts: 27
could u plz help me...i am also developing similar application

i am also practising to develop a similar application which u come across with. i want to create a virtual room with a table and some books on it. along with a chair and i want to move, rotate the chair with the help of mouse. can u plz post the code for picking up the objects and moving, rotating, thanking you..
Quote:
Originally Posted by nlfrnassimi View Post
my code is to select a ball and move it by mouse, I want to click a key and the ball will remain in that position, but have no idea how to do it.

please help

this is my code :


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)

viz.mouse(viz.OFF)

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


def mymouse(e):
global ball
if not ball:
return
else:
ball.setPosition((e.x-0.5)*3,e.y*3.5,3)


viz.callback(viz.MOUSEDOWN_EVENT,mouseclick)
viz.callback(viz.MOUSE_MOVE_EVENT,mymouse)
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
how can I stop an action? nlfrnassimi Vizard 4 02-13-2009 01:59 AM
Pick porblem with arrays shivanangel Vizard 3 08-27-2007 11:10 AM
Moving view with object Xliben Vizard 2 07-25-2005 06:36 PM


All times are GMT -7. The time now is 07:25 AM.


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