Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
RigidBody.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using SiliconStudio.Core.Collections;
4 using SiliconStudio.Core.Mathematics;
5 
6 namespace SiliconStudio.Paradox.Physics
7 {
8  public class RigidBody : Collider
9  {
10  internal delegate void GetWorldTransformDelegate(out Matrix transform);
11  internal GetWorldTransformDelegate GetWorldTransformCallback;
12 
13  internal delegate void SetWorldTransformDelegate(Matrix transform);
14  internal SetWorldTransformDelegate SetWorldTransformCallback;
15 
16  internal ParadoxMotionState MotionState;
17 
18  internal RigidBody(ColliderShape collider)
19  : base(collider)
20  {
21  LinkedConstraints = new FastList<Constraint>();
22  MotionState = new ParadoxMotionState(this);
23  }
24 
25  /// <summary>
26  /// Releases unmanaged and - optionally - managed resources.
27  /// </summary>
28  public override void Dispose()
29  {
30  LinkedConstraints.Clear();
31  MotionState.Dispose();
32  base.Dispose();
33  }
34 
35  float mass;
36  /// <summary>
37  /// Gets or sets the mass.
38  /// </summary>
39  /// <value>
40  /// The mass.
41  /// </value>
42  public float Mass
43  {
44  get
45  {
46  return mass;
47  }
48  set
49  {
50  mass = value;
51  var inertia = ColliderShape.InternalShape.CalculateLocalInertia(value);
52  InternalRigidBody.SetMassProps(value, inertia);
53  InternalRigidBody.UpdateInertiaTensor(); //this was the major headache when I had to debug Slider and Hinge constraint
54  }
55  }
56 
57  /// <summary>
58  /// Gets or sets the angular damping.
59  /// </summary>
60  /// <value>
61  /// The angular damping.
62  /// </value>
63  public float AngularDamping
64  {
65  get
66  {
67  return InternalRigidBody.AngularDamping;
68  }
69  set
70  {
71  InternalRigidBody.SetDamping(LinearDamping, value);
72  }
73  }
74 
75  /// <summary>
76  /// Gets or sets the linear damping.
77  /// </summary>
78  /// <value>
79  /// The linear damping.
80  /// </value>
81  public float LinearDamping
82  {
83  get
84  {
85  return InternalRigidBody.LinearDamping;
86  }
87  set
88  {
89  InternalRigidBody.SetDamping(value, AngularDamping);
90  }
91  }
92 
93  /// <summary>
94  /// Gets or sets the gravity for this single rigid body overriding the engine.
95  /// </summary>
96  /// <value>
97  /// The gravity.
98  /// </value>
99  public Vector3 Gravity
100  {
101  get { return InternalRigidBody.Gravity; }
102  set { InternalRigidBody.Gravity = value; }
103  }
104 
105  /// <summary>
106  /// Gets the total torque.
107  /// </summary>
108  /// <value>
109  /// The total torque.
110  /// </value>
111  public Vector3 TotalTorque
112  {
113  get { return InternalRigidBody.TotalTorque; }
114  }
115 
116  /// <summary>
117  /// Applies the impulse.
118  /// </summary>
119  /// <param name="impulse">The impulse.</param>
120  public void ApplyImpulse(Vector3 impulse)
121  {
122  InternalRigidBody.ApplyCentralImpulse(impulse);
123  }
124 
125  /// <summary>
126  /// Applies the impulse.
127  /// </summary>
128  /// <param name="impulse">The impulse.</param>
129  /// <param name="localOffset">The local offset.</param>
130  public void ApplyImpulse(Vector3 impulse, Vector3 localOffset)
131  {
132  InternalRigidBody.ApplyImpulse(impulse, localOffset);
133  }
134 
135  /// <summary>
136  /// Applies the force.
137  /// </summary>
138  /// <param name="force">The force.</param>
139  public void ApplyForce(Vector3 force)
140  {
141  InternalRigidBody.ApplyCentralForce(force);
142  }
143 
144  /// <summary>
145  /// Applies the force.
146  /// </summary>
147  /// <param name="force">The force.</param>
148  /// <param name="localOffset">The local offset.</param>
149  public void ApplyForce(Vector3 force, Vector3 localOffset)
150  {
151  InternalRigidBody.ApplyForce(force, localOffset);
152  }
153 
154  /// <summary>
155  /// Applies the torque.
156  /// </summary>
157  /// <param name="torque">The torque.</param>
158  public void ApplyTorque(Vector3 torque)
159  {
160  InternalRigidBody.ApplyTorque(torque);
161  }
162 
163  /// <summary>
164  /// Applies the torque impulse.
165  /// </summary>
166  /// <param name="torque">The torque.</param>
167  public void ApplyTorqueImpulse(Vector3 torque)
168  {
169  InternalRigidBody.ApplyTorqueImpulse(torque);
170  }
171 
172  /// <summary>
173  /// Gets or sets the angular velocity.
174  /// </summary>
175  /// <value>
176  /// The angular velocity.
177  /// </value>
178  public Vector3 AngularVelocity
179  {
180  get { return InternalRigidBody.AngularVelocity; }
181  set { InternalRigidBody.AngularVelocity = value; }
182  }
183 
184  /// <summary>
185  /// Gets or sets the linear velocity.
186  /// </summary>
187  /// <value>
188  /// The linear velocity.
189  /// </value>
190  public Vector3 LinearVelocity
191  {
192  get { return InternalRigidBody.LinearVelocity; }
193  set { InternalRigidBody.LinearVelocity = value; }
194  }
195 
196  /// <summary>
197  /// Gets the total force.
198  /// </summary>
199  /// <value>
200  /// The total force.
201  /// </value>
202  public Vector3 TotalForce
203  {
204  get { return InternalRigidBody.TotalForce; }
205  }
206 
207  /// <summary>
208  /// Gets or sets the angular factor.
209  /// </summary>
210  /// <value>
211  /// The angular factor.
212  /// </value>
213  public Vector3 AngularFactor
214  {
215  get { return InternalRigidBody.AngularFactor; }
216  set { InternalRigidBody.AngularFactor = value; }
217  }
218 
219  /// <summary>
220  /// Gets or sets the linear factor.
221  /// </summary>
222  /// <value>
223  /// The linear factor.
224  /// </value>
225  public Vector3 LinearFactor
226  {
227  get { return InternalRigidBody.LinearFactor; }
228  set { InternalRigidBody.LinearFactor = value; }
229  }
230 
231  /// <summary>
232  /// Gets or sets the type.
233  /// </summary>
234  /// <value>
235  /// The type.
236  /// </value>
237  public RigidBodyTypes Type
238  {
239  get
240  {
241  if (InternalRigidBody.CollisionFlags.HasFlag(BulletSharp.CollisionFlags.StaticObject)) return RigidBodyTypes.Static;
242  return InternalRigidBody.CollisionFlags.HasFlag(BulletSharp.CollisionFlags.KinematicObject) ? RigidBodyTypes.Kinematic : RigidBodyTypes.Dynamic;
243  }
244  set
245  {
246  switch (value)
247  {
248  case RigidBodyTypes.Dynamic:
249  if (InternalRigidBody.CollisionFlags.HasFlag(BulletSharp.CollisionFlags.StaticObject)) InternalRigidBody.CollisionFlags ^= BulletSharp.CollisionFlags.StaticObject;
250  else if (InternalRigidBody.CollisionFlags.HasFlag(BulletSharp.CollisionFlags.KinematicObject)) InternalRigidBody.CollisionFlags ^= BulletSharp.CollisionFlags.KinematicObject;
251  break;
252  case RigidBodyTypes.Static:
253  if (InternalRigidBody.CollisionFlags.HasFlag(BulletSharp.CollisionFlags.KinematicObject)) InternalRigidBody.CollisionFlags ^= BulletSharp.CollisionFlags.KinematicObject;
254  InternalRigidBody.CollisionFlags |= BulletSharp.CollisionFlags.StaticObject;
255  break;
256  case RigidBodyTypes.Kinematic:
257  if (InternalRigidBody.CollisionFlags.HasFlag(BulletSharp.CollisionFlags.StaticObject)) InternalRigidBody.CollisionFlags ^= BulletSharp.CollisionFlags.StaticObject;
258  InternalRigidBody.CollisionFlags |= BulletSharp.CollisionFlags.KinematicObject;
259  break;
260  }
261  }
262  }
263 
264  /// <summary>
265  /// Gets the linked constraints.
266  /// </summary>
267  /// <value>
268  /// The linked constraints.
269  /// </value>
270  public FastList<Constraint> LinkedConstraints { get; private set; }
271 
272  internal BulletSharp.RigidBody InternalRigidBody;
273  }
274 }
void ApplyTorque(Vector3 torque)
Applies the torque.
Definition: RigidBody.cs:158
void ApplyImpulse(Vector3 impulse, Vector3 localOffset)
Applies the impulse.
Definition: RigidBody.cs:130
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
void ApplyTorqueImpulse(Vector3 torque)
Applies the torque impulse.
Definition: RigidBody.cs:167
void ApplyForce(Vector3 force, Vector3 localOffset)
Applies the force.
Definition: RigidBody.cs:149
using SiliconStudio.Paradox. Physics
void ApplyImpulse(Vector3 impulse)
Applies the impulse.
Definition: RigidBody.cs:120
override void Dispose()
Releases unmanaged and - optionally - managed resources.
Definition: RigidBody.cs:28
void ApplyForce(Vector3 force)
Applies the force.
Definition: RigidBody.cs:139
Represents a 4x4 mathematical matrix.
Definition: Matrix.cs:47