Leviathan  0.8.0.0
Leviathan game engine
Leviathan::Physics Class Reference

Entity has a physical component. More...

#include <Components.h>

+ Inheritance diagram for Leviathan::Physics:

Classes

class  ApplyForceInfo
 Holder for information regarding a single force. More...
 
struct  BasePhysicsData
 
struct  Data
 

Public Member Functions

 Physics (const Data &args)
 
DLLEXPORT ~Physics ()
 
DLLEXPORT void Release (PhysicalWorld *world)
 Destroys the physical body. More...
 
DLLEXPORT PhysicsBody::pointer CreatePhysicsBody (PhysicalWorld *world, const PhysicsShape::pointer &shape, float mass, int physicsmaterialid=-1)
 Use this to create a body for this component once Collision is set. More...
 
DLLEXPORT bool ChangeShape (PhysicalWorld *world, const PhysicsShape::pointer &shape)
 Updates the shape. This can be the same pointer as before. BUT if the shape is modified this MUST be called before physics is simulated the next time. More...
 
DLLEXPORT void JumpTo (Position &target)
 Syncs this physics body to a changed position. More...
 
DLLEXPORT void ApplyPhysicalState (const BasePhysicsData &data)
 Applies physical state from holder object. More...
 
Float3 _GatherApplyForces (const float &mass)
 Adds all applied forces together. More...
 
DLLEXPORT float GetMass () const
 
PhysicsBodyGetBody () const
 
PhysicsBodyGetBodyWrapper () const
 
bool ChangeShapeWrapper (PhysicalWorld *world, PhysicsShape *shape)
 
PhysicsBodyCreatePhysicsBodyWrapper (PhysicalWorld *world, PhysicsShape *shape, float mass, int physicsmaterialid)
 
 REFERENCE_HANDLE_UNCOUNTED_TYPE (Physics)
 
- Public Member Functions inherited from Leviathan::Component
 Component (COMPONENT_TYPE type)
 
 Component (const Component &)=delete
 
Componentoperator= (const Component &)=delete
 

Public Attributes

ObjectID ThisEntity
 For passing to PhysicsBody::SetOwningEntity. More...
 
Position_Position
 Physics object requires a position. More...
 
- Public Attributes inherited from Leviathan::Component
bool Marked
 
const COMPONENT_TYPE Type
 Type of this component, used for network serialization. More...
 

Static Public Attributes

static constexpr auto TYPE = COMPONENT_TYPE::Physics
 

Detailed Description

Entity has a physical component.

Precondition
Entity has Position component
Todo:
Global Newton lock

Definition at line 333 of file Components.h.

Constructor & Destructor Documentation

◆ Physics()

Leviathan::Physics::Physics ( const Data args)
inline

Definition at line 398 of file Components.h.

398  :
399  Component(TYPE), ThisEntity(args.id), _Position(args.updatepos)
400  {}
ObjectID ThisEntity
For passing to PhysicsBody::SetOwningEntity.
Definition: Components.h:474
Component(COMPONENT_TYPE type)
Definition: Component.h:54
static constexpr auto TYPE
Definition: Components.h:482
Position & _Position
Physics object requires a position.
Definition: Components.h:480

◆ ~Physics()

DLLEXPORT Physics::~Physics ( )

Definition at line 101 of file Components.cpp.

102 {
103  if(Body) {
104  LOG_FATAL("Physics: Release not called before destructor!");
105  }
106 }
#define LOG_FATAL(x)
Definition: Define.h:86

Member Function Documentation

◆ _GatherApplyForces()

Float3 Leviathan::Physics::_GatherApplyForces ( const float &  mass)

Adds all applied forces together.

◆ ApplyPhysicalState()

DLLEXPORT void Leviathan::Physics::ApplyPhysicalState ( const BasePhysicsData data)

Applies physical state from holder object.

◆ ChangeShape()

DLLEXPORT bool Physics::ChangeShape ( PhysicalWorld world,
const PhysicsShape::pointer &  shape 
)

Updates the shape. This can be the same pointer as before. BUT if the shape is modified this MUST be called before physics is simulated the next time.

Definition at line 144 of file Components.cpp.

145 {
146  if(!world || !shape)
147  return false;
148 
149  return world->ChangeBodyShape(Body, shape);
150 }
DLLEXPORT bool ChangeBodyShape(const PhysicsBody::pointer &body, const PhysicsShape::pointer &shape)
Applies a changed shape to a body.

◆ ChangeShapeWrapper()

bool Leviathan::Physics::ChangeShapeWrapper ( PhysicalWorld world,
PhysicsShape shape 
)
inline

Definition at line 451 of file Components.h.

452  {
453  return ChangeShape(world, ReferenceCounted::WrapPtr(shape));
454  }
DLLEXPORT bool ChangeShape(PhysicalWorld *world, const PhysicsShape::pointer &shape)
Updates the shape. This can be the same pointer as before. BUT if the shape is modified this MUST be ...
Definition: Components.cpp:144
static boost::intrusive_ptr< ActualType > WrapPtr(ActualType *ptr)
Creates an intrusive_ptr from raw pointer.

◆ CreatePhysicsBody()

DLLEXPORT PhysicsBody::pointer Physics::CreatePhysicsBody ( PhysicalWorld world,
const PhysicsShape::pointer &  shape,
float  mass,
int  physicsmaterialid = -1 
)

Use this to create a body for this component once Collision is set.

Parameters
physicsmaterialidRetrieve from the same world with GameWorld::GetPhysicalMaterial. -1 to use default material
Todo:
This should be changed so that the Physics constructor would create the physics body
Warning
Only the physical world from the GameWorld that this entity is in may be used here

Definition at line 124 of file Components.cpp.

126 {
127  if(!world || !shape)
128  return nullptr;
129 
130  // Destroy old if there is one //
131  if(Body)
132  world->DestroyBody(Body.get());
133 
134  Body = world->CreateBodyFromCollision(shape, mass, &_Position, physicsmaterialid);
135 
136  if(!Body)
137  return nullptr;
138 
139  Body->SetOwningEntity(ThisEntity);
140 
141  return Body;
142 }
ObjectID ThisEntity
For passing to PhysicsBody::SetOwningEntity.
Definition: Components.h:474
DLLEXPORT PhysicsBody::pointer CreateBodyFromCollision(const PhysicsShape::pointer &shape, float mass, PhysicsPositionProvider *positionsynchronization, int physicsmaterialid=-1)
Constraints body to a 2d plane of movement specified by its normal.
DLLEXPORT bool DestroyBody(PhysicsBody *body)
Destroys a physics body.
Position & _Position
Physics object requires a position.
Definition: Components.h:480

◆ CreatePhysicsBodyWrapper()

PhysicsBody* Leviathan::Physics::CreatePhysicsBodyWrapper ( PhysicalWorld world,
PhysicsShape shape,
float  mass,
int  physicsmaterialid 
)
inline

Definition at line 456 of file Components.h.

458  {
459  auto body = CreatePhysicsBody(
460  world, ReferenceCounted::WrapPtr(shape), mass, physicsmaterialid);
461 
462  if(body)
463  body->AddRef();
464  return body.get();
465  }
DLLEXPORT PhysicsBody::pointer CreatePhysicsBody(PhysicalWorld *world, const PhysicsShape::pointer &shape, float mass, int physicsmaterialid=-1)
Use this to create a body for this component once Collision is set.
Definition: Components.cpp:124
static boost::intrusive_ptr< ActualType > WrapPtr(ActualType *ptr)
Creates an intrusive_ptr from raw pointer.

◆ GetBody()

PhysicsBody* Leviathan::Physics::GetBody ( ) const
inline

Definition at line 435 of file Components.h.

436  {
437  return Body.get();
438  }

◆ GetBodyWrapper()

PhysicsBody* Leviathan::Physics::GetBodyWrapper ( ) const
inline

Definition at line 444 of file Components.h.

445  {
446  if(Body)
447  Body->AddRef();
448  return Body.get();
449  }

◆ GetMass()

DLLEXPORT float Leviathan::Physics::GetMass ( ) const

◆ JumpTo()

DLLEXPORT void Physics::JumpTo ( Position target)

Syncs this physics body to a changed position.

Call after making changes to Position component if you don't want this physics body to overwrite the change on next tick.

Definition at line 118 of file Components.cpp.

119 {
120  if(Body)
121  Body->SetPosition(target.Members._Position, target.Members._Orientation);
122 }

◆ REFERENCE_HANDLE_UNCOUNTED_TYPE()

Leviathan::Physics::REFERENCE_HANDLE_UNCOUNTED_TYPE ( Physics  )

◆ Release()

DLLEXPORT void Physics::Release ( PhysicalWorld world)

Destroys the physical body.

Definition at line 108 of file Components.cpp.

109 {
110  if(Body) {
111  Body->SetOwningEntity(NULL_OBJECT);
112  world->DestroyBody(Body.get());
113  }
114 
115  Body.reset();
116 }
constexpr ObjectID NULL_OBJECT
Definition: EntityCommon.h:14
DLLEXPORT bool DestroyBody(PhysicsBody *body)
Destroys a physics body.

Member Data Documentation

◆ _Position

Position& Leviathan::Physics::_Position

Physics object requires a position.

Definition at line 480 of file Components.h.

◆ ThisEntity

ObjectID Leviathan::Physics::ThisEntity

For passing to PhysicsBody::SetOwningEntity.

Definition at line 474 of file Components.h.

◆ TYPE

constexpr auto Leviathan::Physics::TYPE = COMPONENT_TYPE::Physics
static

Definition at line 482 of file Components.h.


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