Back to contents pageHow to add collision detection to your application

How to add collision detection to your application

Summary

This article shows how to create a spatial relationship that performs collision detection using Site Manager and the .NET API.



Prerequisites

You should know the information in the following article:



Example material

Save this link to get a zip file of the example material


Guide

Before you start, remember to:

This guide shows you how to create a spatial relationship that monitors collisions between people.



1) Create the spatial relationship

After completing the basic setup above, add 2 new roles in the 'Geometry' tab of Site Manager. Name them 'Person_Object_Role' and 'Person_Subject_Role'. Both of these roles are going to have shapes relative to the type 'Person', so they will move around as the people move. The object role contains the subject role, so we need to define the shapes in the following ways. Firstly, define a new relative shape for the 'Person_Object_Role' as a cylinder slightly larger than the person, noting the bottom and top values:



Now define a new relative shape for the 'Person_Subject_Role' as a very small cylinder in the middle of the person, noting the bottom and top values. This ensures that the object role can contain the subject role as easily as possible:



Now add the relationship 'Person_Object_Role contains Person_Subject_Role' to the 'Monitored relations' section of the 'Geometry' tab of Site Manager. Do this by clicking on the 'Edit' button for either the 'Person_Object_Role Contains' relations or the 'Person_Subject_Role Contained by' relations.



2) Monitor the spatial relationship

We cannot use the Ubisense Map application to monitor this relationship because each person plays the role of both object and subject. Therefore, the relationship 'Person_Object_Role contains Person_Subject_Role' is always true for every person. We need to add a simple monitoring application that ignores 'collisions' of people with themselves. You should have created a Ubisense.USpatial.Monitor schema event client object monitor_schema and connected to the schema service at the Geometry Cell level.

Add an insert handler for the Ubisense.USpatial.Monitor.Contains relation called HandleCollision:


    Ubisense.USpatial.Monitor.Contains.AddInsertHandler(monitor_schema, HandleCollision);

Define the collision handler as follows. Remember the row type for the Ubisense.USpatial.Monitor.Contains relation is Ubisense.USpatial.Interaction:


    static void HandleCollision(Ubisense.USpatial.Interaction i)
    {
        // A person cannot collide with themselves
        if (i.object_ != i.subject_)
        {
            // Add your code for when 2 people collide here...
        }
    }

Use the simulator to make 2 people walk through each other to make your handler execute. If you defined the roles' shapes to be symmetrical about the middle of the person, your handler will execute once for each of the 2 people involved in the collision:



The example application simply prints out the names of the objects involved:





3) Improvements to the collision detection

It is possible to make collision detection very accurate by extending what we have already done. You can add many 'Person_Subject_Role_x' roles to cover the edge of a person with very small shapes. You can add more 'Person_Object_Role_x' roles for each side of the person's shape, ensuring that each of the subject roles fits completely inside each of the object roles.

Finally, add monitored relations for each possibility of 'Person_Object_Role_x contains Person_Subject_Role_x'.

Other things you might want to add include:



Back to top