Win an Ipad
  Developer   Demos & Samples   Sensors

Sensors

Screenshot
Sensors provide significant location opportunities for applications. Nowadays mobile devices support many types of sensors like GPS, acceleration, magnetic sensor etc. Applications like GPS navigation, magnetic compass, spirit-level or various games (e.g. teeter etc) can be created through the use of sensors. In this sample we are going to create a basic teeter game. It will have only one ball that moves according to real gravity laws.

Sensors

Sensors in Moscrif are managed by native Sensor class that supports following sensor types:

  • acceleration: Acceleration sensor measures the rate of acceleration on three axes.
  • magnetic: Magnetic sensor measures the intensity of the magnetic field of the Earth on three axes. 
  • proximity: Proximity sensor is a sensor able to detect the presence of nearby objects without any physical contact.
  • tilt: Tilt sensor measures the tilt of device. The same measurement can also be done also by acceleration or magnetic sensor.

Note: The GPS sensor is managed by separate class GPS.

However, not every device running Moscrif supports all types of sensors. To check if the device supports required sensor type, we can use function isAvailable. Parameter of this function is a symbol representing the type of sensor.

One instance of the Sensor class can manage only one sensor type. Sensor type is set by first parameter of the Sensor class constructor. Event onDataReceived is called in regular intervals and its parameters contain all necessary data from the sensor.

Acceleration sensor

To create an access to the acceleration sensor, we need  to create an instance of the Sensor class. Firstly, before the new instance of sensor is created we need to check if the device supports this sensor.

Example: create an instance of Sensor class to access acceleration sensor

// check if accelometer sensor is available
if (!Sensor.isAvailable(#acceleration)) {
    System.messageBox("No accelometer detected!");
} else {
    // create sensor
    var sensor = new Sensor(#acceleration, 40);
    // setup handler for receiving data
    sensor.onDataReceived = function(sender, timestamp, params)
    {
        ...
    };
    // start receiving data
    sensor.start();
}

When the sensor receives new data, it causes event onDataReceived. For acceleration sensor, the third parameter of this function is array of the three float values, representing g-force on x, y and z axis. Start function commences data-receiving from the sensor.

Image: acceleration sensor axis distribution

Example: onDataReceived function

// setup handler for receiving data
sensor.onDataReceived = function(sender, timestamp, params)
{
    // get g-force on every axis
    var (x, y, z) = (params[0], params[1], params[2]);
    // update info object
    app.info.accelX = String.printf("accel X: %.2f", x);
    app.info.accelY = String.printf("accel Y: %.2f", y);
    app.info.accelZ = String.printf("accel Z: %.2f", z);
    // update the gravity, acording to device position
    app._world.setGravity(-x * 5., -y * 5.);
};

Physics

This sample implements one physics world, which gravity is changed according to data from thesensor. There is only one ball in the world; represented by PhysicsSprite class. Borders are set around the play area and when the ball collides with the them, sound effect is played and the ball changes direction, depending on the collision circumstances.

Example: create world and manage contacts

// create physics world - no gravity
this._world = PhysicsScene.create(0.0, 0.0, true, true);
// play sounds when collision wall<->ball detected
this._world.onBeginContact = function(sender, contact)
{
    var current = contact;
    while (current != null) {
        var bodyA = current.getBodyA();
        var bodyB = current.getBodyB();
        if (bodyA == this super._ball || bodyB == this super._ball)
            this super._sound.play();
        current = current.getNext();
    }
};

This sample is focused on sensors, for more informations about physics world and bodies see Box2dCrate sample.

Summary

Moscrif supports four types of sensors: acceleration, magnetic, proximity and tilt. These sensors are managed by Sensor class. GPS sensor is managed by separate GPS class.  Class constructor is used to get  an access to the sensor and start function is used for data-receiving The sensor data are stored in onDataReceived event. 

 Hello UI   Vector graphics 
Write a Comment (0)
Subject
Please complete this mandatory field.
HTML Tags Not Allowed!
Comment
Please complete this mandatory field.