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 346 of file Components.h.

Constructor & Destructor Documentation

◆ Physics()

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

Definition at line 412 of file Components.h.

412  :
413  Component(TYPE), ThisEntity(args.id), _Position(args.updatepos)
414  {}
ObjectID ThisEntity
For passing to PhysicsBody::SetOwningEntity.
Definition: Components.h:488
Component(COMPONENT_TYPE type)
Definition: Component.h:54
static constexpr auto TYPE
Definition: Components.h:496
Position & _Position
Physics object requires a position.
Definition: Components.h:494

◆ ~Physics()

DLLEXPORT Physics::~Physics ( )

Definition at line 100 of file Components.cpp.

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

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 143 of file Components.cpp.

144 {
145  if(!world || !shape)
146  return false;
147 
148  return world->ChangeBodyShape(Body, shape);
149 }
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 465 of file Components.h.

466  {
467  return ChangeShape(world, ReferenceCounted::WrapPtr(shape));
468  }
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:143
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 123 of file Components.cpp.

125 {
126  if(!world || !shape)
127  return nullptr;
128 
129  // Destroy old if there is one //
130  if(Body)
131  world->DestroyBody(Body.get());
132 
133  Body = world->CreateBodyFromCollision(shape, mass, &_Position, physicsmaterialid);
134 
135  if(!Body)
136  return nullptr;
137 
138  Body->SetOwningEntity(ThisEntity);
139 
140  return Body;
141 }
ObjectID ThisEntity
For passing to PhysicsBody::SetOwningEntity.
Definition: Components.h:488
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:494

◆ CreatePhysicsBodyWrapper()

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

Definition at line 470 of file Components.h.

472  {
473  auto body = CreatePhysicsBody(
474  world, ReferenceCounted::WrapPtr(shape), mass, physicsmaterialid);
475 
476  if(body)
477  body->AddRef();
478  return body.get();
479  }
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:123
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 449 of file Components.h.

450  {
451  return Body.get();
452  }

◆ GetBodyWrapper()

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

Definition at line 458 of file Components.h.

459  {
460  if(Body)
461  Body->AddRef();
462  return Body.get();
463  }

◆ 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 117 of file Components.cpp.

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

◆ 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 107 of file Components.cpp.

108 {
109  if(Body) {
110  Body->SetOwningEntity(NULL_OBJECT);
111  world->DestroyBody(Body.get());
112  }
113 
114  Body.reset();
115 }
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 494 of file Components.h.

◆ ThisEntity

ObjectID Leviathan::Physics::ThisEntity

For passing to PhysicsBody::SetOwningEntity.

Definition at line 488 of file Components.h.

◆ TYPE

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

Definition at line 496 of file Components.h.


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