Atoms Crowd  7.0.0
RigidBody.h
1 #pragma once
2 #ifndef ATOMS_UNREAL
3 // ===========================================================================
4 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
5 //
6 // Use of this software is subject to the terms of the Toolchefs license
7 // agreement provided at the time of installation or download, or which
8 // otherwise accompanies this software in either electronic or hard copy form.
9 // ===========================================================================
10 
11 #include <Atoms/Globals.h>
12 #include <AtomsCore/AtomsMath.h>
13 #include <AtomsUtils/Mesh.h>
14 #include <AtomsCore/Metadata/MapMetadata.h>
15 #include <string>
16 #include <atomic>
17 #ifdef VS2012
18 #include <tbb/atomic.h>
19 #endif
20 
21 #define GETSET_RIGID_BODY_ATTRIBUTE( SET_NAME, GET_NAME, TYPE, ATTRIBUTE ) \
22 inline TYPE GET_NAME() const { return ATTRIBUTE; } \
23 void SET_NAME(TYPE value) { ATTRIBUTE = value; } \
24 
25 
26 namespace physx
27 {
28  class PxPhysics;
29  class PxScene;
30  class PxGeometry;
31  class PxRigidDynamic;
32  class PxRigidActor;
33  class PxTransform;
34  class PxCooking;
35 }
36 
37 namespace Atoms
38 {
39  class ATOMS_EXPORT RigidBodyAttributes
40  {
41  public:
42 
43  enum State
44  {
45  kStatic = 0,
46  kDynamic = 1,
47  kKinematic = 2
48  };
49 
50  enum Type
51  {
52  kPlane = 0,
53  kSphere,
54  kBox,
55  kCapsule,
56  kConvexMesh,
57  kTriangleMesh,
58  kHeightField
59  };
60 
61  enum Axis
62  {
63  kX = 0,
64  kY,
65  kZ
66  };
67 
68  enum RotationOrder
69  {
70  kXYZ = 0,
71  kYZX,
72  kZXY,
73  kXZY,
74  kYXZ,
75  kZYX,
76  };
77 
79 
81 
82  public:
83 
85  bool serialise(std::string& outStream) const;
86 
88  bool deserialise(const std::string& inStream);
89 
91  bool serialise(AtomsCore::MapMetadata* outStream) const;
92 
94  bool deserialise(const AtomsCore::MapMetadata* inStream);
95 
96  public:
97 
98  static const size_t version;
99 
100  public:
101  AtomsCore::Vector3f dimension;
102  AtomsCore::Vector3f linearVelocity;
103  AtomsCore::Vector3f angularVelocity;
104  AtomsCore::Vector3f massSpaceInertiaTensor;
105  AtomsCore::Matrixf transform;
106  AtomsCore::Matrixf shapeOffset;
107  AtomsCore::Matrixf cMassLocalPose;
108  AtomsCore::Vector3f rotatePivot;
109  AtomsCore::Vector3f scalePivot;
110  AtomsCore::Vector3f scalePivotTranslate;
111  AtomsCore::Vector3f rotatePivotTranslate;
112  AtomsCore::Vector3f scale;
113  AtomsCore::Vector3f position;
114  AtomsCore::Quaternionf rotation;
115  AtomsCore::Vector2f stuckOnCollisionBreakForce;
116  AtomsCore::Vector2f solverIteratorCount;
117  float collisionForceThreshold;
118  float maxAngularVelocity;
119  float angularDamping;
120  float linearDamping;
121  float density;
122  float sleepThreshold;
123  float stabilizationThreshold;
124  float wakeCounter;
125  float mass;
126  float staticFriction;
127  float dynamicFriction;
128  float restitution;
129  float restOffset;
130  float contactOffset;
131  float contactReportThreshold;
132  float stuckOnCollisionImpulse;
133  float heightFieldScale;
134  int aggregate;
135  int collisionPool;
136  int dynamicOnCollisionPool;
137  int stuckOnCollisionPool;
138  int heightFieldRows;
139  int heightFieldColumns;
140  short state;
141  short type;
142  short axis;
143  short rotationOrder;
144  bool active;
145  bool useMass;
146  bool dynamicOnCollision;
147  bool disableCollisionEvent;
148  bool stuckOnCollision;
149  bool aggregateSelfCollision;
150  bool storeImpactData;
151  bool deformable;
152  };
153 
154  class ATOMS_EXPORT RigidBody
155  {
156  public:
157 
159  {
160  kSphere = 0,
161  kPlane,
162  kCapsule,
163  kBox,
164  kConvexMesh,
165  kTriangleMesh,
166  kHeightfield,
168  kInvalid = -1
169  };
170 
171  struct ShapeData
172  {
173  AtomsCore::Matrixf transform;
174  short shapeType;
175  AtomsCore::Vector3f size;
176  std::vector<AtomsCore::Vector3f> points;
177  std::vector<AtomsCore::Vector3f> normals;
178  std::vector<unsigned int> indices;
179  };
180 
181  struct ImpactData
182  {
183  AtomsCore::Vector3f position;
184  AtomsCore::Vector3f force;
185  int asImpulse;
186  double time;
187  size_t obj;
188  };
189 
190  enum DynamicOnCollisionType
191  {
192  kSingle = 0,
193  kAggregate,
194  kChildren
195  };
196 
197  RigidBody();
198 
199  ~RigidBody();
200 
201  void setKinematicTarget(const AtomsCore::Matrixf& mtx);
202 
203  void storeTransform();
204 
205  void setKinematic(bool value);
206 
207  void pose(AtomsCore::Vector3f& pos, AtomsCore::Quaternionf& rot) const;
208 
209  void getGeometry(std::vector<AtomsCore::Vector3f>& points,
210  std::vector<AtomsCore::Vector3f>& normals,
211  std::vector<unsigned int>& indices) const;
212 
213  void getShape(ShapeData& data) const;
214 
215  inline bool isStatic() const;
216 
217  inline bool isKinematic() const;
218 
219  inline bool isDynamic() const;
220 
221  void addForce(const AtomsCore::Vector3f& force);
222 
223  void addImpulse(const AtomsCore::Vector3f& impulse);
224 
225  void addForceAtPosition(const AtomsCore::Vector3f& force, const AtomsCore::Vector3f& position);
226 
227  void addImpulseAtPosition(const AtomsCore::Vector3f& impulse, const AtomsCore::Vector3f& position);
228 
229  void addTorque(const AtomsCore::Vector3f& force);
230 
231  void addTorqueImpulse(const AtomsCore::Vector3f& impulse);
232 
233  void applyForces(float scale);
234 
235  void clearForces();
236 
237  void clearFeedbacks();
238 
239  AtomsCore::Matrixf centerOfMass();
240 
241  float friction();
242 
243  float bounce();
244 
245  void release();
246 
247  bool updateMeshPoints(const std::vector<AtomsCore::Vector3f>& newPoints);
248 
249  public:
250 
251  std::vector<ImpactData> impacts;
252 
253  std::vector<ImpactData> feedback;
254 
255  physx::PxRigidActor* actor;
256 
257  short state;
258 
259  short rotationOrder;
260 
261  AtomsCore::Quaternionf targetRot;
262 
263  AtomsCore::Quaternionf currentRot;
264 
265  AtomsCore::Vector3f targetPos;
266 
267  AtomsCore::Vector3f currentPos;
268 
269  AtomsCore::Vector3f rotatePivot;
270 
271  AtomsCore::Vector3f scale;
272 
273  AtomsCore::Vector3f scalePivot;
274 
275  AtomsCore::Vector3f scalePivotTranslate;
276 
277  AtomsCore::Vector3f rotatePivotTranslate;
278 
279  AtomsCore::Matrixf initTransform;
280 
281  AtomsCore::Vector3f initPosition;
282 
283  AtomsCore::Quaternionf initRotation;
284 #ifndef VS2012
285  std::atomic<bool> dynamicSwitch;
286 
287  std::atomic<int> dynamicPool;
288 #else
289  tbb::atomic<bool> dynamicSwitch;
290 
291  tbb::atomic<int> dynamicPool;
292 #endif
293  AtomsCore::Vector3f force;
294 
295  AtomsCore::Vector3f impulse;
296 
297  AtomsCore::Vector3f torque;
298 
299  AtomsCore::Vector3f torqueImpulse;
300 
301  AtomsCore::Vector3f velocity;
302 
303  AtomsCore::Vector3f angularVelocity;
304 
305  AtomsCore::Vector3f acceleration;
306 
307  float mass;
308 
309  size_t id;
310 
311  short dynamicOnCollisionType;
312 
313  int aggregate;
314 
315  float stuckOnCollisionImpulse;
316 
317  AtomsCore::Vector2f stuckOnCollisionBreakForce;
318 
319  float collisionForceThreshold;
320 
321  int dynamicOnCollisionPool;
322 
323  int stuckOnCollisionPool;
324 
325  int collisionPool;
326 
327  bool dynamicOnCollision;
328 
329  bool isRoot;
330 
331  bool stuckOnCollision;
332 
333  bool aggregateSelfCollision;
334 
335  bool isJoint;
336 
337  bool storeImpactData;
338 
339  bool deformable;
340  };
341 
342 }
343 
344 #include "RigidBody.impl.h"
345 #endif
Definition: RigidBody.h:40
bool serialise(std::string &outStream) const
Binary Serialise.
bool deserialise(const AtomsCore::MapMetadata *inStream)
Binary Deserialise.
bool serialise(AtomsCore::MapMetadata *outStream) const
Binary Serialise.
bool deserialise(const std::string &inStream)
Binary Deserialise.
Definition: RigidBody.h:155
ShapeType
Definition: RigidBody.h:159
@ kGeometryCount
internal use only!
Definition: RigidBody.h:167
MapMetadata class.
Definition: MapMetadata.h:24
AtomsMath::Matrixf Matrixf
Matrix class.
Definition: AtomsMath.h:64
AtomsMath::Vector2f Vector2f
Vector2 class.
Definition: AtomsMath.h:55
AtomsMath::Vector3f Vector3f
Vector3 class.
Definition: AtomsMath.h:58
AtomsMath::Quaternionf Quaternionf
Quaternion class.
Definition: AtomsMath.h:68
Atoms namespace.
Definition: Agent.h:29
Definition: RigidBody.h:182
Definition: RigidBody.h:172