Introduction

Sensors acquire information from a simulation. There are several types of Sensors, including Acceleration Sensors, Incline Sensors, Raycast Sensors, and Volume Sensors. This tutorial will show how to setup each type. It will also give examples of how each type could be used. Keep in mind that a Sensor can either be attached to a Solid or just positioned somewhere in the environment.

Let’s first create all the objects we’ll need (and assume they will be initialized elsewhere):
Solid solid = sim.CreateSolid(); 

Note: sim is an initialized Simulator

Acceleration Sensor

The first Sensor we will use here is an Acceleration Sensor:
AccelerationSensorData data = new AccelerationSensorData();
data.Solid = solid;
data.Transform.translate(4, 0, -1);
AccelerationSensor accelSensor = sim.CreateAccelerationSensor();
accelSensor.Init(data);

...

Vector3 accel = accelSensor.GlobalLinearAccel; 


The Sensor is automatically attached to the Solid if its Solid pointer is set. It calculates the linear and angular acceleration of the attached Solid at the point of the Sensor (which can be offset from the Solid by using the Sensor’s transform). It could be used in a simulated robot to give it the ability to sense which way it is falling.

Incline Sensor

The next Sensor is the Incline Sensor:
InclineSensorData data = new InclineSensorData();
data.Solid = solid;
data.Axis = new Vector3(1, 0, 0);
InclineSensor inclinometer = sim.CreateInclineSensor();
inclinometer.Init(data);

...

float angle = inclinometer.Angle; 


The Sensor takes a local rotation axis relative to its attached Solid. It calculates and returns the angle the Solid has rotated around this axis. Multiple Incline Sensors could be attached to a vehicle, for instance, to simulate a real inclinometer. The combination of an Acceleration Sensor and an Incline Sensor could be used to simulate the human vestibular system.

Raycast Sensor

The next Sensor is the Raycast Sensor:
RaycastSensorData data;
data.Solid = solid;
data.Ray.Position = new Vector3(0, 0, 0);
data.Ray.Direction = new Vector3(0, 0, -1);
data.ContactGroup = 2;
RaycastSensor raySensor = sim.CreateRaycastSensor();
raySensor.Init(data);

...

RaycastResult result = raySensor.FireRay(500);
Vector3 hitPoint = result.Intersection;
Solid hitSolid = result.Solid; 


Raycast Sensors must be fired manually and you need to set the firing distance – they do not update anything every time step. When they are fired, they cast a ray into the environment and return data describing the closest point of intersection (if any) from the ray’s origin. The Raycast Sensor uses a contact group to limit which objects it collides with (see Tutorial: Contact Groups).

Also check out this method:
public virtual List<RaycastResult> RaycastSensor.FirePiercingRay(float length); 

Volume Sensor

The Volume Sensor is similar to the Raycast Sensor:
VolumeSensorData data = new VolumeSensorData();
VolumeSensor volSensor = sim.CreateVolumeSensor();
volSensor.Init(data);

// Define a “volume” Solid.
Solid volume = sim.CreateSolid();
SphereShapeData sphereData = new SphereShapeData();
sphereData.Radius = 5;
volume.AddShape(sphereData);

...

// Set transform for the sensor
Matrix m = Matrix.Identity;
m.Translation = new Vector3(x, y, z);
volSensor.Transform = m;

VolumeQueryResult result = volSensor.QueryVolume(volume);
for (int i = 0; i < result.NumSolids; i++)
{
    _Solid s = result.GetSolid(i);
    _Force f = new Force();
    _f.Type = ForceType.GlobalForce;
    _f.Duration = 0.05;
    _f.Position = s.Position – volume.Position;
    _f.Position *= 100;
    _s.AddForce(f);
} 


Volume Sensors take a “volume” Solid and query the environment for Solids that collide with the volume Solid. The resulting data structure contains a list of pointers to those Solids. Like Raycast Sensors, they can utilize contact groups to limit the set of Solids that are collided with the volume Solid; simply set the contact group of the volume Solid. They also are not updated every time step; they must be queried manually. The example above shows how to simulate an explosion: it finds all the Solids within a 5 unit radius from the volume’s center and applies an outward force on each object scaled linearly by its distance from the center.

Destroying Sensors

When you are done with a Sensor, tell the Simulator to destroy it:
sim.DestroySensor(sensor); 

Last edited Oct 12, 2006 at 7:07 PM by KleMiX, version 2

Comments

No comments yet.