PDA

View Full Version : Draw Line between Points selected by mouse


Johannes
01-07-2005, 12:55 PM
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)

farshizzo
01-07-2005, 01:07 PM
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?

Johannes
01-07-2005, 01:35 PM
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

farshizzo
01-07-2005, 01:46 PM
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.

Johannes
01-07-2005, 01:49 PM
Thank you, I understand this now better!
Johannes

Johannes
01-07-2005, 01:51 PM
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

Johannes
01-07-2005, 03:07 PM
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)

farshizzo
01-07-2005, 03:50 PM
Hi,

vizmat.VectorToPoint returns a python list of 3 numbers. To create a vector from it you must do the following:vector = viz.Vector(vizmat.VectorToPoint(p1, p2))