Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
RigidbodyActuator.cs
Go to the documentation of this file.
4 using UnityEngine;
5 
6 namespace droid.Runtime.Prototyping.Actuators {
10  [AddComponentMenu(ActuatorComponentMenuPath._ComponentMenuPath
11  + "Rigidbody"
12  + ActuatorComponentMenuPath._Postfix)]
13  [RequireComponent(typeof(Rigidbody))]
14  public class RigidbodyActuator : Actuator {
17  [SerializeField]
18  protected ForceMode _ForceMode = ForceMode.Force;
19 
22  [SerializeField]
23  protected Space _Relative_To = Space.Self;
24 
27  [SerializeField]
28  protected Rigidbody _Rigidbody;
29 
30  string _rot_x;
31  string _rot_y;
32  string _rot_z;
33 
34  string _x;
35  string _y;
36  string _z;
37 
38  public override string PrototypingTypeName { get { return "Rigidbody"; } }
39 
43  protected override void Setup() {
44  this._Rigidbody = this.GetComponent<Rigidbody>();
45 
46  this._x = this.Identifier + "X_";
47  this._y = this.Identifier + "Y_";
48  this._z = this.Identifier + "Z_";
49  this._rot_x = this.Identifier + "RotX_";
50  this._rot_y = this.Identifier + "RotY_";
51  this._rot_z = this.Identifier + "RotZ_";
52  }
53 
56  protected override void RegisterComponent() {
57  //this.ParentActor = NeodroidUtilities.RegisterComponent((IHasRegister<IActuator>)this.ParentActor, (Actuator)this);
58 
59  this.Parent =
60  NeodroidUtilities.RegisterComponent((IHasRegister<IActuator>)this.Parent, (Actuator)this, this._x);
61  this.Parent =
62  NeodroidUtilities.RegisterComponent((IHasRegister<IActuator>)this.Parent, (Actuator)this, this._y);
63  this.Parent =
64  NeodroidUtilities.RegisterComponent((IHasRegister<IActuator>)this.Parent, (Actuator)this, this._z);
65  this.Parent =
66  NeodroidUtilities.RegisterComponent((IHasRegister<IActuator>)this.Parent, (Actuator)this, this._rot_x);
67  this.Parent =
68  NeodroidUtilities.RegisterComponent((IHasRegister<IActuator>)this.Parent, (Actuator)this, this._rot_y);
69  this.Parent =
70  NeodroidUtilities.RegisterComponent((IHasRegister<IActuator>)this.Parent, (Actuator)this, this._rot_z);
71  }
72 
76  protected override void InnerApplyMotion(IMotion motion) {
77  if (this._Relative_To == Space.World) {
78  if (motion.ActuatorName == this._x) {
79  this._Rigidbody.AddForce(Vector3.left * motion.Strength, this._ForceMode);
80  } else if (motion.ActuatorName == this._y) {
81  this._Rigidbody.AddForce(Vector3.up * motion.Strength, this._ForceMode);
82  } else if (motion.ActuatorName == this._z) {
83  this._Rigidbody.AddForce(Vector3.forward * motion.Strength, this._ForceMode);
84  } else if (motion.ActuatorName == this._rot_x) {
85  this._Rigidbody.AddTorque(Vector3.left * motion.Strength, this._ForceMode);
86  } else if (motion.ActuatorName == this._rot_y) {
87  this._Rigidbody.AddTorque(Vector3.up * motion.Strength, this._ForceMode);
88  } else if (motion.ActuatorName == this._rot_z) {
89  this._Rigidbody.AddTorque(Vector3.forward * motion.Strength, this._ForceMode);
90  }
91  } else if (this._Relative_To == Space.Self) {
92  if (motion.ActuatorName == this._x) {
93  this._Rigidbody.AddRelativeForce(Vector3.left * motion.Strength, this._ForceMode);
94  } else if (motion.ActuatorName == this._y) {
95  this._Rigidbody.AddRelativeForce(Vector3.up * motion.Strength, this._ForceMode);
96  } else if (motion.ActuatorName == this._z) {
97  this._Rigidbody.AddRelativeForce(Vector3.forward * motion.Strength, this._ForceMode);
98  } else if (motion.ActuatorName == this._rot_x) {
99  this._Rigidbody.AddRelativeTorque(Vector3.left * motion.Strength, this._ForceMode);
100  } else if (motion.ActuatorName == this._rot_y) {
101  this._Rigidbody.AddRelativeTorque(Vector3.up * motion.Strength, this._ForceMode);
102  } else if (motion.ActuatorName == this._rot_z) {
103  this._Rigidbody.AddRelativeTorque(Vector3.forward * motion.Strength, this._ForceMode);
104  }
105  } else {
106  Debug.LogWarning($"Not applying force in space {this._Relative_To}");
107  }
108  }
109  }
110 }