Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
RigidbodyConfigurable.cs
Go to the documentation of this file.
1 using System;
6 using UnityEngine;
7 
8 namespace droid.Runtime.Prototyping.Configurables {
12  [AddComponentMenu(ConfigurableComponentMenuPath._ComponentMenuPath
13  + "Rigidbody"
14  + ConfigurableComponentMenuPath._Postfix)]
15  [RequireComponent(typeof(Rigidbody))]
20  string _ang_x;
21 
24  string _ang_y;
25 
28  string _ang_z;
29 
32  [SerializeField]
33  Vector3 _angular_velocity = Vector3.zero;
34 
37  [SerializeField]
38  Space3 _angular_velocity_space = Space3.ZeroOne;
39 
42  Rigidbody _rigidbody = null;
43 
46  string _vel_x;
47 
50  string _vel_y;
51 
54  string _vel_z;
55 
58  [Header("Observation", order = 110)]
59  [SerializeField]
60  Vector3 _velocity = Vector3.zero;
61 
64  [SerializeField]
65  Space3 _velocity_space = Space3.ZeroOne;
66 
69  public override string PrototypingTypeName { get { return "RigidbodyConfigurable"; } }
70 
73  public Vector3 Velocity { get { return this._velocity; } set { this._velocity = value; } }
74 
77  public Vector3 AngularVelocity {
78  get { return this._angular_velocity; }
79  private set { this._angular_velocity = value; }
80  }
81 
84  public Space3 VelocitySpace { get { return this._velocity_space; } }
85 
88  public Space3 AngularSpace { get { return this._angular_velocity_space; } }
89 
90  public override ISpace ConfigurableValueSpace { get; }
91 
94  public override void UpdateCurrentConfiguration() {
95  this.Velocity = this._rigidbody.velocity;
96  this.AngularVelocity = this._rigidbody.angularVelocity;
97  }
98 
101  protected override void PreSetup() {
102  this._rigidbody = this.GetComponent<Rigidbody>();
103  this._vel_x = this.Identifier + "VelX";
104  this._vel_y = this.Identifier + "VelY";
105  this._vel_z = this.Identifier + "VelZ";
106  this._ang_x = this.Identifier + "AngX";
107  this._ang_y = this.Identifier + "AngY";
108  this._ang_z = this.Identifier + "AngZ";
109  }
110 
113  protected override void RegisterComponent() {
114  this.ParentEnvironment =
115  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._vel_x);
116  this.ParentEnvironment =
117  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._vel_y);
118  this.ParentEnvironment =
119  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._vel_z);
120  this.ParentEnvironment =
121  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._ang_x);
122  this.ParentEnvironment =
123  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._ang_y);
124  this.ParentEnvironment =
125  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, (Configurable)this, this._ang_z);
126  }
127 
130  protected override void UnRegisterComponent() {
131  if (this.ParentEnvironment == null) {
132  return;
133  }
134 
135  this.ParentEnvironment.UnRegister(this, this._vel_x);
136  this.ParentEnvironment.UnRegister(this, this._vel_y);
137  this.ParentEnvironment.UnRegister(this, this._vel_z);
138  this.ParentEnvironment.UnRegister(this, this._ang_x);
139  this.ParentEnvironment.UnRegister(this, this._ang_y);
140  this.ParentEnvironment.UnRegister(this, this._ang_z);
141  }
142 
146  public override void ApplyConfiguration(IConfigurableConfiguration simulator_configuration) {
147  var vel = this._rigidbody.velocity;
148  var ang = this._rigidbody.velocity;
149 
150  var v = simulator_configuration.ConfigurableValue;
151  if (this.VelocitySpace.DecimalGranularity >= 0) {
152  v = (int)Math.Round(v, this.VelocitySpace.DecimalGranularity);
153  }
154 
155  if (this.VelocitySpace._Min_Values[0].CompareTo(this.VelocitySpace._Max_Values[0]) != 0) {
156  //TODO NOT IMPLEMENTED CORRECTLY VelocitySpace should not be index but should check all pairwise values, VelocitySpace._Min_Values == VelocitySpace._Max_Values
157  if (v < this.VelocitySpace._Min_Values[0] || v > this.VelocitySpace._Max_Values[0]) {
158  Debug.Log(string.Format("Configurable does not accept input{2}, outside allowed range {0} to {1}",
159  this.VelocitySpace._Min_Values[0],
160  this.VelocitySpace._Max_Values[0],
161  v));
162  return; // Do nothing
163  }
164  }
165 
166  #if NEODROID_DEBUG
167  if (this.Debugging) {
168  Debug.Log("Applying " + v + " To " + this.Identifier);
169  }
170  #endif
171 
172  if (this.RelativeToExistingValue) {
173  if (simulator_configuration.ConfigurableName == this._vel_x) {
174  vel.Set(v - vel.x, vel.y, vel.z);
175  } else if (simulator_configuration.ConfigurableName == this._vel_y) {
176  vel.Set(vel.x, v - vel.y, vel.z);
177  } else if (simulator_configuration.ConfigurableName == this._vel_z) {
178  vel.Set(vel.x, vel.y, v - vel.z);
179  } else if (simulator_configuration.ConfigurableName == this._ang_x) {
180  ang.Set(v - ang.x, ang.y, ang.z);
181  } else if (simulator_configuration.ConfigurableName == this._ang_y) {
182  ang.Set(ang.x, v - ang.y, ang.z);
183  } else if (simulator_configuration.ConfigurableName == this._ang_z) {
184  ang.Set(ang.x, ang.y, v - ang.z);
185  }
186  } else {
187  if (simulator_configuration.ConfigurableName == this._vel_x) {
188  vel.Set(v, vel.y, vel.z);
189  } else if (simulator_configuration.ConfigurableName == this._vel_y) {
190  vel.Set(vel.x, v, vel.z);
191  } else if (simulator_configuration.ConfigurableName == this._vel_z) {
192  vel.Set(vel.x, vel.y, v);
193  } else if (simulator_configuration.ConfigurableName == this._ang_x) {
194  ang.Set(v, ang.y, ang.z);
195  } else if (simulator_configuration.ConfigurableName == this._ang_y) {
196  ang.Set(ang.x, v, ang.z);
197  } else if (simulator_configuration.ConfigurableName == this._ang_z) {
198  ang.Set(ang.x, ang.y, v);
199  }
200  }
201 
202  this._rigidbody.velocity = vel;
203  this._rigidbody.angularVelocity = ang;
204  }
205 
211  public override Configuration[] SampleConfigurations() {
212  return new[] {new Configuration(this._ang_x, Space1.ZeroOne.Sample())};
213  }
214  }
215 }
override void ApplyConfiguration(IConfigurableConfiguration simulator_configuration)