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)
 
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 ()
 
FORCE_INLINE void Release ()
 removes a reference and deletes the object if reference count reaches zero More...
 
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...
 
- 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 74 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 }
#define LOG_ERROR(x)
Definition: Define.h:84
DLLEXPORT void DetachResources()
This releases all resources held by this. This is called by the PhysicalWorld when this is destroyed ...
Definition: PhysicsBody.cpp:35

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 293 of file PhysicsBody.cpp.

294 {
295  if(!Body)
296  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
297 
298  // TODO: properties
299 }

◆ ApplyShapeChange()

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

Applies shape change.

Definition at line 45 of file PhysicsBody.cpp.

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

◆ ApplyTorque()

DLLEXPORT void PhysicsBody::ApplyTorque ( const Float3 torque)

Adds to the torque of this object.

Definition at line 220 of file PhysicsBody.cpp.

221 {
222  if(!Body)
223  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
224 
225 
226  // Safety check. Can be disabled in release builds if this is a performance issue
227  if(torque.HasInvalidValues()) {
228 
229  std::stringstream msg;
230  msg << "ApplyTorque call had at least one value with non-finite value, torque: "
231  << Convert::ToString(torque);
232  LOG_ERROR("PhysicsBody: " + msg.str());
233  throw InvalidArgument(msg.str());
234  }
235 
236  Body->applyTorque(torque);
237 }
#define LOG_ERROR(x)
Definition: Define.h:84
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 164 of file PhysicsBody.cpp.

165 {
166  if(!Body)
167  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
168 
169 
170  Body->setAngularVelocity(btVector3(0, 0, 0));
171  Body->setLinearVelocity(btVector3(0, 0, 0));
172  Body->clearForces();
173 }

◆ 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 283 of file PhysicsBody.cpp.

285 {
286  if(!Body)
287  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
288 
289  Body->setLinearFactor(movement);
290  Body->setAngularFactor(rotation);
291 }

◆ 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 35 of file PhysicsBody.cpp.

36 {
37  Body->setUserPointer(nullptr);
38  Body->setMotionState(nullptr);
39  Body.reset();
40  PositionUpdate.reset();
41 
42  Shape.reset();
43 }

◆ GetAngularVelocity()

DLLEXPORT Float3 PhysicsBody::GetAngularVelocity ( ) const

Gets the omega (angular velocity)

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  return Body->getAngularVelocity();
189 }

◆ GetBody()

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

Definition at line 163 of file PhysicsBody.h.

164  {
165  return Body.get();
166  }

◆ GetMass()

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

Definition at line 148 of file PhysicsBody.h.

149  {
150  return Mass;
151  }

◆ GetOwningEntity()

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

Data for helping with callbacks.

Definition at line 179 of file PhysicsBody.h.

180  {
181  return ThisEntity;
182  }

◆ GetPhysicalMaterialID()

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

Definition at line 173 of file PhysicsBody.h.

174  {
175  return PhysicalMaterialID;
176  }

◆ GetPosition()

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

Definition at line 93 of file PhysicsBody.cpp.

94 {
95  if(!Body)
96  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
97 
98  return Body->getCenterOfMassTransform().getOrigin();
99 }

◆ GetShape()

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

Definition at line 168 of file PhysicsBody.h.

169  {
170  return Shape.get();
171  }

◆ GetShapeWrapper()

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

Definition at line 192 of file PhysicsBody.h.

193  {
194  if(Shape)
195  Shape->AddRef();
196  return Shape.get();
197  }

◆ GetTorque()

DLLEXPORT Float3 PhysicsBody::GetTorque ( ) const

Gets the torque of the body (rotational velocity)

Definition at line 211 of file PhysicsBody.cpp.

212 {
213  if(!Body)
214  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
215 
216 
217  return Body->getTotalTorque();
218 }

◆ GetVelocity()

DLLEXPORT Float3 PhysicsBody::GetVelocity ( ) const

Gets the absolute velocity.

Definition at line 175 of file PhysicsBody.cpp.

176 {
177  if(!Body)
178  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
179 
180  return Body->getLinearVelocity();
181 }

◆ 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 125 of file PhysicsBody.cpp.

127 {
128  if(!Body)
129  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
130 
131  // Safety check. Can be disabled in release builds if this is a performance issue
132  if(deltaspeed.HasInvalidValues() || point.HasInvalidValues()) {
133 
134  std::stringstream msg;
135  msg << "GiveImpulse call had at least one value with non-finite value, deltaspeed: "
136  << Convert::ToString(deltaspeed) << " point: " << Convert::ToString(point);
137  LOG_ERROR("PhysicsBody: " + msg.str());
138  throw InvalidArgument(msg.str());
139  }
140 
141  Body->applyImpulse(deltaspeed, point);
142  // Maybe this needs to call activate() for the force to move it?
143 }
#define LOG_ERROR(x)
Definition: Define.h:84
DLLEXPORT bool HasInvalidValues() const noexcept
Definition: Types.h:903
static std::string ToString(const T &val)
Definition: Convert.h:72

◆ 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 191 of file PhysicsBody.cpp.

192 {
193  if(!Body)
194  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
195 
196 
197  // Safety check. Can be disabled in release builds if this is a performance issue
198  if(velocities.HasInvalidValues()) {
199 
200  std::stringstream msg;
201  msg << "SetAngularVelocity call had at least one value with non-finite value, "
202  "velocities: "
203  << Convert::ToString(velocities);
204  LOG_ERROR("PhysicsBody: " + msg.str());
205  throw InvalidArgument(msg.str());
206  }
207 
208  Body->setAngularVelocity(velocities);
209 }
#define LOG_ERROR(x)
Definition: Define.h:84
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 239 of file PhysicsBody.cpp.

240 {
241  if(!Body)
242  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
243 
244  Body->setDamping(linear, angular);
245 }

◆ SetFriction()

DLLEXPORT void PhysicsBody::SetFriction ( float  friction)

Sets the friction on this body.

Definition at line 247 of file PhysicsBody.cpp.

248 {
249  if(!Body)
250  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
251 
252  Body->setFriction(friction);
253 }

◆ 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 255 of file PhysicsBody.cpp.

256 {
257  if(!Body)
258  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
259 
260 
261  // Safety check. Shouldn't be disabled
262  if(!std::isfinite(mass)) {
263 
264  std::stringstream msg;
265  msg << "SetMass mass is invalid value: " << Convert::ToString(mass)
266  << " (use 0 for immovable)";
267  LOG_ERROR("Physics: " + msg.str());
268  throw InvalidArgument(msg.str());
269  }
270 
271  Mass = mass;
272 
273  // First calculate inertia and center of mass points //
274  const bool isDynamic = Mass > 0;
275 
276  btVector3 localInertia(0, 0, 0);
277  if(isDynamic)
278  Shape->GetShape()->calculateLocalInertia(Mass, localInertia);
279 
280  Body->setMassProps(mass, localInertia);
281 }
#define LOG_ERROR(x)
Definition: Define.h:84
#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 101 of file PhysicsBody.cpp.

102 {
103  if(!Body)
104  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
105 
106  // Safety check. Can be disabled in release builds if this is a performance issue
107  if(orientation.HasInvalidValues()) {
108 
109  std::stringstream msg;
110  msg << "SetOnlyOrientation call had at least one value with non-finite value, "
111  "orientation: "
112  << Convert::ToString(orientation);
113  LOG_ERROR("PhysicsBody: " + msg.str());
114  throw InvalidArgument(msg.str());
115  }
116 
117  btTransform pos = Body->getCenterOfMassTransform();
118 
119  pos.setRotation(orientation);
120 
121  Body->setCenterOfMassTransform(pos);
122  return true;
123 }
#define LOG_ERROR(x)
Definition: Define.h:84
static std::string ToString(const T &val)
Definition: Convert.h:72
DLLEXPORT bool HasInvalidValues() const noexcept
Definition: Types.h:1276

◆ SetOwningEntity()

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

Definition at line 184 of file PhysicsBody.h.

185  {
186  ThisEntity = entity;
187  }

◆ 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 115 of file PhysicsBody.h.

116  {
117  PhysicalMaterialID = id;
118  }

◆ 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 67 of file PhysicsBody.cpp.

68 {
69  if(!Body)
70  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
71 
72  // Safety check. Can be disabled in release builds if this is a performance issue
73  if(pos.HasInvalidValues() || orientation.HasInvalidValues()) {
74 
75  std::stringstream msg;
76  msg << "SetPosition call had at least one value with non-finite value, pos: "
77  << Convert::ToString(pos) << " orientation: " << Convert::ToString(orientation);
78  LOG_ERROR("PhysicsBody: " + msg.str());
79  throw InvalidArgument(msg.str());
80  }
81 
82  btTransform transform;
83  // TODO: check is this required
84  transform.setIdentity();
85 
86  transform.setRotation(orientation);
87  transform.setOrigin(pos);
88 
89  Body->setCenterOfMassTransform(transform);
90  return true;
91 }
#define LOG_ERROR(x)
Definition: Define.h:84
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:1276

◆ SetVelocity()

DLLEXPORT void PhysicsBody::SetVelocity ( const Float3 velocities)

Overrides this objects velocity in ApplyForceAndTorqueEvent.

Definition at line 145 of file PhysicsBody.cpp.

146 {
147  if(!Body)
148  throw InvalidArgument("PhysicsBody has no longer an internal physics engine body");
149 
150 
151  // Safety check. Can be disabled in release builds if this is a performance issue
152  if(velocities.HasInvalidValues()) {
153 
154  std::stringstream msg;
155  msg << "SetVelocity call had at least one value with non-finite value, velocities: "
156  << Convert::ToString(velocities);
157  LOG_ERROR("PhysicsBody: " + msg.str());
158  throw InvalidArgument(msg.str());
159  }
160 
161  Body->setLinearVelocity(velocities);
162 }
#define LOG_ERROR(x)
Definition: Define.h:84
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 75 of file PhysicsBody.h.

Member Data Documentation

◆ ReferenceCounted

friend Leviathan::PhysicsBody::ReferenceCounted
protected

Definition at line 78 of file PhysicsBody.h.


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