SFML 3.0.1
Loading...
Searching...
No Matches
Enumerations | Functions | Variables
sf::Sensor Namespace Reference
Window module

Give access to the real-time state of the sensors. More...

## Enumerations
enum class | Type { Accelerometer , Gyroscope , Magnetometer , Gravity , UserAcceleration , Orientation }
| Sensor type. More...

## Functions
bool | isAvailable (Type sensor)
| Check if a sensor is available on the underlying platform.
void | setEnabled (Type sensor, bool enabled)
| Enable or disable a sensor.
Vector3f | getValue (Type sensor)
| Get the current sensor value.

## Variables
static constexpr unsigned int | Count {6}
| The total number of sensor types.

## Detailed Description

Give access to the real-time state of the sensors.

sf::Sensor provides an interface to the state of the various sensors that a device provides.

This namespace allows users to query the sensors values at any time and directly, without having to deal with a window and its events. Compared to the SensorChanged event, sf::Sensor can retrieve the state of a sensor at any time (you don't need to store and update its current value on your side).

Depending on the OS and hardware of the device (phone, tablet, ...), some sensor types may not be available. You should always check the availability of a sensor before trying to read it, with the sf::Sensor::isAvailable function.

You may wonder why some sensor types look so similar, for example Accelerometer and Gravity / UserAcceleration. The first one is the raw measurement of the acceleration, and takes into account both the earth gravity and the user movement. The others are more precise: they provide these components separately, which is usually more useful. In fact they are not direct sensors, they are computed internally based on the raw acceleration and other sensors. This is exactly the same for Gyroscope vs Orientation.

Because sensors consume a non-negligible amount of current, they are all disabled by default. You must call sf::Sensor::setEnabled for each sensor in which you are interested.

Usage example:

if (sf::Sensor::isAvailable(sf::Sensor::Type::Gravity))
{
// gravity sensor is available
}
// enable the gravity sensor
sf::Sensor::setEnabled(sf::Sensor::Type::Gravity, true);
// get the current value of gravity
sf::Vector3f gravity = sf::Sensor::getValue(sf::Sensor::Type::Gravity);
sf::Sensor::getValue
Vector3f getValue(Type sensor)
Get the current sensor value.
sf::Sensor::Type::Gravity
@ Gravity
Measures the direction and intensity of gravity, independent of device acceleration (m/s^2)
Definition Sensor.hpp:49
sf::Sensor::isAvailable
bool isAvailable(Type sensor)
Check if a sensor is available on the underlying platform.
sf::Sensor::setEnabled
void setEnabled(Type sensor, bool enabled)
Enable or disable a sensor.
sf::Vector3f
Vector3< float > Vector3f
Definition Vector3.hpp:306

## Enumeration Type Documentation

## ◆ Type

enum class sf::Sensor::Type
| strong

Sensor type.

Enumerator
Accelerometer | Measures the raw acceleration (m/s^2)
Gyroscope | Measures the raw rotation rates (radians/s)
Magnetometer | Measures the ambient magnetic field (micro-teslas)
Gravity | Measures the direction and intensity of gravity, independent of device acceleration (m/s^2)
UserAcceleration | Measures the direction and intensity of device acceleration, independent of the gravity (m/s^2)
Orientation | Measures the absolute 3D orientation (radians)

Definition at line 44 of file Sensor.hpp.

## Function Documentation

## ◆ getValue()

Vector3f sf::Sensor::getValue | ( | Type | sensor | ) |
| nodiscard

Get the current sensor value.

Parameters

  sensor | Sensor to read

Returns
  The current sensor value

## ◆ isAvailable()

bool sf::Sensor::isAvailable | ( | Type | sensor | ) |
| nodiscard

Check if a sensor is available on the underlying platform.

Parameters

  sensor | Sensor to check

Returns
  true if the sensor is available, false otherwise

## ◆ setEnabled()

void sf::Sensor::setEnabled | ( | Type | sensor,
|  | bool | enabled )

Enable or disable a sensor.

All sensors are disabled by default, to avoid consuming too much battery power. Once a sensor is enabled, it starts sending events of the corresponding type.

This function does nothing if the sensor is unavailable.

Parameters

  sensor | Sensor to enable
  enabled | true to enable, false to disable

## Variable Documentation

## ◆ Count

unsigned int sf::Sensor::Count {6}
| static constexpr

The total number of sensor types.

Definition at line 55 of file Sensor.hpp.