Leviathan  0.8.0.0
Leviathan game engine
Leviathan::PhysicsBody Class Reference

This is an instance of a collision body. More...

#include <PhysicsBody.h>

+ Inheritance diagram for Leviathan::PhysicsBody:

Public Member Functions

DLLEXPORT ~PhysicsBody ()
 
DLLEXPORT void GiveImpulse (const Float3 &deltaspeed, const Float3 &point=Float3(0))
 
DLLEXPORT void SetVelocity (const Float3 &velocities)
 Overrides this objects velocity in ApplyForceAndTorqueEvent. More...
 
DLLEXPORT void ClearVelocity ()
 Clears velocity and last frame forces (but not the applied force list) More...
 
DLLEXPORT Float3 GetVelocity () const
 Gets the absolute velocity. More...
 
DLLEXPORT Float3 GetAngularVelocity () const
 Gets the omega (angular velocity) More...
 
DLLEXPORT void SetAngularVelocity (const Float3 &velocities)
 Sets the omega. More...
 
DLLEXPORT void ApplyTorque (const Float3 &torque)
 Adds to the torque of this object. More...
 
DLLEXPORT Float3 GetTorque () const
 Gets the torque of the body (rotational velocity) More...
 
DLLEXPORT void SetPhysicalMaterialID (int id)
 Sets the physical material ID of this object. More...
 
DLLEXPORT void SetDamping (float linear, float angular)
 Sets the linear dampening which slows down the object and angular dampening. More...
 
DLLEXPORT void SetFriction (float friction)
 Sets the friction on this body. More...
 
DLLEXPORT bool SetPosition (const Float3 &pos, const Float4 &orientation)
 Moves the physical body to the specified position. More...
 
DLLEXPORT Float3 GetPosition () const
 
DLLEXPORT bool SetOnlyOrientation (const Float4 &orientation)
 Same as SetPosition but only sets orientation. More...
 
DLLEXPORT void SetMass (float mass)
 Calculates the mass matrix and applies the mass parameter to the body. More...
 
DLLEXPORT float GetMass () const
 
DLLEXPORT void ConstraintMovementAxises (const Float3 &movement=Float3(1, 0, 1), const Float3 &rotation=Float3(0, 1, 0))
 Adds a constraint to the current Body to only move on specified axises. More...
 
DLLEXPORT void ApplyMaterial (PhysicalMaterial &material)
 Applies material properties. More...
 
DLLEXPORT btRigidBody * GetBody ()
 
PhysicsShapeGetShape () const
 
auto GetPhysicalMaterialID () const
 
DLLEXPORT auto GetOwningEntity () const
 Data for helping with callbacks. More...
 
DLLEXPORT void SetOwningEntity (ObjectID entity)
 
DLLEXPORT const auto & GetConstraints () const
 
PhysicsShapeGetShapeWrapper () const
 
 REFERENCE_COUNTED_PTR_TYPE (PhysicsBody)
 
- Public Member Functions inherited from Leviathan::ReferenceCounted
 ReferenceCounted (const ReferenceCounted &other)=delete
 
ReferenceCountedoperator= (const ReferenceCounted &other)=delete
 
FORCE_INLINE void AddRef () const
 
FORCE_INLINE void Release () const
 removes a reference and deletes the object if reference count reaches zero More...
 
FORCE_INLINE void AddRef ()
 
FORCE_INLINE void Release ()
 
int32_t GetRefCount () const
 Returns the reference count. More...
 

Protected Member Functions

DLLEXPORT PhysicsBody (std::unique_ptr< btRigidBody > &&body, float mass, const PhysicsShape::pointer &shape, std::unique_ptr< PhysicsDataBridge > &&positionsynchronization, int materialid)
 
DLLEXPORT void DetachResources ()
 This releases all resources held by this. This is called by the PhysicalWorld when this is destroyed or the world wants to be destroyed but there are still external references to this. More...
 
DLLEXPORT void ApplyShapeChange (const PhysicsShape::pointer &shape)
 Applies shape change. More...
 
DLLEXPORT void NotifyAttachedConstraint (PhysicsConstraint *constraint)
 Called by PhysicsConstraint constructor. More...
 
DLLEXPORT void NotifyDetachedConstraint (PhysicsConstraint *constraint)
 Called by PhysicsConstraint when detaching resources. More...
 
- Protected Member Functions inherited from Leviathan::ReferenceCounted
DLLEXPORT ReferenceCounted ()
 
virtual DLLEXPORT ~ReferenceCounted ()
 

Protected Attributes

friend ReferenceCounted
 

Friends

class PhysicalWorld
 

Additional Inherited Members

- Public Types inherited from Leviathan::ReferenceCounted
using basepointer = boost::intrusive_ptr< ReferenceCounted >
 
using refcountedpointer = boost::intrusive_ptr< ReferenceCounted >
 
- Static Public Member Functions inherited from Leviathan::ReferenceCounted
template<class ActualType >
static boost::intrusive_ptr< ActualType > WrapPtr (ActualType *ptr)
 Creates an intrusive_ptr from raw pointer. More...
 
template<class ActualType , class... Args>
static boost::intrusive_ptr< ActualType > MakeShared (Args &&... args)
 Constructs a new instance and wraps it. More...
 

Detailed Description

This is an instance of a collision body.

Definition at line 76 of file PhysicsBody.h.

Constructor & Destructor Documentation

◆ PhysicsBody()

DLLEXPORT PhysicsBody::PhysicsBody ( std::unique_ptr< btRigidBody > &&  body,
float  mass,
const PhysicsShape::pointer &  shape,
std::unique_ptr< PhysicsDataBridge > &&  positionsynchronization,
int  materialid 
)
protected

Definition at line 13 of file PhysicsBody.cpp.

15  :
16  Body(std::move(body)),
17  Mass(mass), Shape(shape), PositionUpdate(std::move(positionsynchronization)),
18  PhysicalMaterialID(materialid)
19 {
20  if(!Body)
21  throw InvalidArgument("null rigid body passed to PhysicsBody");
22 
23  // Add this as user data //
24  Body->setUserPointer(this);
25 }

◆ ~PhysicsBody()

DLLEXPORT PhysicsBody::~PhysicsBody ( )

Definition at line 27 of file PhysicsBody.cpp.

28 {
29  if(Body) {
30  LOG_ERROR("PhysicsBody: destroyed before being removed from a world");
32  }
33 
34  if(!PartOfConstraints.empty())
35  LOG_ERROR("PhysicsBody: destroyed while having active constraints");
36 }
#define LOG_ERROR(x)
Definition: Define.h:96
DLLEXPORT void DetachResources()
This releases all resources held by this. This is called by the PhysicalWorld when this is destroyed ...
Definition: PhysicsBody.cpp:38

Member Function Documentation

◆ ApplyMaterial()

DLLEXPORT void PhysicsBody::ApplyMaterial ( PhysicalMaterial material)

Applies material properties.

Todo:
Add in the properties that make sense as there currently is none

Definition at line 312 of file PhysicsBody.cpp.

313 {
314  if(!Body)
315  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
316 
317  // TODO: properties
318 }

◆ ApplyShapeChange()

DLLEXPORT void PhysicsBody::ApplyShapeChange ( const PhysicsShape::pointer &  shape)
protected

Applies shape change.

Definition at line 48 of file PhysicsBody.cpp.

49 {
50  if(!shape)
51  return;
52 
53  // This is only called after the physical world has verified that this has a body
54  Body->setCollisionShape(shape->GetShape());
55  Shape = shape;
56 
57  // Recalculate inertia
58  const bool isDynamic = Mass > 0;
59 
60  btVector3 localInertia(0, 0, 0);
61  if(isDynamic)
62  Shape->GetShape()->calculateLocalInertia(Mass, localInertia);
63 
64  Body->setMassProps(Mass, localInertia);
65 
66  // TODO: is Body->updateInertiaTensor() needed here? or is the removing and adding this
67  // back to the world good enough?
68 }

◆ ApplyTorque()

DLLEXPORT void PhysicsBody::ApplyTorque ( const Float3 torque)

Adds to the torque of this object.

Definition at line 239 of file PhysicsBody.cpp.

240 {
241  if(!Body)
242  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
243 
244 
245  // Safety check. Can be disabled in release builds if this is a performance issue
246  if(torque.HasInvalidValues()) {
247 
248  std::stringstream msg;
249  msg << "ApplyTorque call had at least one value with non-finite value, torque: "
250  << Convert::ToString(torque);
251  LOG_ERROR("PhysicsBody: " + msg.str());
252  throw InvalidArgument(msg.str());
253  }
254 
255  Body->applyTorque(torque);
256 }
#define LOG_ERROR(x)
Definition: Define.h:96
DLLEXPORT bool HasInvalidValues() const noexcept
Definition: Types.h:903
static std::string ToString(const T &val)
Definition: Convert.h:72

◆ ClearVelocity()

DLLEXPORT void PhysicsBody::ClearVelocity ( )

Clears velocity and last frame forces (but not the applied force list)

Definition at line 183 of file PhysicsBody.cpp.

184 {
185  if(!Body)
186  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
187 
188 
189  Body->setAngularVelocity(btVector3(0, 0, 0));
190  Body->setLinearVelocity(btVector3(0, 0, 0));
191  Body->clearForces();
192 }

◆ ConstraintMovementAxises()

DLLEXPORT void PhysicsBody::ConstraintMovementAxises ( const Float3 movement = Float3(1, 0, 1),
const Float3 rotation = Float3(0, 1, 0) 
)

Adds a constraint to the current Body to only move on specified axises.

This uses the bullet linear factor and replaces

Definition at line 302 of file PhysicsBody.cpp.

304 {
305  if(!Body)
306  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
307 
308  Body->setLinearFactor(movement);
309  Body->setAngularFactor(rotation);
310 }

◆ DetachResources()

DLLEXPORT void PhysicsBody::DetachResources ( )
protected

This releases all resources held by this. This is called by the PhysicalWorld when this is destroyed or the world wants to be destroyed but there are still external references to this.

Definition at line 38 of file PhysicsBody.cpp.

39 {
40  Body->setUserPointer(nullptr);
41  Body->setMotionState(nullptr);
42  Body.reset();
43  PositionUpdate.reset();
44 
45  Shape.reset();
46 }

◆ GetAngularVelocity()

DLLEXPORT Float3 PhysicsBody::GetAngularVelocity ( ) const

Gets the omega (angular velocity)

Definition at line 202 of file PhysicsBody.cpp.

203 {
204  if(!Body)
205  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
206 
207  return Body->getAngularVelocity();
208 }

◆ GetBody()

DLLEXPORT btRigidBody* Leviathan::PhysicsBody::GetBody ( )
inline

Definition at line 166 of file PhysicsBody.h.

167  {
168  return Body.get();
169  }

◆ GetConstraints()

DLLEXPORT const auto& Leviathan::PhysicsBody::GetConstraints ( ) const
inline

Definition at line 192 of file PhysicsBody.h.

193  {
194  return PartOfConstraints;
195  }

◆ GetMass()

DLLEXPORT float Leviathan::PhysicsBody::GetMass ( ) const
inline

Definition at line 151 of file PhysicsBody.h.

152  {
153  return Mass;
154  }

◆ GetOwningEntity()

DLLEXPORT auto Leviathan::PhysicsBody::GetOwningEntity ( ) const
inline

Data for helping with callbacks.

Definition at line 182 of file PhysicsBody.h.

183  {
184  return ThisEntity;
185  }

◆ GetPhysicalMaterialID()

auto Leviathan::PhysicsBody::GetPhysicalMaterialID ( ) const
inline

Definition at line 176 of file PhysicsBody.h.

177  {
178  return PhysicalMaterialID;
179  }

◆ GetPosition()

DLLEXPORT Float3 PhysicsBody::GetPosition ( ) const
Returns
The current physical body position

Definition at line 112 of file PhysicsBody.cpp.

113 {
114  if(!Body)
115  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
116 
117  return Body->getCenterOfMassTransform().getOrigin();
118 }

◆ GetShape()

PhysicsShape* Leviathan::PhysicsBody::GetShape ( ) const
inline

Definition at line 171 of file PhysicsBody.h.

172  {
173  return Shape.get();
174  }

◆ GetShapeWrapper()

PhysicsShape* Leviathan::PhysicsBody::GetShapeWrapper ( ) const
inline

Definition at line 200 of file PhysicsBody.h.

201  {
202  if(Shape)
203  Shape->AddRef();
204  return Shape.get();
205  }

◆ GetTorque()

DLLEXPORT Float3 PhysicsBody::GetTorque ( ) const

Gets the torque of the body (rotational velocity)

Definition at line 230 of file PhysicsBody.cpp.

231 {
232  if(!Body)
233  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
234 
235 
236  return Body->getTotalTorque();
237 }

◆ GetVelocity()

DLLEXPORT Float3 PhysicsBody::GetVelocity ( ) const

Gets the absolute velocity.

Definition at line 194 of file PhysicsBody.cpp.

195 {
196  if(!Body)
197  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
198 
199  return Body->getLinearVelocity();
200 }

◆ GiveImpulse()

DLLEXPORT void PhysicsBody::GiveImpulse ( const Float3 deltaspeed,
const Float3 point = Float3(0) 
)
Parameters
pointIs relative position of the impulse given
Exceptions
InvalidArgumentif deltaspeed or point has non-finite values

Definition at line 144 of file PhysicsBody.cpp.

146 {
147  if(!Body)
148  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
149 
150  // Safety check. Can be disabled in release builds if this is a performance issue
151  if(deltaspeed.HasInvalidValues() || point.HasInvalidValues()) {
152 
153  std::stringstream msg;
154  msg << "GiveImpulse call had at least one value with non-finite value, deltaspeed: "
155  << Convert::ToString(deltaspeed) << " point: " << Convert::ToString(point);
156  LOG_ERROR("PhysicsBody: " + msg.str());
157  throw InvalidArgument(msg.str());
158  }
159 
160  Body->applyImpulse(deltaspeed, point);
161  // Maybe this needs to call activate() for the force to move it?
162 }
#define LOG_ERROR(x)
Definition: Define.h:96
DLLEXPORT bool HasInvalidValues() const noexcept
Definition: Types.h:903
static std::string ToString(const T &val)
Definition: Convert.h:72

◆ NotifyAttachedConstraint()

DLLEXPORT void PhysicsBody::NotifyAttachedConstraint ( PhysicsConstraint constraint)
protected

Called by PhysicsConstraint constructor.

Note
This doesn't protect against duplicates

Definition at line 70 of file PhysicsBody.cpp.

71 {
72  if(constraint)
73  PartOfConstraints.push_back(constraint);
74 }

◆ NotifyDetachedConstraint()

DLLEXPORT void PhysicsBody::NotifyDetachedConstraint ( PhysicsConstraint constraint)
protected

Called by PhysicsConstraint when detaching resources.

Definition at line 76 of file PhysicsBody.cpp.

77 {
78  for(auto iter = PartOfConstraints.begin(); iter != PartOfConstraints.end(); ++iter) {
79  if(iter->get() == constraint) {
80  PartOfConstraints.erase(iter);
81  return;
82  }
83  }
84 }

◆ REFERENCE_COUNTED_PTR_TYPE()

Leviathan::PhysicsBody::REFERENCE_COUNTED_PTR_TYPE ( PhysicsBody  )

◆ SetAngularVelocity()

DLLEXPORT void PhysicsBody::SetAngularVelocity ( const Float3 velocities)

Sets the omega.

Definition at line 210 of file PhysicsBody.cpp.

211 {
212  if(!Body)
213  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
214 
215 
216  // Safety check. Can be disabled in release builds if this is a performance issue
217  if(velocities.HasInvalidValues()) {
218 
219  std::stringstream msg;
220  msg << "SetAngularVelocity call had at least one value with non-finite value, "
221  "velocities: "
222  << Convert::ToString(velocities);
223  LOG_ERROR("PhysicsBody: " + msg.str());
224  throw InvalidArgument(msg.str());
225  }
226 
227  Body->setAngularVelocity(velocities);
228 }
#define LOG_ERROR(x)
Definition: Define.h:96
DLLEXPORT bool HasInvalidValues() const noexcept
Definition: Types.h:903
static std::string ToString(const T &val)
Definition: Convert.h:72

◆ SetDamping()

DLLEXPORT void PhysicsBody::SetDamping ( float  linear,
float  angular 
)

Sets the linear dampening which slows down the object and angular dampening.

Recommended value is 0.1f

Definition at line 258 of file PhysicsBody.cpp.

259 {
260  if(!Body)
261  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
262 
263  Body->setDamping(linear, angular);
264 }

◆ SetFriction()

DLLEXPORT void PhysicsBody::SetFriction ( float  friction)

Sets the friction on this body.

Definition at line 266 of file PhysicsBody.cpp.

267 {
268  if(!Body)
269  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
270 
271  Body->setFriction(friction);
272 }

◆ SetMass()

DLLEXPORT void PhysicsBody::SetMass ( float  mass)

Calculates the mass matrix and applies the mass parameter to the body.

Note
It is much more effective to calculate the mass first and then make a body instead of adjusting it later. But if the mass changes then this needs to be called.

Definition at line 274 of file PhysicsBody.cpp.

275 {
276  if(!Body)
277  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
278 
279 
280  // Safety check. Shouldn't be disabled
281  if(!std::isfinite(mass)) {
282 
283  std::stringstream msg;
284  msg << "SetMass mass is invalid value: " << Convert::ToString(mass)
285  << " (use 0 for immovable)";
286  LOG_ERROR("Physics: " + msg.str());
287  throw InvalidArgument(msg.str());
288  }
289 
290  Mass = mass;
291 
292  // First calculate inertia and center of mass points //
293  const bool isDynamic = Mass > 0;
294 
295  btVector3 localInertia(0, 0, 0);
296  if(isDynamic)
297  Shape->GetShape()->calculateLocalInertia(Mass, localInertia);
298 
299  Body->setMassProps(mass, localInertia);
300 }
#define LOG_ERROR(x)
Definition: Define.h:96
#define isfinite
Definition: jsoncpp.cpp:4247
static std::string ToString(const T &val)
Definition: Convert.h:72

◆ SetOnlyOrientation()

DLLEXPORT bool PhysicsBody::SetOnlyOrientation ( const Float4 orientation)

Same as SetPosition but only sets orientation.

Definition at line 120 of file PhysicsBody.cpp.

121 {
122  if(!Body)
123  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
124 
125  // Safety check. Can be disabled in release builds if this is a performance issue
126  if(orientation.HasInvalidValues()) {
127 
128  std::stringstream msg;
129  msg << "SetOnlyOrientation call had at least one value with non-finite value, "
130  "orientation: "
131  << Convert::ToString(orientation);
132  LOG_ERROR("PhysicsBody: " + msg.str());
133  throw InvalidArgument(msg.str());
134  }
135 
136  btTransform pos = Body->getCenterOfMassTransform();
137 
138  pos.setRotation(orientation);
139 
140  Body->setCenterOfMassTransform(pos);
141  return true;
142 }
#define LOG_ERROR(x)
Definition: Define.h:96
static std::string ToString(const T &val)
Definition: Convert.h:72
DLLEXPORT bool HasInvalidValues() const noexcept
Definition: Types.h:1275

◆ SetOwningEntity()

DLLEXPORT void Leviathan::PhysicsBody::SetOwningEntity ( ObjectID  entity)
inline

Definition at line 187 of file PhysicsBody.h.

188  {
189  ThisEntity = entity;
190  }

◆ SetPhysicalMaterialID()

DLLEXPORT void Leviathan::PhysicsBody::SetPhysicalMaterialID ( int  id)
inline

Sets the physical material ID of this object.

Note
You have to fetch the ID from the world's corresponding PhysicalMaterialManager
Todo:
There needs to be a physical world helper for actually applying the new properties

Definition at line 118 of file PhysicsBody.h.

119  {
120  PhysicalMaterialID = id;
121  }

◆ SetPosition()

DLLEXPORT bool PhysicsBody::SetPosition ( const Float3 pos,
const Float4 orientation 
)

Moves the physical body to the specified position.

Returns
False if this fails because there currently is no physics body for this component
Exceptions
InvalidArgumentif pos or orientation has non-finite values

Definition at line 86 of file PhysicsBody.cpp.

87 {
88  if(!Body)
89  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
90 
91  // Safety check. Can be disabled in release builds if this is a performance issue
92  if(pos.HasInvalidValues() || orientation.HasInvalidValues()) {
93 
94  std::stringstream msg;
95  msg << "SetPosition call had at least one value with non-finite value, pos: "
96  << Convert::ToString(pos) << " orientation: " << Convert::ToString(orientation);
97  LOG_ERROR("PhysicsBody: " + msg.str());
98  throw InvalidArgument(msg.str());
99  }
100 
101  btTransform transform;
102  // TODO: check is this required
103  transform.setIdentity();
104 
105  transform.setRotation(orientation);
106  transform.setOrigin(pos);
107 
108  Body->setCenterOfMassTransform(transform);
109  return true;
110 }
#define LOG_ERROR(x)
Definition: Define.h:96
DLLEXPORT bool HasInvalidValues() const noexcept
Definition: Types.h:903
static std::string ToString(const T &val)
Definition: Convert.h:72
DLLEXPORT bool HasInvalidValues() const noexcept
Definition: Types.h:1275

◆ SetVelocity()

DLLEXPORT void PhysicsBody::SetVelocity ( const Float3 velocities)

Overrides this objects velocity in ApplyForceAndTorqueEvent.

Definition at line 164 of file PhysicsBody.cpp.

165 {
166  if(!Body)
167  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
168 
169 
170  // Safety check. Can be disabled in release builds if this is a performance issue
171  if(velocities.HasInvalidValues()) {
172 
173  std::stringstream msg;
174  msg << "SetVelocity call had at least one value with non-finite value, velocities: "
175  << Convert::ToString(velocities);
176  LOG_ERROR("PhysicsBody: " + msg.str());
177  throw InvalidArgument(msg.str());
178  }
179 
180  Body->setLinearVelocity(velocities);
181 }
#define LOG_ERROR(x)
Definition: Define.h:96
DLLEXPORT bool HasInvalidValues() const noexcept
Definition: Types.h:903
static std::string ToString(const T &val)
Definition: Convert.h:72

Friends And Related Function Documentation

◆ PhysicalWorld

friend class PhysicalWorld
friend

Definition at line 77 of file PhysicsBody.h.

Member Data Documentation

◆ ReferenceCounted

friend Leviathan::PhysicsBody::ReferenceCounted
protected

Definition at line 81 of file PhysicsBody.h.


The documentation for this class was generated from the following files: