WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 02-04-2007, 09:40 AM
Jerry Jerry is offline
Member
 
Join Date: Jun 2004
Posts: 105
Mouse-drag an object

What's the best way to click on and mouse-drag an object
around the screen so that the object stays under the cursor?
Reply With Quote
  #2  
Old 02-05-2007, 03:07 PM
Gladsomebeast Gladsomebeast is offline
Member
 
Join Date: Mar 2005
Location: Isla Vizta, CA
Posts: 397
In 3.0 you can link to a viz.Mouse linkable object. If the objects you want to drag are on the screen you could do a viz.grab( viz.Mouse, myScreenObj ) when you click on the object. This viz.grab() function would maintain the place where the mouse grabbed the object.

Also, you can do this all manualy. The vizinfo object does this and because it is implemnted in python all the code is there for you to see. Open the Vizard30/python/vizinfo.py file and search for "drag."
__________________
Paul Elliott
WorldViz LLC
Reply With Quote
  #3  
Old 02-05-2007, 03:19 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Here is a sample script that should work with 2.5
Code:
import viz
viz.go()

DRAG_TIMER = 1

balls = []

#Add 3 balls and place them in front of the user
for x in range(3):
	ball = viz.add('ball.wrl')
	ball.translate(x-1,1.7,4)
	balls.append(ball)
	
#Add a ground
ground = viz.add('tut_ground.wrl')
ground.disable(viz.PICKING)

#Set background color
viz.clearcolor(viz.SKYBLUE)

#Disable mouse navigation
viz.mouse(0)

#The object that will be dragged
dragObject = None

def BeginDrag():
	global dragObject
	#Pick an object
	info = viz.pick(1)
	#Make sure object is one of the balls
	if info.object in balls:
		#Set the drag object
		dragObject = info.object
		dragObject.offset = vizmat.Vector(dragObject.get(viz.POSITION)) - info.intersectPoint
		dragObject.dist = vizmat.Distance(viz.get(viz.HEAD_POS),info.intersectPoint)
		viz.starttimer(DRAG_TIMER,0,viz.FOREVER)

vizact.onmousedown(viz.MOUSEBUTTON_LEFT,BeginDrag)

def EndDrag():
	global dragObject
	#Stop dragging
	dragObject = None
	viz.killtimer(DRAG_TIMER)

vizact.onmouseup(viz.MOUSEBUTTON_LEFT,EndDrag)

def onTimer(num):
	if num == DRAG_TIMER:
		#Move the object based on mouse movement
		line = viz.screentoworld(viz.mousepos())
		begin = line[:3]
		end = line[3:]
		dir = vizmat.Vector(end) - begin
		dir.normalize()
		dir *= dragObject.dist
		pos = vizmat.Vector(begin) + dir + dragObject.offset
		dragObject.translate(pos.get())

viz.callback(viz.TIMER_EVENT,onTimer)
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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


All times are GMT -7. The time now is 01:35 AM.


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