From d952791303b4a90715ff8475dc638104536ab8bb Mon Sep 17 00:00:00 2001 From: Otto Naderer Date: Sun, 1 May 2022 12:15:25 +0200 Subject: [PATCH] added setRotation, lil cleanup --- include/irrDynamics.h | 1 + src/irrDynamics.cpp | 135 +++++++++++++++++++++++------------------- 2 files changed, 76 insertions(+), 60 deletions(-) diff --git a/include/irrDynamics.h b/include/irrDynamics.h index 2abe03a..b47784c 100644 --- a/include/irrDynamics.h +++ b/include/irrDynamics.h @@ -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. diff --git a/src/irrDynamics.cpp b/src/irrDynamics.cpp index 285a32e..67f443a 100644 --- a/src/irrDynamics.cpp +++ b/src/irrDynamics.cpp @@ -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 -#include #include "irrDynamics.h" +#include + +#include +#include + +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(terrain, rbody)); + inst->objects[terrain] = rbody; terrain->grab(); } @@ -128,7 +131,7 @@ void irrDynamics::setGravity(f32 newGravity) void irrDynamics::updateObjects() { - for (std::map::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::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::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(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(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::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::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::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::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::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::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::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"); } }