added setRotation, lil cleanup

This commit is contained in:
Otto Naderer 2022-05-01 12:15:25 +02:00
parent 41d9752cc1
commit d952791303
2 changed files with 76 additions and 60 deletions

View File

@ -110,6 +110,7 @@ class irrDynamics
static void applyTorque(irr::scene::ISceneNode* node, const irr::core::vector3df& torque);
static void setDamping(irr::scene::ISceneNode* node, irr::f32 linearDamping, irr::f32 angularDamping);
static void setPosition(irr::scene::ISceneNode* node, const irr::core::vector3df& newPos);
static void setRotation(irr::scene::ISceneNode* node, const irr::core::vector3df& newRotation);
//! Change default gravity
/** Per default, irrDynamics is set to an earth-like gravitational force. Modify that for other planets
* or your space sim game.

View File

@ -1,31 +1,35 @@
/*
irrDynamics - Light-weight Bullet Physics wrapper for the irrlicht graphics engine
Copyright (C) 2014 Otto Naderer - otto.naderer@aec.at
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
Copyright (C) 2014 Otto Naderer - otto@socialnerds.org
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <btBulletCollisionCommon.h>
#include <btBulletDynamicsCommon.h>
#include "irrDynamics.h"
#include <iostream>
#include <btBulletCollisionCommon.h>
#include <btBulletDynamicsCommon.h>
using namespace std;
using namespace irr;
irrDynamics* irrDynamics::instance = NULL;
irrDynamics* irrDynamics::instance = nullptr;
irrDynamics::irrDynamics() : lastStep(0)
{
@ -52,7 +56,6 @@ void irrDynamics::simStep(u32 curTimeStamp)
if (inst->lastStep == 0)
inst->lastStep = curTimeStamp;
// printf("NUMCONST: %i\n", inst->world->getNumConstraints());
inst->world->stepSimulation((curTimeStamp - inst->lastStep) * 0.001f, 5);
inst->updateObjects();
@ -68,7 +71,7 @@ void irrDynamics::shutdown()
delete inst->dispatcher;
delete inst->broadPhase;
delete inst->collisionConfiguration;
instance = 0;
instance = nullptr;
}
void irrDynamics::addTerrain(scene::ITerrainSceneNode* terrain, u32 lodLevel)
@ -93,7 +96,7 @@ void irrDynamics::addTerrain(scene::ITerrainSceneNode* terrain, u32 lodLevel)
core::vector3df terrScale = terrain->getScale();
//Build the triangleMesh
const u32 indexCount = buffer->getIndexCount();
printf("indeces: %u\n", indexCount);
//fprintf("indeces: %u\n", indexCount);
video::S3DVertex2TCoords* mb_vertices = (irr::video::S3DVertex2TCoords*) buffer->getVertexBuffer().getData();
u32* mb_indices = (u32*) buffer->getIndices();
for (j = 0; (u32) j < indexCount; j += 3)
@ -111,7 +114,7 @@ void irrDynamics::addTerrain(scene::ITerrainSceneNode* terrain, u32 lodLevel)
btBvhTriangleMeshShape* mShape = new btBvhTriangleMeshShape(mTriMesh, true);
btRigidBody* rbody = new btRigidBody(0.f, MotionState, mShape);
inst->world->addRigidBody(rbody);
inst->objects.insert(std::pair<scene::ISceneNode*, btRigidBody*>(terrain, rbody));
inst->objects[terrain] = rbody;
terrain->grab();
}
@ -128,7 +131,7 @@ void irrDynamics::setGravity(f32 newGravity)
void irrDynamics::updateObjects()
{
for (std::map<scene::ISceneNode*, btRigidBody*>::iterator iter = objects.begin(); iter != objects.end(); iter++)
for (auto iter = objects.begin(); iter != objects.end(); iter++)
{
btVector3 Point = iter->second->getCenterOfMassPosition();
iter->first->setPosition(core::vector3df((f32)Point[0], (f32)Point[1], (f32)Point[2]));
@ -146,7 +149,7 @@ void irrDynamics::updateObjects()
void irrDynamics::removeObject(scene::ISceneNode* node)
{
irrDynamics* inst = getInstance();
std::map<scene::ISceneNode*, btRigidBody*>::iterator iter = inst->objects.find(node);
auto iter = inst->objects.find(node);
if (iter != inst->objects.end())
{
inst->removeConstraints(iter->second);
@ -161,13 +164,13 @@ void irrDynamics::removeObject(scene::ISceneNode* node)
}
else
{
printf("irrdynamics: object not found in map!\n");
cout << "irrdynamics: object not found in map!" << endl;
}
}
void irrDynamics::clearObjects()
{
for (std::map<scene::ISceneNode*, btRigidBody*>::iterator iter = objects.begin(); iter != objects.end(); iter++)
for (auto iter = objects.begin(); iter != objects.end(); iter++)
{
removeConstraints(iter->second);
world->removeRigidBody(iter->second);
@ -204,7 +207,7 @@ btRigidBody* irrDynamics::addSphericalObject(scene::ISceneNode* node, f32 radius
// Add it to the world
inst->world->addRigidBody(rigidBody);
inst->objects.insert(std::pair<scene::ISceneNode*, btRigidBody*>(node, rigidBody));
inst->objects[node] = rigidBody;
node->grab();
return rigidBody;
}
@ -246,7 +249,7 @@ btRigidBody* irrDynamics::addBoxObject(scene::ISceneNode* node, f32 mass)
// Add it to the world
inst->world->addRigidBody(rigidBody);
inst->objects.insert(std::pair<scene::ISceneNode*, btRigidBody*>(node, rigidBody));
inst->objects[node] = rigidBody;
node->grab();
return rigidBody;
}
@ -261,13 +264,13 @@ bool irrDynamics::createHingeConstraint(scene::ISceneNode* nodeA, scene::ISceneN
if (iterA == inst->objects.end())
{
printf("irrdynamics: Unable to find first node for constraint!\n");
cout << "irrdynamics: Unable to find first node for constraint!" << endl;
return false;
}
if (iterB == inst->objects.end())
{
printf("irrdynamics: Unable to find second node for constraint!\n");
cout << "irrdynamics: Unable to find second node for constraint!" << endl;
return false;
}
@ -282,19 +285,18 @@ bool irrDynamics::createPoint2PointConstraint(scene::ISceneNode* nodeA, scene::I
{
irrDynamics* inst = getInstance();
//find the corresponding rigid bodies:
std::map<scene::ISceneNode*, btRigidBody*>::iterator iterA, iterB;
iterA = inst->objects.find(nodeA);
iterB = inst->objects.find(nodeB);
auto iterA = inst->objects.find(nodeA);
auto iterB = inst->objects.find(nodeB);
if (iterA == inst->objects.end())
{
printf("irrdynamics: Unable to find first node for constraint!\n");
cout << "irrdynamics: Unable to find first node for constraint!" << endl;
return false;
}
if (iterB == inst->objects.end())
{
printf("irrdynamics: Unable to find second node for constraint!\n");
cout << "irrdynamics: Unable to find second node for constraint!" << endl;
return false;
}
@ -314,19 +316,18 @@ bool irrDynamics::createSliderConstraint(scene::ISceneNode* nodeA,
{
irrDynamics* inst = getInstance();
//find the corresponding rigid bodies:
std::map<scene::ISceneNode*, btRigidBody*>::iterator iterA, iterB;
iterA = inst->objects.find(nodeA);
iterB = inst->objects.find(nodeB);
auto iterA = inst->objects.find(nodeA);
auto iterB = inst->objects.find(nodeB);
if (iterA == inst->objects.end())
{
printf("irrdynamics: Unable to find first node for constraint!\n");
cout << "irrdynamics: Unable to find first node for constraint!" << endl;
return false;
}
if (iterB == inst->objects.end())
{
printf("irrdynamics: Unable to find second node for constraint!\n");
cout << "irrdynamics: Unable to find second node for constraint!" << endl;
return false;
}
@ -359,11 +360,10 @@ void irrDynamics::applyCentralForce(scene::ISceneNode* node, const core::vector3
{
irrDynamics* inst = getInstance();
//find the corresponding rigid body:
std::map<scene::ISceneNode*, btRigidBody*>::iterator iter;
iter = inst->objects.find(node);
auto iter = inst->objects.find(node);
if (iter == inst->objects.end())
{
printf("irrdynamics: Unable to find node in list. Force application aborted.\n");
cout << "irrdynamics: Unable to find node in list. Force application aborted." << endl;
return;
}
@ -375,11 +375,10 @@ void irrDynamics::applyCentralImpulse(scene::ISceneNode* node, const core::vecto
{
irrDynamics* inst = getInstance();
//find the corresponding rigid body:
std::map<scene::ISceneNode*, btRigidBody*>::iterator iter;
iter = inst->objects.find(node);
auto iter = inst->objects.find(node);
if (iter == inst->objects.end())
{
printf("irrdynamics: Unable to find node in list. Impulse application aborted.\n");
cout << "irrdynamics: Unable to find node in list. Impulse application aborted." << endl;
return;
}
@ -391,11 +390,10 @@ void irrDynamics::applyTorque(scene::ISceneNode* node, const core::vector3df& to
{
irrDynamics* inst = getInstance();
//find the corresponding rigid body:
std::map<scene::ISceneNode*, btRigidBody*>::iterator iter;
iter = inst->objects.find(node);
auto iter = inst->objects.find(node);
if (iter == inst->objects.end())
{
printf("irrdynamics: Unable to find node in list. Torque application aborted.\n");
cout << "irrdynamics: Unable to find node in list. Torque application aborted." << endl;
return;
}
@ -432,11 +430,10 @@ void irrDynamics::setDamping(scene::ISceneNode* node, f32 linearDamping, f32 ang
{
irrDynamics* inst = getInstance();
//find the corresponding rigid body:
std::map<scene::ISceneNode*, btRigidBody*>::iterator iter;
iter = inst->objects.find(node);
auto iter = inst->objects.find(node);
if (iter == inst->objects.end())
{
printf("irrdynamics: Unable to find node in list. Damping application aborted.\n");
cout << "irrdynamics: Unable to find node in list. Damping application aborted." << endl;
return;
}
@ -447,23 +444,41 @@ void irrDynamics::setPosition(scene::ISceneNode* node, const core::vector3df& ne
{
irrDynamics* inst = getInstance();
//find the corresponding rigid body:
std::map<scene::ISceneNode*, btRigidBody*>::iterator iter;
iter = inst->objects.find(node);
auto iter = inst->objects.find(node);
if (iter == inst->objects.end())
{
printf("irrdynamics: Unable to find node in list. Position update aborted.\n");
cout << "irrdynamics: Unable to find node in list. Position update aborted." << endl;
return;
}
btTransform transform = iter->second->getCenterOfMassTransform();
btTransform transform = iter->second->getWorldTransform();
transform.setOrigin(btVector3(newPos.X, newPos.Y, newPos.Z));
iter->second->setWorldTransform(transform);
}
void irrDynamics::setRotation(scene::ISceneNode* node, const core::vector3df& newRotation)
{
irrDynamics* inst = getInstance();
//find the corresponding rigid body:
auto iter = inst->objects.find(node);
if (iter == inst->objects.end())
{
cout << "irrdynamics: Unable to find node in list. Rotation update aborted." << endl;
return;
}
btTransform transform; // = iter->second->getWorldTransform();
transform.setIdentity();
btQuaternion quat;
quat.setEuler(core::degToRad(newRotation.Y), core::degToRad(newRotation.X), core::degToRad(newRotation.Z));
transform.setRotation(quat);
transform.setOrigin(iter->second->getCenterOfMassPosition());
iter->second->setCenterOfMassTransform(transform);
}
void irrDynamics::removeConstraints(btRigidBody* rigidBody)
{
const int len = rigidBody->getNumConstraintRefs();
printf("CONSTREF A: %i\n", rigidBody->getNumConstraintRefs());
//printf("CONSTREF A: %i\n", rigidBody->getNumConstraintRefs());
//first, remove constraint reference at the other node
for (int i = 0; i < len; i++)
{
@ -474,7 +489,7 @@ void irrDynamics::removeConstraints(btRigidBody* rigidBody)
bodyB->removeConstraintRef(constraint);
}
printf("CONSTREF B: %i\n", rigidBody->getNumConstraintRefs());
//printf("CONSTREF B: %i\n", rigidBody->getNumConstraintRefs());
//then remove/delete at our side
while (rigidBody->getNumConstraintRefs() > 0)
{
@ -482,6 +497,6 @@ void irrDynamics::removeConstraints(btRigidBody* rigidBody)
rigidBody->removeConstraintRef(constraint);
world->removeConstraint(constraint);
delete constraint;
printf("DELETED\n");
//printf("DELETED\n");
}
}