YourGameLib
Loading...
Searching...
No Matches
physenv.h
Go to the documentation of this file.
1/*
2Copyright (c) 2019-2024 Alexander Scholz
3
4This software is provided 'as-is', without any express or implied
5warranty. In no event will the authors be held liable for any damages
6arising from the use of this software.
7
8Permission is granted to anyone to use this software for any purpose,
9including commercial applications, and to alter it and redistribute it
10freely, subject to the following restrictions:
11
121. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
162. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
183. This notice may not be removed or altered from any source distribution.
19*/
20#ifndef YOURGAME_PHYSENV_H
21#define YOURGAME_PHYSENV_H
22
23#include <memory> // unique_ptr
24#include <map>
25#include <string>
26#include "yourgame/math/trafo.h"
27
28namespace yourgame
29{
30 namespace util
31 {
33 {
34 float mass = 1.0f;
35 float linearDamping = 0.0f;
36 float angularDamping = 0.0f;
37 float friction = 0.5f;
38 float rollingFriction = 0.0f;
39 float spinningFriction = 0.0f;
40 float restitution = 0.0f;
43 bool kinematic = false;
44 bool disableDeactivation = false;
45 };
46
49 {
50 public:
57 RigidBody(std::string name, void *body) : m_name(name), m_body(body) {}
58
60
67
76
82 void setRestitution(float restitution);
83
89 void setFriction(float friction);
90
97 void setSleepingThresholds(float linear, float angular);
98
106 void setLinearFactors(float x, float y, float z);
107
113 void setAngularFactor(float angFac);
114
122 void setAngularFactors(float x, float y, float z);
123
131 void applyCentralForce(float x, float y, float z);
132
140 void applyCentralImpulse(float x, float y, float z);
141
142 std::string m_name;
143 void *const m_body;
144 };
145
148 {
149 public:
157 Collision(RigidBody *body0, RigidBody *body1, float impulse) : m_body0(body0),
158 m_body1(body1),
159 m_impulse(impulse) {}
163
170 bool involves(const std::string &name) const
171 {
172 return (m_body0->m_name == name) || (m_body1->m_name == name);
173 }
174
182 bool involves2(const std::string &nameA, const std::string &nameB) const
183 {
184 return ((m_body0->m_name == nameA) && (m_body1->m_name == nameB)) ||
185 ((m_body0->m_name == nameB) && (m_body1->m_name == nameA));
186 }
187 };
188
191 {
192 public:
193 RayTestResult(RigidBody *rigidBody) : m_rigidBody(rigidBody) {}
195 };
196
199 {
200 public:
203
211 void setGravity(float x, float y, float z);
212
222 bool newBoxShape(const std::string &name, float halfX, float halfY, float halfZ);
223
231 bool newSphereShape(const std::string &name, float radius);
232
241 bool newCylinderShape(const std::string &name, float radius, float height);
242
251 bool newConeShape(const std::string &name, float radius, float height);
252
259 bool deleteShape(const std::string &name);
260
266 size_t clearShapes();
267
273 size_t numShapes();
274
282 bool newRigidBody(const std::string &name,
283 const std::string &shapeName,
284 const yourgame::math::Trafo &trafo,
285 const RigidBodyInfo &info);
286
293 bool deleteRigidBody(const std::string &name);
294
301 yourgame::util::RigidBody *getRigidBody(const std::string &name);
302
309 std::vector<RigidBody *> getRigidBodiesStartingWith(const std::string &name);
310
317
324
330 void tick(float dt);
331
337 std::vector<Collision> getCollisions();
338
344 RayTestResult rayTest(float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ);
345
346 private:
347 class impl;
348 std::unique_ptr<impl> pimpl;
349 };
350 } // namespace util
351} // namespace yourgame
352
353#endif
Definition trafo.h:33
Represents a Collision between two rigid bodies.
Definition physenv.h:148
bool involves2(const std::string &nameA, const std::string &nameB) const
Checks if this Collision happened between rigid bodies with names nameA and nameB.
Definition physenv.h:182
RigidBody * m_body1
Definition physenv.h:161
float m_impulse
Definition physenv.h:162
RigidBody * m_body0
Definition physenv.h:160
bool involves(const std::string &name) const
Checks if rigid body with name name is involved in this Collision.
Definition physenv.h:170
Collision(RigidBody *body0, RigidBody *body1, float impulse)
Constructor.
Definition physenv.h:157
Represents a Physical Environment.
Definition physenv.h:199
bool newRigidBody(const std::string &name, const std::string &shapeName, const yourgame::math::Trafo &trafo, const RigidBodyInfo &info)
Creates and stores a new rigid body.
bool newBoxShape(const std::string &name, float halfX, float halfY, float halfZ)
Creates and stores a new box shape to be referenced during rigid body creation.
bool deleteRigidBody(const std::string &name)
Deletes rigid body by name.
yourgame::util::RigidBody * getRigidBody(const std::string &name)
Returns rigid body by name.
std::vector< RigidBody * > getRigidBodiesStartingWith(const std::string &name)
Returns rigid bodies with names starting with name.
size_t clearRigidBodies()
Deletes all rigid bodies.
bool newCylinderShape(const std::string &name, float radius, float height)
Creates and stores a new cylinder shape to be referenced during rigid body creation.
bool newSphereShape(const std::string &name, float radius)
Creates and stores a new sphere shape to be referenced during rigid body creation.
bool newConeShape(const std::string &name, float radius, float height)
Creates and stores a new cone shape to be referenced during rigid body creation.
size_t numShapes()
Returns the number of present shapes.
void setGravity(float x, float y, float z)
Set the Gravity of the physical world.
RayTestResult rayTest(float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ)
Shoots a ray from eye to target into the world. Result contains first rigid body hit,...
size_t clearShapes()
Deletes all shapes.
size_t numRigidBodies()
Returns the number of present rigid bodies.
bool deleteShape(const std::string &name)
Deletes a shape by name.
std::vector< Collision > getCollisions()
Returns the collisions of rigid bodies since last tick()
void tick(float dt)
Advances the physical world dt seconds.
Represents the result of a ray test.
Definition physenv.h:191
RayTestResult(RigidBody *rigidBody)
Definition physenv.h:193
RigidBody * m_rigidBody
Definition physenv.h:194
Represents a rigid body, holding a void pointer to an underlying implementation.
Definition physenv.h:49
yourgame::math::Trafo getTrafo()
Get transform (Trafo) calculated from rigid body.
RigidBody(std::string name, void *body)
Constructor.
Definition physenv.h:57
void setAngularFactor(float angFac)
Set the Angular Factor of the rigid body.
void applyCentralImpulse(float x, float y, float z)
Apply central impulse to the rigid body.
void setRestitution(float restitution)
Set the restitution of the rigid body.
void setTrafo(yourgame::math::Trafo trafo)
Set transform (Trafo) of rigid body.
void *const m_body
Definition physenv.h:143
void setSleepingThresholds(float linear, float angular)
Set the Sleeping Thresholds of the rigid body.
void setAngularFactors(float x, float y, float z)
Set the Angular Factors (x,y,z) of the rigid body.
void applyCentralForce(float x, float y, float z)
Apply central force to the rigid body.
void setFriction(float friction)
Set the friction of the rigid body.
std::string m_name
Definition physenv.h:142
void setLinearFactors(float x, float y, float z)
Set the Linear Factors (x,y,z) of the rigid body.
Definition audio.h:27
Definition physenv.h:33
float linearSleepingThreshold
Definition physenv.h:41
float spinningFriction
Definition physenv.h:39
float angularSleepingThreshold
Definition physenv.h:42
float friction
Definition physenv.h:37
float restitution
Definition physenv.h:40
float angularDamping
Definition physenv.h:36
float linearDamping
Definition physenv.h:35
float mass
Definition physenv.h:34
bool kinematic
Definition physenv.h:43
float rollingFriction
Definition physenv.h:38
bool disableDeactivation
Definition physenv.h:44