Leviathan  0.8.0.0
Leviathan game engine
Leviathan::PhysicalWorld Class Reference

#include <PhysicalWorld.h>

Public Member Functions

DLLEXPORT PhysicalWorld (GameWorld *owner)
 
DLLEXPORT ~PhysicalWorld ()
 
DLLEXPORT void SimulateWorld (int maxruns=-1)
 Calculates and simulates away all accumulated time. More...
 
DLLEXPORT void SimulateWorldFixed (uint32_t mspassed, uint32_t stepcount=1)
 Advances the simulation the specified amount of time. More...
 
DLLEXPORT void ClearTimers ()
 Clears passed time. More...
 
DLLEXPORT void AdjustClock (int milliseconds)
 Adds or subtracts time from the clock. More...
 
DLLEXPORT void DestroyCollision (NewtonCollision *collision)
 
DLLEXPORT NewtonCollision * CreateCompoundCollision ()
 
DLLEXPORT NewtonCollision * CreateSphere (float radius, const Ogre::Matrix4 &offset=Ogre::Matrix4::IDENTITY)
 
DLLEXPORT NewtonCollision * CreateBox (float xdimension, float ydimension, float zdimension, const Ogre::Matrix4 &offset=Ogre::Matrix4::IDENTITY)
 
DLLEXPORT NewtonJoint * Create2DJoint (NewtonBody *body, const Float3 &planenormal)
 Constraints body to a 2d plane of movement specified by its normal. More...
 
DLLEXPORT NewtonBody * CreateBodyFromCollision (NewtonCollision *collision)
 
DLLEXPORT void DestroyBody (NewtonBody *body)
 
DLLEXPORT GameWorldGetGameWorld ()
 
DLLEXPORT NewtonWorld * GetNewtonWorld ()
 

Protected Attributes

int64_t PassedTimeTotal = 0
 Total amount of microseconds required to be simulated. More...
 
int64_t LastSimulatedTime = 0
 
NewtonWorld * World
 
GameWorldOwningWorld
 
Mutex WorldUpdateLock
 Lock for world updates. More...
 
NewtonBody * ResimulatedBody = nullptr
 

Friends

int SingleBodyUpdate (const NewtonWorld *const newtonWorld, const void *islandHandle, int bodyCount)
 

Detailed Description

Definition at line 46 of file PhysicalWorld.h.

Constructor & Destructor Documentation

◆ PhysicalWorld()

DLLEXPORT Leviathan::PhysicalWorld::PhysicalWorld ( GameWorld owner)

Definition at line 12 of file PhysicalWorld.cpp.

12  : OwningWorld(owner)
13 {
14  // create newton world //
15  World = NewtonCreate();
16 
17  // set physics accuracy //
18  // most accurate mode //
19 
20  // \todo figure out how to use this exact mode //
21  // NewtonSetSolverModel(World, 0);
22 
23  // Accurate enough mode //
24  NewtonSetSolverModel(World, 1);
25 
26  // Set us as the user data //
27  NewtonWorldSetUserData(World, this);
28 
29  // Create materials for this world //
31 }
DLLEXPORT void CreateActualMaterialsForWorld(NewtonWorld *newtonworld)
static DLLEXPORT PhysicsMaterialManager * Get()

◆ ~PhysicalWorld()

DLLEXPORT Leviathan::PhysicalWorld::~PhysicalWorld ( )

Definition at line 33 of file PhysicalWorld.cpp.

34 {
35  // Destroy the newton world
36  NewtonDestroy(World);
37 
38  // NewtonWorldSetUserData(World, NULL);
39 
40  auto physmanager = PhysicsMaterialManager::Get();
41 
42  if(physmanager)
43  physmanager->DestroyActualMaterialsForWorld(World);
44 
45  World = NULL;
46 }
static DLLEXPORT PhysicsMaterialManager * Get()

Member Function Documentation

◆ AdjustClock()

DLLEXPORT void Leviathan::PhysicalWorld::AdjustClock ( int  milliseconds)

Adds or subtracts time from the clock.

For example passing in 100 will run the physical simulation more times next update to account for milliseconds amount of passed time

Definition at line 129 of file PhysicalWorld.cpp.

130 {
131  // Convert from milliseconds (10^-3) to micro seconds (10^-6) //
132  LastSimulatedTime -= 1000 * milliseconds;
133 }

◆ ClearTimers()

DLLEXPORT void Leviathan::PhysicalWorld::ClearTimers ( )

Clears passed time.

Definition at line 123 of file PhysicalWorld.cpp.

124 {
126  PassedTimeTotal = 0;
127 }
int64_t PassedTimeTotal
Total amount of microseconds required to be simulated.
static DLLEXPORT int64_t GetTimeMicro64()

◆ Create2DJoint()

DLLEXPORT NewtonJoint * PhysicalWorld::Create2DJoint ( NewtonBody *  body,
const Float3 planenormal 
)

Constraints body to a 2d plane of movement specified by its normal.

Definition at line 244 of file PhysicalWorld.cpp.

246 {
247  // Method from http://newtondynamics.com/wiki/index.php5?title=CJ_2D_Joint_planar_rotation
248  dFloat matrix[16];
249  NewtonBodyGetMatrix(body, &matrix[0]);
250  const auto planeOrigin = ExtractNewtonMatrixTranslation(matrix);
251 
252  NewtonJoint* joint =
253  NewtonConstraintCreateUserJoint(World, 6, Joint2D::SubmitConstraints, body, nullptr);
254 
255  Joint2D* jointData = new Joint2D(planenormal, planeOrigin, body);
256 
257  NewtonJointSetUserData(joint, static_cast<BaseCustomJoint*>(jointData));
258 
259  NewtonJointSetDestructor(joint, BaseCustomJoint::JointDestructorCallback);
260  return joint;
261 }
static void SubmitConstraints(const NewtonJoint *const joint, dFloat timestep, int threadIndex)
static DLLEXPORT void JointDestructorCallback(const NewtonJoint *joint)
DLLEXPORT Float3 ExtractNewtonMatrixTranslation(const float(&matrix)[16])
Grabs the translation from a newton matrix without transposing.

◆ CreateBodyFromCollision()

DLLEXPORT NewtonBody * PhysicalWorld::CreateBodyFromCollision ( NewtonCollision *  collision)

Definition at line 177 of file PhysicalWorld.cpp.

178 {
179  if(!collision)
180  return nullptr;
181 
182  const auto& prep = PrepareOgreMatrixForNewton(Ogre::Matrix4::IDENTITY);
183  return NewtonCreateDynamicBody(World, collision, prep[0]);
184 }
DLLEXPORT Ogre::Matrix4 PrepareOgreMatrixForNewton(const Ogre::Matrix4 &matrix)

◆ CreateBox()

DLLEXPORT NewtonCollision * PhysicalWorld::CreateBox ( float  xdimension,
float  ydimension,
float  zdimension,
const Ogre::Matrix4 &  offset = Ogre::Matrix4::IDENTITY 
)

Definition at line 168 of file PhysicalWorld.cpp.

170 {
171  const auto& prep = PrepareOgreMatrixForNewton(offset);
172 
173  return NewtonCreateBox(
174  World, xdimension, ydimension, zdimension, UNUSED_SHAPE_ID, prep[0]);
175 }
DLLEXPORT Ogre::Matrix4 PrepareOgreMatrixForNewton(const Ogre::Matrix4 &matrix)
constexpr auto UNUSED_SHAPE_ID
Definition: PhysicalWorld.h:41

◆ CreateCompoundCollision()

DLLEXPORT NewtonCollision * PhysicalWorld::CreateCompoundCollision ( )

Definition at line 140 of file PhysicalWorld.cpp.

141 {
142  // 0 is shapeID
143  return NewtonCreateCompoundCollision(World, UNUSED_SHAPE_ID);
144 }
constexpr auto UNUSED_SHAPE_ID
Definition: PhysicalWorld.h:41

◆ CreateSphere()

DLLEXPORT NewtonCollision * PhysicalWorld::CreateSphere ( float  radius,
const Ogre::Matrix4 &  offset = Ogre::Matrix4::IDENTITY 
)

Definition at line 146 of file PhysicalWorld.cpp.

148 {
149 #ifdef CHECK_FOR_NANS
150  bool matrixNans = false;
151  for(size_t i = 0; i < 16; ++i) {
152  if(std::isnan(*(offset[0] + i))) {
153  matrixNans = true;
154  break;
155  }
156  }
157 
158  if(std::isnan(radius) || matrixNans) {
159 
160  DEBUG_BREAK;
161  throw std::runtime_error("CreateSphere has NaNs in it!");
162  }
163 #endif // CHECK_FOR_NANS
164  const auto& prep = PrepareOgreMatrixForNewton(offset);
165  return NewtonCreateSphere(World, radius, UNUSED_SHAPE_ID, prep[0]);
166 }
DLLEXPORT Ogre::Matrix4 PrepareOgreMatrixForNewton(const Ogre::Matrix4 &matrix)
constexpr auto UNUSED_SHAPE_ID
Definition: PhysicalWorld.h:41

◆ DestroyBody()

DLLEXPORT void PhysicalWorld::DestroyBody ( NewtonBody *  body)

Definition at line 186 of file PhysicalWorld.cpp.

187 {
188  NewtonDestroyBody(body);
189 }

◆ DestroyCollision()

DLLEXPORT void PhysicalWorld::DestroyCollision ( NewtonCollision *  collision)

Definition at line 135 of file PhysicalWorld.cpp.

136 {
137  NewtonDestroyCollision(collision);
138 }

◆ GetGameWorld()

DLLEXPORT GameWorld* Leviathan::PhysicalWorld::GetGameWorld ( )
inline

Definition at line 97 of file PhysicalWorld.h.

98  {
99  return OwningWorld;
100  }

◆ GetNewtonWorld()

DLLEXPORT NewtonWorld* Leviathan::PhysicalWorld::GetNewtonWorld ( )
inline

Definition at line 102 of file PhysicalWorld.h.

103  {
104  return World;
105  }

◆ SimulateWorld()

DLLEXPORT void Leviathan::PhysicalWorld::SimulateWorld ( int  maxruns = -1)

Calculates and simulates away all accumulated time.

Definition at line 48 of file PhysicalWorld.cpp.

49 {
50  if(maxruns <= 0) {
51 
52  // TODO: report error?
53  maxruns = 1;
54  }
55 
56  int runs = 0;
57 
58  auto curtime = Time::GetTimeMicro64();
59 
60  // Calculate passed time and reset //
62  LastSimulatedTime = curtime;
63 
64  // Cap passed time, if over one second //
67 
68  Lock lock(WorldUpdateLock);
69 
71 
72  // Call event //
75 
76  NewtonUpdate(World, NEWTON_TIMESTEP);
77  PassedTimeTotal -= static_cast<int64_t>(NEWTON_FPS_IN_MICROSECONDS);
78  runs++;
79 
80  if(runs == maxruns) {
81 
82  Logger::Get()->Warning("PhysicalWorld: bailing from update after " +
83  Convert::ToString(runs) +
84  " with time left: " + Convert::ToString(PassedTimeTotal));
85  break;
86  }
87  }
88 }
#define NEWTON_FPS_IN_MICROSECONDS
Definition: PhysicalWorld.h:25
Class that represents a statically defined event.
Definition: Event.h:128
int64_t PassedTimeTotal
Total amount of microseconds required to be simulated.
#define NEWTON_TIMESTEP
Definition: PhysicalWorld.h:26
DLLEXPORT void Warning(const std::string &data) override
Definition: Logger.cpp:190
static DLLEXPORT int64_t GetTimeMicro64()
Mutex WorldUpdateLock
Lock for world updates.
EventHandler * GetEventHandler()
Definition: Engine.h:144
static std::string ToString(const T &val)
Definition: Convert.h:72
DLLEXPORT void CallEvent(Event *event)
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:82
Data for EVENT_TYPE_PHYSICS_BEGIN.
Definition: Event.h:96
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:15
constexpr auto MICROSECONDS_IN_SECOND
Definition: Define.h:55

◆ SimulateWorldFixed()

DLLEXPORT void Leviathan::PhysicalWorld::SimulateWorldFixed ( uint32_t  mspassed,
uint32_t  stepcount = 1 
)

Advances the simulation the specified amount of time.

Definition at line 90 of file PhysicalWorld.cpp.

92 {
93  float timestep = (mspassed / 1000.f) / stepcount;
94 
95  for(uint32_t i = 0; i < stepcount; ++i) {
96 
99 
100  NewtonUpdate(World, timestep);
101  }
102 }
Class that represents a statically defined event.
Definition: Event.h:128
EventHandler * GetEventHandler()
Definition: Engine.h:144
DLLEXPORT void CallEvent(Event *event)
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:82
unsigned int uint32_t
Definition: core.h:40
Data for EVENT_TYPE_PHYSICS_BEGIN.
Definition: Event.h:96

Friends And Related Function Documentation

◆ SingleBodyUpdate

int SingleBodyUpdate ( const NewtonWorld *const  newtonWorld,
const void *  islandHandle,
int  bodyCount 
)
friend

Member Data Documentation

◆ LastSimulatedTime

int64_t Leviathan::PhysicalWorld::LastSimulatedTime = 0
protected

Definition at line 110 of file PhysicalWorld.h.

◆ OwningWorld

GameWorld* Leviathan::PhysicalWorld::OwningWorld
protected

Definition at line 113 of file PhysicalWorld.h.

◆ PassedTimeTotal

int64_t Leviathan::PhysicalWorld::PassedTimeTotal = 0
protected

Total amount of microseconds required to be simulated.

Definition at line 109 of file PhysicalWorld.h.

◆ ResimulatedBody

NewtonBody* Leviathan::PhysicalWorld::ResimulatedBody = nullptr
protected

Used for resimulation

Todo:
Potentially allow this to be a vector

Definition at line 120 of file PhysicalWorld.h.

◆ World

NewtonWorld* Leviathan::PhysicalWorld::World
protected

Definition at line 112 of file PhysicalWorld.h.

◆ WorldUpdateLock

Mutex Leviathan::PhysicalWorld::WorldUpdateLock
protected

Lock for world updates.

Definition at line 116 of file PhysicalWorld.h.


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