-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathSensorDistance.java
42 lines (32 loc) · 1.1 KB
/
SensorDistance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import processing.core.*;
/**
* Simulates an ultrasonic distance sensor.
* Readings are synchronous and blocking.
*/
class SensorDistance extends Sensor{
// Random noise applied to the final reading
private static final float NOISE_AMOUNT = .01f;
// Angle in degrees between robot's heading and sensor heading
public float localOrientation = 0f;
SensorDistance(GameSimulator g, Robot r, float localOrientation) {
super(g, r);
this.localOrientation = localOrientation;
}
/**
* @return float array of size 1 with the current distance
*/
// TODO this should fail to read based on the angle between ray and surface
public float[] readValues() {
PVector origin = robot.getRealPosition();
float direction = robot.getOrientation() + (float) Math.toRadians(localOrientation);
// use border of robot
PVector v = PVector.fromAngle(direction);
v.setMag(robot.getRadius());
origin.add(v);
float dist = game.closestSimulatableInRay(robot, origin, direction);
dist = getReadingAfterNoise(dist, NOISE_AMOUNT);
float[] values = new float[1];
values[0] = Math.max(dist, 0);
return values;
}
}