WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 01-07-2005, 11:55 AM
Johannes Johannes is offline
Member
 
Join Date: Jan 2005
Posts: 143
Draw Line between Points selected by mouse

Hi,
the following program should draw a line between two in the "world" selected by mouse.

As viz.screentoworld(viz.mousepos()) returns a vector - is there an easier way to figure out the point the mouse was pressed at (e.g. return value [3,4,5]?

I guess I really don't understand viz.screentoworld(viz.mousepos()) that much - where does this vector start - at the viewpoint?

Thank you,
Johannes



import viz

viz.go()

SPIsSet,EPIsSet=0,0
startPoint=[0,0,0]
endPoint=[0,0,0]


def mousedown(button):
#
if button == viz.MOUSEBUTTON_LEFT:
global startPoint,SPIsSet
global endPoint,EPIsSet
if SPIsSet==0:
startPoint=viz.screentoworld(viz.mousepos())
print 'startPoint:',startPoint
SPIsSet=1
elif (SPIsSet!=0 & EPIsSet==0):
endPoint=viz.screentoworld(viz.mousepos())
print 'ePoint:',endPoint
EPIsSet=1
drawLine(startPoint, endPoint)
EPIsSet=0
SPIsSet=0
else:
print 'forgot option 3'

def drawLine(begin, end):
viz.clearcolor(0.0,0.0,0.0)
viz.vertexcolor(2, 4, 4)
viz.startlayer(viz.LINE_LOOP)
viz.vertex(begin)
viz.vertex(end)
line = viz.endlayer()
#posBox=viz.add('../resources/models/box.wrl')
#posBox.scale(0.01,0.01,0.01)
#posBox.translate(begin)

#Create callbacks for timer events and mouse click events
viz.callback(viz.MOUSEDOWN_EVENT,mousedown)
Reply With Quote
  #2  
Old 01-07-2005, 12:07 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Hi,

The viz.screentoworld command returns a 3d vector in world coordinates that points into the screen from the given 2d screen coordinates. The return value is a list of 6 numbers. The first 3 numbers represent the begin point of the vector and the last 3 numbers represent the end point of the vector.

In your program, is the mouse selecting a specific 3d object, or is it simply selecting any point on the screen?
Reply With Quote
  #3  
Old 01-07-2005, 12:35 PM
Johannes Johannes is offline
Member
 
Join Date: Jan 2005
Posts: 143
dear farshizzo,

You wrote:
>The viz.screentoworld command >returns a 3d vector in world >coordinates that points into the >screen from the given 2d screen >coordinates.

I still don't understand where it get's the start-point from (the first three numbers)? Howdoes vizard determine how far to point into the screen? Why does this begin-point not start at the origin of the 3d universe?

The mouse is simply selecting any point on the screen!

The program below works, just wanted to understand, where screentoworld get's its startvalue from and second if there is an easier way to get the point where the mouse is pressed (in 3d coordinates e.g. [3,3,3]) - so I could simplify it.

Best, Johannes

Last edited by Johannes; 01-07-2005 at 12:47 PM.
Reply With Quote
  #4  
Old 01-07-2005, 12:46 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Hi,

The start point is based on the position and rotation of the viewpoint. The line is then extended out from this position towards the direction of the viewpoint.

If you simply wanted to draw the lines on the screen then you wouldn't need the 3d coordinates, but this is the only way to it using 3d coordinates. If the mouse was selecting a 3d object in the world, then you could get the exact 3d point that the mouse is clicking on using the viz.pick command, but if you are simply clicking on any point on the screen then what you are doing now is fine.
Reply With Quote
  #5  
Old 01-07-2005, 12:49 PM
Johannes Johannes is offline
Member
 
Join Date: Jan 2005
Posts: 143
Thank you, I understand this now better!
Johannes
Reply With Quote
  #6  
Old 01-07-2005, 12:51 PM
Johannes Johannes is offline
Member
 
Join Date: Jan 2005
Posts: 143
you wrote:
>If you simply wanted to draw the lines on the screen then you wouldn't need the 3d coordinates


no, I don't want to simply draw lines on the screen - i want to generate a vector for moving objects with a certain direction.

Johannes
Reply With Quote
  #7  
Old 01-07-2005, 02:07 PM
Johannes Johannes is offline
Member
 
Join Date: Jan 2005
Posts: 143
I have the two points, now I want to make the ball move between these two points.

vectorToPoint (even if the name sounds different) should return a vector (Help: This command will return a normalized vector that points from p1 to p2)

Multiplying the vector with 0.2 I get the following error:

Traceback (most recent call last):
File "<string>", line 12, in ?
File "Vizard7.py", line 16, in ?
futurePos = pos + (ball.vector * 0.02)
TypeError: can't multiply sequence to non-int



Extracted an example code:

import viz
import vizmat
viz.go()


p1 = [0,0,0]
p2 = [1,0,1]

#This will print out [0.707,0,0.707]
print vizmat.VectorToPoint(p1,p2)

ball = viz.add('../resources/models/ball.wrl')
vector=vizmat.VectorToPoint(p1, p2)
print 'ballVector',vector
pos = ball.get(viz.POSITION)
futurePos = pos + (vector * 0.02)
Reply With Quote
  #8  
Old 01-07-2005, 02:50 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Hi,

vizmat.VectorToPoint returns a python list of 3 numbers. To create a vector from it you must do the following:
Code:
vector = viz.Vector(vizmat.VectorToPoint(p1, p2))
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:10 PM.


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