Vizard 7 » Tutorials & Examples » Picking with the mouse » Tutorial: Picking objects
7.7

Tutorial: Picking objects

This tutorial will teach you how to detect when a user clicks on an object. In this example there will be three objects in front of the user. When the user clicks on one of the objects, an arrow will appear above the object showing which one they selected.

This content is discussed in more detail in the reference section.

Setting up a picking world

If you have trouble getting the code in this tutorial to work, you can find the complete example script pickingWithTheMouse.py in the \tutorials\picking directory.

 

Add the following code to it:

import viz
import vizact

viz.setMultiSample(4)
viz.fov(60)
viz.go()

viz.clearcolor(viz.SKYBLUE)

soccerball = viz.addChild('soccerball.osgb')
basketball = viz.addChild('basketball.osgb')
volleyball = viz.addChild('volleyball.osgb')

soccerball.setPosition([-0.5,2,1.5])
basketball.setPosition([0,2,1.5])
volleyball.setPosition([0.5,2,1.5])

arrow = viz.addChild('arrow.wrl')
arrow.setScale([0.1,0.1,0.1])
arrow.visible(viz.OFF)

Run the script. As you can see, all it does is add three balls and places them in front of the user. It also adds an arrow but hides it since no ball is initially selected.

Picking objects

Accessing pick information

Picked object details