Vizard 7 » Tutorials & Examples » Viewpoints & windows » Viewpoint collision » Tutorial: Enabling collision detection
7.6

Tutorial: Enabling collision detection

Once you have saved the file run the script.

 

The screen will initially be dark, use the mouse to navigate backwards.  As you move backwards you will emerge from a big red block. There will be other red blocks and cylinders in the scene.  Feel free to explore the scene.  

 

By default collision detection is off, so you should be able to move through all the objects.

 

Now let's enable collision detection. Just add the following line of code at the end of your script:

viz.MainView.collision( viz.ON )

Now run the script again.

 

The screen will still be initially dark. Try using the mouse to navigate backwards.  Don't worry, you are not supposed to be able to move backwards. The reason for this is that the viewpoint is initially at the origin, which in this case happens to be inside the big red block. So when we try to move out of it, the built-in collision detection, which we just enabled, stops us from going any further.  

 

This is a common problem people experience.  When enabling collision detection, you must ensure that the viewpoint is initially outside of any objects, or else you might spend a long time wondering why your world starts out blank.

 

To fix this we simply move the viewpoint backwards before we turn on collision detection. Add the following line of code before the line that turns on collision detection:

viz.MainView.setPosition( [5, 1.82, -15] )

This will move the viewpoint backwards 15 meters and 5 meters to the right. Now run the world. Navigate through the world again. Now you can collide with the other objects.

 

Try going up the ramp then moving off the edge of the block. You notice that collision works with the ground too because this command automatically activates gravity.

Setting up a collision world

Enabling collision detection

Gravity

Step height