4#include <ext/reactphysics3d/src/body/Body.h>
5#include <ext/reactphysics3d/src/body/CollisionBody.h>
6#include <ext/reactphysics3d/src/collision/narrowphase/DefaultCollisionDispatch.h>
7#include <ext/reactphysics3d/src/collision/narrowphase/NarrowPhaseAlgorithm.h>
8#include <ext/reactphysics3d/src/collision/shapes/CollisionShape.h>
9#include <ext/reactphysics3d/src/collision/CollisionCallback.h>
10#include <ext/reactphysics3d/src/collision/NarrowPhaseInfo.h>
11#include <ext/reactphysics3d/src/collision/ProxyShape.h>
12#include <ext/reactphysics3d/src/constraint/ContactPoint.h>
13#include <ext/reactphysics3d/src/engine/CollisionWorld.h>
14#include <ext/reactphysics3d/src/engine/DynamicsWorld.h>
15#include <ext/reactphysics3d/src/mathematics/Transform.h>
16#include <ext/reactphysics3d/src/mathematics/Vector3.h>
17#include <ext/reactphysics3d/src/memory/MemoryAllocator.h>
58constexpr uint16_t Body::TYPEIDS_ALL;
59constexpr uint16_t Body::TYPEID_STATIC;
60constexpr uint16_t Body::TYPEID_DYNAMIC;
62Body::Body(
World* world,
const string&
id,
int type,
bool enabled, uint16_t collisionTypeId,
const Transformations& transformations,
float restitution,
float friction,
float mass,
const Vector3& inertiaTensor,
const vector<BoundingVolume*> boundingVolumes)
74 this->
rigidBody = this->world->
world.createRigidBody(reactphysics3d::Transform());
75 this->
rigidBody->setType(reactphysics3d::BodyType::STATIC);
79 this->
rigidBody = this->world->
world.createRigidBody(reactphysics3d::Transform());
80 this->
rigidBody->setType(reactphysics3d::BodyType::DYNAMIC);
84 this->
rigidBody = this->world->
world.createRigidBody(reactphysics3d::Transform());
85 this->
rigidBody->setType(reactphysics3d::BodyType::KINEMATIC);
90 this->
collisionBody = this->world->
world.createCollisionBody(reactphysics3d::Transform());
93 Console::println(
"Body::Body(): unsupported type: " + to_string(
type) +
": using collision body");
95 this->
collisionBody = this->world->
world.createCollisionBody(reactphysics3d::Transform());
99 rigidBody->getMaterial().setFrictionCoefficient(friction);
100 rigidBody->getMaterial().setBounciness(restitution);
105 this->boundingVolumes.push_back(
dynamic_cast<TerrainMesh*
>(boundingVolume) !=
nullptr?boundingVolume:boundingVolume->
clone());
113 if (
dynamic_cast<TerrainMesh*
>(boundingVolume) !=
nullptr &&
cloned ==
true)
continue;
114 delete boundingVolume;
119 return Vector3(0.0f, 0.0f, 0.0f);
129 scaleXAxis > Math::EPSILON &&
mass > Math::EPSILON?1.0f / (scaleXAxis * 1.0f / 12.0f *
mass * (height * height + depth * depth)):0.0f,
134 scaleYAxis > Math::EPSILON &&
mass > Math::EPSILON?1.0f / (scaleYAxis * 1.0f / 12.0f *
mass * (width * width + depth * depth)):0.0f,
139 scaleZAxis > Math::EPSILON &&
mass > Math::EPSILON?1.0f / (scaleZAxis * 1.0f / 12.0f *
mass * (width * width + height * height)):0.0f,
183 proxyShape->setCollisionCategoryBits(typeId);
196 proxyShape->setCollideWithMaskBits(collisionTypeIds);
228 rigidBody->removeCollisionShape(proxyShape);
244 if (
mass > Math::EPSILON) {
246 float volumeTotal = 0.0f;
249 boundingVolume->boundingBoxTransformed.getDimensions().getX() *
250 boundingVolume->boundingBoxTransformed.getDimensions().getY() *
251 boundingVolume->boundingBoxTransformed.getDimensions().getZ();
255 reactphysics3d::ProxyShape* proxyShape =
nullptr;
256 float volumeBoundingVolume =
257 boundingVolume->boundingBoxTransformed.getDimensions().getX() *
258 boundingVolume->boundingBoxTransformed.getDimensions().getY() *
259 boundingVolume->boundingBoxTransformed.getDimensions().getZ();
261 proxyShape =
rigidBody->addCollisionShape(boundingVolume->collisionShape, boundingVolume->collisionShapeLocalTransform,
mass / volumeTotal * volumeBoundingVolume);
263 proxyShape =
collisionBody->addCollisionShape(boundingVolume->collisionShape, boundingVolume->collisionShapeLocalTransform);
270 reactphysics3d::ProxyShape* proxyShape =
nullptr;
272 proxyShape =
rigidBody->addCollisionShape(boundingVolume->collisionShape, boundingVolume->collisionShapeLocalTransform, 0.0f);
274 proxyShape =
collisionBody->addCollisionShape(boundingVolume->collisionShape, boundingVolume->collisionShapeLocalTransform);
291 reactphysics3d::Transform transform;
297 reactphysics3d::Matrix3x3(
298 inverseInertiaMatrixArray[0],
299 inverseInertiaMatrixArray[1],
300 inverseInertiaMatrixArray[2],
301 inverseInertiaMatrixArray[4],
302 inverseInertiaMatrixArray[5],
303 inverseInertiaMatrixArray[6],
304 inverseInertiaMatrixArray[8],
305 inverseInertiaMatrixArray[9],
306 inverseInertiaMatrixArray[10]
331 Console::println(
"Body::getFriction(): no rigid body attached");
334 return rigidBody->getMaterial().getFrictionCoefficient();
340 Console::println(
"Body::setFriction(): no rigid body attached");
343 rigidBody->getMaterial().setFrictionCoefficient(friction);
349 Console::println(
"Body::getRestitution(): no rigid body attached");
352 return rigidBody->getMaterial().getBounciness();
358 Console::println(
"Body::setRestitution(): no rigid body attached");
361 rigidBody->getMaterial().setBounciness(restitution);
373 Console::println(
"Body::setMass(): no rigid body attached");
382 Console::println(
"Body::getLinearVelocity(): no rigid body attached");
394 Console::println(
"Body::setLinearVelocity(): no rigid body attached");
397 rigidBody->setLinearVelocity(reactphysics3d::Vector3(linearVelocity.
getX(), linearVelocity.
getY(), linearVelocity.
getZ()));
403 Console::println(
"Body::getAngularVelocity(): no rigid body attached");
415 Console::println(
"Body::setAngularVelocity(): no rigid body attached");
418 rigidBody->setAngularVelocity(reactphysics3d::Vector3(angularVelocity.
getX(), angularVelocity.
getY(), angularVelocity.
getZ()));
424 Console::println(
"Body::getLinearDamping(): no rigid body attached");
433 Console::println(
"Body::setLinearDamping(): no rigid body attached");
436 rigidBody->setLinearDamping(linearDamping);
442 Console::println(
"Body::getAngularDamping(): no rigid body attached");
451 Console::println(
"Body::setAngularDamping(): no rigid body attached");
454 rigidBody->setAngularDamping(angularDamping);
494 reactphysics3d::Transform(
495 reactphysics3d::Vector3(
497 this->transformations.getTranslation().getY(),
498 this->transformations.getTranslation().getZ()
500 reactphysics3d::Quaternion(
501 this->transformations.getRotationsQuaternion().getX(),
502 this->transformations.getRotationsQuaternion().getY(),
503 this->transformations.getRotationsQuaternion().getZ(),
504 this->transformations.getRotationsQuaternion().getW()
513 Console::println(
"Body::addForce(): no rigid body attached");
517 reactphysics3d::Vector3(force.
getX(), force.
getY(), force.
getZ()),
518 reactphysics3d::Vector3(forceOrigin.
getX(), forceOrigin.
getY(), forceOrigin.
getZ())
525 Console::println(
"Body::addForce(): no rigid body attached");
529 reactphysics3d::Vector3(force.
getX(), force.
getY(), force.
getZ())
536 Console::println(
"Body::addTorque(): no rigid body attached");
540 reactphysics3d::Vector3(torque.
getX(), torque.
getY(), torque.
getZ())
557 listener->onCollision(
this, other, collisionResponse);
564 listener->onCollisionBegin(
this, other, collisionResponse);
571 listener->onCollisionEnd(
this, other);
Dynamic rigid/static rigid/collision body class.
vector< BoundingVolume * > boundingVolumes
uint16_t getCollisionTypeIds()
void setCloned(bool cloned)
Set cloned.
vector< CollisionListener * > collisionListener
void addTorque(const Vector3 &torque)
Add torque.
reactphysics3d::RigidBody * rigidBody
void fireOnCollisionBegin(Body *other, CollisionResponse &collisionResponse)
Fire on collision begin.
void setMass(float mass)
Set up mass.
void resetProxyShapes()
Reset proxy shapes.
void setAngularVelocity(const Vector3 &angularVelocity)
Set angular velocity.
vector< BoundingVolume * > & getBoundingVolumes()
Transformations transformations
BoundingBox computeBoundingBoxTransformed()
Compute bounding box transformed.
void setEnabled(bool enabled)
Set up if rigid body is enabled.
static Matrix4x4 computeInverseInertiaMatrix(BoundingBox *boundingBox, float mass, float scaleXAxis, float scaleYAxis, float scaleZAxis)
Computes the inverse inertia matrix.
void setLinearVelocity(const Vector3 &linearVelocity)
Set linear velocity.
void setFriction(float friction)
Set up friction.
void setRestitution(float restitution)
Set up restitution.
void setSleeping(bool sleeping)
Set sleeping.
reactphysics3d::CollisionBody * collisionBody
void setCollisionTypeId(uint16_t typeId)
Set collision type id.
void addForce(const Vector3 &forceOrigin, const Vector3 &force)
Add force.
static constexpr int32_t TYPE_DYNAMIC
int getType()
Return type, see TYPE_*.
void setLinearDamping(float linearDamping)
Set linear damping.
void removeCollisionListener(CollisionListener *listener)
Remove a collision listener to this rigid body.
void fireOnCollisionEnd(Body *other)
Fire on collision end.
void setAngularDamping(float angularDamping)
Set angular damping.
static constexpr int32_t TYPE_KINEMATIC
const Transformations & getTransformations()
const Vector3 getAngularVelocity()
const string & getRootId()
vector< reactphysics3d::ProxyShape * > proxyShapes
static const Vector3 getNoRotationInertiaTensor()
void setCollisionTypeIds(uint16_t collisionTypeIds)
Set up collision type ids.
Vector3 transformationsScale
void fromTransformations(const Transformations &transformations)
Synchronizes this rigid body with transformations.
void addCollisionListener(CollisionListener *listener)
Add a collision listener to this rigid body.
float getAngularDamping()
uint16_t getCollisionTypeId()
void fireOnCollision(Body *other, CollisionResponse &collisionResponse)
Fire on collision.
static constexpr int32_t TYPE_STATIC
const Vector3 getLinearVelocity()
static constexpr int32_t TYPE_COLLISION
void setRootId(const string &rootId)
Set root id.
Dynamic physics world class.
reactphysics3d::DynamicsWorld world
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
const Vector3 & getDimensions() const
Bounding volume interface.
Capsule physics primitive.
Oriented bounding box physics primitive.
Terrain mesh physics primitive.
BoundingVolume * clone() const override
Clones this bounding volume.
array< float, 16 > & getArray() const
Returns array data.
bool equals(const Vector3 &v) const
Compares this vector with given vector.
Vector3 & set(float x, float y, float z)
Set up vector.