Main Content

Collision Detection Using Line Sensor

The vrcollisions example shows a simple way how to implement collision detection in Simulink® 3D Animation™.

In the virtual world, an X3D LinePickSensor is defined. This sensor detects approximate collisions of several rays (modeled as IndexedLineSet) with arbitrary geometries in the scene. In case of geometric primitives, exact collisions are detected. One of LinePickSensor output fields is the isActive field, which becomes TRUE as soon as the collision between any of the rays and surrounding scene objects is detected.

The LinePickSensor with 3 visible yellow detection rays is attached to a trivial model of a Robot (represented by a blue cylinder). The Robot is inside a room with several obstacles. During the simulation, the Robot moves forward as long as its sensor doesn't bounce into a wall or an obstacle. Use the Left and Right buttons to turn the Robot so that there is a free path ahead, and the Robot starts moving again.

In the model, there are both VR Sink and VR Source blocks defined, associated with the same virtual scene. VR Source is used to read the sensor isActive signal and the current position of the Robot. The VR Sink is used to set the Robot position / rotation and the color - the Robot turns red when blocked.

In the virtual world, there are two viewpoints defined - one static - "Overview", and one attached to the Robot - "Follow Robot". Change to the latter to see the scene from a different perspective.