Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
RigidbodySensor.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
5 using UnityEngine;
6 
7 namespace droid.Runtime.Prototyping.Sensors.Rigidbody {
11  [AddComponentMenu(SensorComponentMenuPath._ComponentMenuPath
12  + "Rigidbody"
13  + SensorComponentMenuPath._Postfix)]
14  [ExecuteInEditMode]
15  [RequireComponent(typeof(UnityEngine.Rigidbody))]
16  public class RigidbodySensor : Sensor,
18  [SerializeField] Space3 _angular_space = new Space3(new DistributionSampler(), 10);
19 
20  [Header("Observation", order = 100)]
21  [SerializeField]
22  Vector3 _angular_velocity = Vector3.zero;
23 
24  [SerializeField] bool _differential = false;
25 
26  [SerializeField] float _last_update_time = 0;
27 
28  [Header("Configuration", order = 110)]
29  [SerializeField]
30  UnityEngine.Rigidbody _rigidbody = null;
31 
32  [SerializeField] Vector3 _velocity = Vector3.zero;
33  [SerializeField] Space3 _velocity_space = new Space3(new DistributionSampler(), 10);
34 
38  public override string PrototypingTypeName {
39  get {
40  if (this._differential) {
41  return "RigidbodyDifferential";
42  }
43 
44  return "Rigidbody";
45  }
46  }
47 
51  public Vector3 Velocity {
52  get { return this._velocity; }
53  set {
54  this._velocity = this._velocity_space.IsNormalised
55  ? this._velocity_space.ClipNormaliseRound(value)
56  : value;
57  }
58  }
59 
63  public Vector3 AngularVelocity {
64  get { return this._angular_velocity; }
65  set {
66  this._angular_velocity = this._angular_space.IsNormalised
67  ? this._angular_space.ClipNormaliseRound(value)
68  : value;
69  }
70  }
71 
75  public Space3 VelocitySpace { get { return this._velocity_space; } }
76 
80  public Space3 AngularSpace { get { return this._angular_space; } }
81 
85  public override IEnumerable<float> FloatEnumerable {
86  get {
87  return new[] {
88  this.Velocity.x,
89  this.Velocity.y,
90  this.Velocity.z,
91  this.AngularVelocity.x,
92  this.AngularVelocity.y,
93  this.AngularVelocity.z
94  };
95  }
96  }
97 
101  public override void UpdateObservation() {
102  var update_time_difference = Time.time - this._last_update_time;
103  if (this._differential && update_time_difference > 0) {
104  var vel_diff = this.Velocity - this._rigidbody.velocity;
105  var ang_diff = this.AngularVelocity - this._rigidbody.angularVelocity;
106 
107  var vel_magnitude = vel_diff.magnitude;
108  if (vel_magnitude > 0) {
109  this.Velocity = vel_diff / (update_time_difference + float.Epsilon);
110  } else {
111  this.Velocity = vel_diff;
112  }
113 
114  var ang_magnitude = ang_diff.magnitude;
115  if (ang_magnitude > 0) {
116  this.AngularVelocity = ang_diff / (update_time_difference + float.Epsilon);
117  } else {
118  this.AngularVelocity = ang_diff;
119  }
120  } else {
121  this.Velocity = this._rigidbody.velocity;
122  this.AngularVelocity = this._rigidbody.angularVelocity;
123  }
124 
125  this._last_update_time = Time.time;
126  }
127 
131  protected override void PreSetup() { this._rigidbody = this.GetComponent<UnityEngine.Rigidbody>(); }
132  }
133 }
Vector3 ClipNormaliseRound(Vector3 v)
Definition: Space3.cs:55