ComPlicitNgine, Weld

This commit is contained in:
floralrainfall 2023-07-20 16:19:57 -04:00
parent a203911dee
commit 7184da29cc
13 changed files with 266 additions and 109 deletions

View File

@ -30,9 +30,11 @@ int main(int argc, char** argv)
window.updateTree(world->getDatamodel());
window.ogreWidget->setWorld(world);
RNR::ComPlicitNgine* ngine = world->getComPlicitNgine();
while (window.isVisible())
{
window.statusBar()->showMessage(QString::asprintf("Dt=%f, Rt=%f, Pt=%f, FPS=%f, pFPS=%f", window.ogreWidget->delta, window.ogreWidget->render_time, window.ogreWidget->world->getPhysicsTime(), 1 / window.ogreWidget->delta, 1 / window.ogreWidget->world->getLastPhysicsDelta()));
window.statusBar()->showMessage(QString::asprintf("Dt=%f, Rt=%f, Pt=%f, FPS=%f, pFPS=%f", window.ogreWidget->delta, window.ogreWidget->render_time, ngine->getPhysicsTime(), 1 / window.ogreWidget->delta, 1 / ngine->getLastPhysicsDelta()));
app.processEvents();
window.ogreWidget->render();
world->update();

View File

@ -27,6 +27,8 @@ add_library(Engine STATIC
Header/App/V8/Tree/PVInstance.hpp
Header/App/V8/Tree/ModelInstance.hpp
Header/App/V8/World/World.hpp
Header/App/V8/World/Weld.hpp
Header/App/V8/World/ComPlicitNgine.hpp
Header/App/CoordinateFrame.hpp
Header/App/BrickColor.hpp
Header/App/InputManager.hpp
@ -64,6 +66,8 @@ add_library(Engine STATIC
Source/App/BrickColor.cpp
Source/App/InputManager.cpp
Source/App/V8/World/World.cpp
Source/App/V8/World/Weld.cpp
Source/App/V8/World/ComPlicitNgine.cpp
Source/Network/GUID.cpp
Source/Network/Player.cpp
Source/Network/Players.cpp

View File

@ -5,7 +5,7 @@
namespace RNR
{
class FaceInstance : public Instance, Ogre::ManualObject
class FaceInstance : public Instance
{
private:
NormalId m_face;

View File

@ -0,0 +1,40 @@
#pragma once
#include <thread>
#include <OGRE/Ogre.h>
#include <App/V8/DataModel/PartInstance.hpp>
#include "LinearMath/btVector3.h"
#include "btBulletDynamicsCommon.h"
#include <map>
namespace RNR
{
class World;
class ComPlicitNgine
{
std::thread m_physicsThread;
Ogre::Timer* m_physicsTimer;
float m_lastPhysicsDelta;
float m_physicsTime;
btDiscreteDynamicsWorld* m_dynamicsWorld;
World* m_world;
std::map<PartInstance*, btRigidBody*> m_physicsParts;
void thread();
public:
ComPlicitNgine(World* world);
~ComPlicitNgine();
void updateTree();
void updateTreeRender();
Ogre::Timer* getPhysicsTimer() { return m_physicsTimer; }
float getLastPhysicsDelta() { return m_lastPhysicsDelta; }
float getPhysicsTime() { return m_physicsTime; }
btRigidBody* getBody(PartInstance* part) { return m_physicsParts[part]; };
void registerPhysicsPart(PartInstance* partRegistered);
void deletePhysicsPart(PartInstance* partDelete);
};
}

View File

@ -0,0 +1,28 @@
#pragma once
#include <App/V8/Tree/Instance.hpp>
#include <App/V8/DataModel/PartInstance.hpp>
#include <App/V8/World/ComPlicitNgine.hpp>
namespace RNR
{
class Weld : public Instance
{
btRigidBody* m_aBody;
PartInstance* m_aInstance;
btRigidBody* m_bBody;
PartInstance* m_bInstance;
btFixedConstraint* m_constraint;
public:
Weld();
~Weld();
virtual std::string getClassName() { return "Weld"; }
void weld(PartInstance* a, PartInstance* b);
void create();
void destroy();
bool getBroken();
};
}

View File

@ -6,6 +6,7 @@
#include <App/V8/DataModel/Camera.hpp>
#include <App/V8/DataModel/RunService.hpp>
#include <App/V8/DataModel/DataModel.hpp>
#include <App/V8/World/ComPlicitNgine.hpp>
#include <Network/Players.hpp>
#include <App/GUI/TopMenuBar.hpp>
#include <OGRE/Ogre.h>
@ -14,7 +15,6 @@
#include "LinearMath/btVector3.h"
#include "btBulletDynamicsCommon.h"
namespace RNR
{
class IInputManager;
@ -30,9 +30,6 @@ namespace RNR
class World
{
private:
std::thread m_physicsThread;
Ogre::Timer* m_physicsTimer;
bool m_runPhysics;
std::map<std::string, Instance*> m_refs;
std::stack<WorldUndeserialized> m_undeserialized;
@ -47,8 +44,7 @@ namespace RNR
InstanceFactory* m_instanceFactory;
IInputManager* m_inputManager;
float m_lastDelta;
float m_lastPhysicsDelta;
float m_physicsTime;
ComPlicitNgine* m_ngine;
void xmlAddItem(pugi::xml_node node, Instance* parent);
public:
@ -65,8 +61,9 @@ namespace RNR
void update();
btDiscreteDynamicsWorld* getDynamicsWorld() { return m_dynamicsWorld; }
ComPlicitNgine* getComPlicitNgine() { return m_ngine; }
float getLastDelta() { return m_lastDelta; }
float getLastPhysicsDelta() { return m_lastPhysicsDelta; }
float getLastPhysicsDelta() { return m_ngine->getLastPhysicsDelta(); }
DataModel* getDatamodel() { return m_datamodel; }
void setInputManager(IInputManager* inputManager) { m_inputManager = inputManager; }
IInputManager* getInputManager() { return m_inputManager; }
@ -77,14 +74,8 @@ namespace RNR
void setRunService(RunService* runService) { m_runService = runService; }
Ogre::Root* getOgreRoot() { return m_ogreRoot; }
Ogre::SceneManager* getOgreSceneManager() { return m_ogreSceneManager; }
Ogre::Timer* getPhysicsTimer() { return m_physicsTimer; }
float getPhysicsTime() { return m_physicsTime; }
void setPhysicsTime(float newTime) { m_physicsTime = newTime; } // should only be used by physicsThread
bool getPhysicsShouldBeRunningPleaseStopIfItIsStillRunning() { return m_runPhysics; }
void registerPhysicsPart(PartInstance* partRegistered);
void deletePhysicsPart(PartInstance* partDelete);
Lock physicsIterateLock;
};
}

View File

@ -1,5 +1,6 @@
#include <App/Humanoid/Humanoid.hpp>
#include <App/V8/World/World.hpp>
#include <App/V8/World/Weld.hpp>
#include <App/InputManager.hpp>
namespace RNR
@ -24,7 +25,13 @@ namespace RNR
void Humanoid::buildJoints()
{
//
if(getTorso() && getHead())
{
Weld* headWeld = new Weld();
headWeld->weld(getTorso(), getHead());
headWeld->create();
headWeld->setParent(getTorso());
}
}
void Humanoid::checkForJointDeath()
@ -118,7 +125,7 @@ namespace RNR
move *= world->getLastDelta();
bool move_valid = true;
// TODO: collision checking
if(!move_valid)

View File

@ -6,10 +6,11 @@
namespace RNR
{
FaceInstance::FaceInstance() : Ogre::ManualObject(Strings::random_hex(8))
FaceInstance::FaceInstance()
{
setNode(world->getOgreSceneManager()->getRootSceneNode()->createChildSceneNode());
getNode()->attachObject(this);
build();
}
}

View File

@ -60,7 +60,7 @@ namespace RNR
child_node->setVisible(true);
break;
}
world->registerPhysicsPart(part);
world->getComPlicitNgine()->registerPhysicsPart(part);
}
}
@ -81,7 +81,7 @@ namespace RNR
{
PartInstance* part = dynamic_cast<PartInstance*>(childRemoved);
if(part)
world->deletePhysicsPart(part);
world->getComPlicitNgine()->deletePhysicsPart(part);
m_geomDirty = true;
}

View File

@ -0,0 +1,119 @@
#include <App/V8/World/ComPlicitNgine.hpp>
#include <App/V8/World/World.hpp>
#include <Helpers/Bullet.hpp>
namespace RNR
{
void ComPlicitNgine::thread()
{
float delta;
float time;
while(m_world->getPhysicsShouldBeRunningPleaseStopIfItIsStillRunning())
{
delta = m_physicsTimer->getMicroseconds() / 1000000.0;
time += m_physicsTimer->getMilliseconds() / 1000.0;
m_physicsTimer->reset();
m_lastPhysicsDelta = delta;
m_physicsTime = time;
m_world->preStep();
m_world->step(delta);
m_lastPhysicsDelta = delta;
}
}
ComPlicitNgine::ComPlicitNgine(World* world)
{
m_physicsTimer = new Ogre::Timer();
m_physicsTime = 0.0;
m_physicsThread = std::thread(&ComPlicitNgine::thread, this);
m_dynamicsWorld = world->getDynamicsWorld();
m_world = world;
}
ComPlicitNgine::~ComPlicitNgine()
{
m_physicsThread.join();
}
void ComPlicitNgine::updateTreeRender()
{
m_world->physicsIterateLock.lock();
for(int j = m_dynamicsWorld->getNumCollisionObjects() - 1; j >= 0; j--)
{
btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[j];
if(!obj->isActive())
continue;
PartInstance* part = (PartInstance*)obj->getUserPointer();
part->updateMatrix();
}
m_world->physicsIterateLock.unlock();
}
void ComPlicitNgine::updateTree()
{
for(int j = m_dynamicsWorld->getNumCollisionObjects() - 1; j >= 0; j--)
{
btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[j];
if(!obj->isActive())
continue;
btRigidBody* body = btRigidBody::upcast(obj);
btTransform trans;
if(body && body->getMotionState())
{
body->getMotionState()->getWorldTransform(trans);
}
else
{
trans = obj->getWorldTransform();
}
PartInstance* part = (PartInstance*)obj->getUserPointer();
part->getCFrame().setPosition(Bullet::v3ToOgre(trans.getOrigin()));
Ogre::Matrix3 partRot;
Ogre::Quaternion transOgre = Bullet::qtToOgre(trans.getRotation());
transOgre.ToRotationMatrix(partRot);
part->getCFrame().setRotation(partRot);
}
}
void ComPlicitNgine::registerPhysicsPart(PartInstance* partRegistered)
{
btCollisionShape* partShape = new btBoxShape(Bullet::v3ToBullet(partRegistered->getSize() / 2.f));
partShape->setUserPointer(partRegistered);
btTransform partTransform;
partTransform.setIdentity();
partTransform.setOrigin(Bullet::v3ToBullet(partRegistered->getPosition()));
partTransform.setRotation(Bullet::qtToBullet(partRegistered->getRotation()));
btScalar mass = partRegistered->getSize().length();
if(partRegistered->getAnchored())
mass = 0;
btVector3 localInertia = btVector3(0,0,0);
if(mass)
partShape->calculateLocalInertia(mass, localInertia);
btDefaultMotionState* partMotionState = new btDefaultMotionState(partTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, partMotionState, partShape, localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
body->setUserPointer(partRegistered);
m_world->physicsIterateLock.lock();
m_dynamicsWorld->addRigidBody(body);
m_world->physicsIterateLock.unlock();
m_physicsParts[partRegistered] = body;
}
void ComPlicitNgine::deletePhysicsPart(PartInstance* partDelete)
{
btRigidBody* toDelete = m_physicsParts[partDelete];
if(toDelete)
{
}
}
}

View File

@ -0,0 +1,45 @@
#include <App/V8/World/Weld.hpp>
#include <App/V8/World/World.hpp>
namespace RNR
{
Weld::Weld()
{
setName("Weld");
}
Weld::~Weld()
{
destroy();
}
void Weld::weld(PartInstance* a, PartInstance* b)
{
m_aInstance = a;
m_bInstance = b;
m_aBody = world->getComPlicitNgine()->getBody(a);
m_bBody = world->getComPlicitNgine()->getBody(b);
}
void Weld::create()
{
if(!m_aBody || !m_bBody)
return;
m_constraint = new btFixedConstraint(*m_aBody, *m_bBody, m_aBody->getWorldTransform(), m_bBody->getWorldTransform());
}
void Weld::destroy()
{
delete m_constraint;
}
bool Weld::getBroken()
{
if(!m_constraint)
return true;
return false;
}
}

View File

@ -12,21 +12,6 @@
namespace RNR
{
void physicsThread(World* world)
{
float delta;
float time;
while(world->getPhysicsShouldBeRunningPleaseStopIfItIsStillRunning())
{
delta = world->getPhysicsTimer()->getMicroseconds() / 1000000.0;
time += world->getPhysicsTimer()->getMilliseconds() / 1000.0;
world->setPhysicsTime(time);
world->getPhysicsTimer()->reset();
world->preStep();
world->step(delta);
}
}
World::World(Ogre::Root* ogre, Ogre::SceneManager* ogreSceneManager)
{
Instance::setWorld(this);
@ -62,10 +47,7 @@ namespace RNR
m_players = (Players*)m_datamodel->getService("Players");
m_runPhysics = true;
m_physicsTimer = new Ogre::Timer();
m_physicsThread = std::thread(physicsThread, this);
m_physicsTime = 0.0;
m_ngine = new ComPlicitNgine(this);
m_tmb = new TopMenuBar(this);
Camera* start_cam = new Camera();
@ -76,7 +58,7 @@ namespace RNR
World::~World()
{
m_runPhysics = false;
m_physicsThread.join();
delete m_ngine;
}
void World::xmlAddItem(pugi::xml_node node, Instance* parent)
@ -170,19 +152,8 @@ namespace RNR
m_tmb->frame();
m_lastDelta = timestep;
if(m_runService && m_runService->getRunning() && !m_runService->getPaused())
{
physicsIterateLock.lock();
for(int j = m_dynamicsWorld->getNumCollisionObjects() - 1; j >= 0; j--)
{
btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[j];
if(!obj->isActive())
continue;
PartInstance* part = (PartInstance*)obj->getUserPointer();
part->updateMatrix();
}
update();
physicsIterateLock.unlock();
}
m_ngine->updateTreeRender();
update();
}
void World::preStep()
@ -195,34 +166,11 @@ namespace RNR
if(m_runService && m_runService->getRunning() && !m_runService->getPaused())
{
m_runService->step(timestep);
m_dynamicsWorld->stepSimulation(timestep, 2);
physicsIterateLock.lock();
for(int j = m_dynamicsWorld->getNumCollisionObjects() - 1; j >= 0; j--)
{
btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[j];
if(!obj->isActive())
continue;
btRigidBody* body = btRigidBody::upcast(obj);
btTransform trans;
if(body && body->getMotionState())
{
body->getMotionState()->getWorldTransform(trans);
}
else
{
trans = obj->getWorldTransform();
}
PartInstance* part = (PartInstance*)obj->getUserPointer();
part->getCFrame().setPosition(Bullet::v3ToOgre(trans.getOrigin()));
Ogre::Matrix3 partRot;
Ogre::Quaternion transOgre = Bullet::qtToOgre(trans.getRotation());
transOgre.ToRotationMatrix(partRot);
part->getCFrame().setRotation(partRot);
}
m_dynamicsWorld->stepSimulation(timestep, 2);
physicsIterateLock.unlock();
m_ngine->updateTree();
}
m_lastPhysicsDelta = timestep;
return 0.0;
}
@ -231,34 +179,4 @@ namespace RNR
m_workspace->buildGeom();
}
void World::registerPhysicsPart(PartInstance* partRegistered)
{
btCollisionShape* partShape = new btBoxShape(Bullet::v3ToBullet(partRegistered->getSize() / 2.f));
partShape->setUserPointer(partRegistered);
btTransform partTransform;
partTransform.setIdentity();
partTransform.setOrigin(Bullet::v3ToBullet(partRegistered->getPosition()));
partTransform.setRotation(Bullet::qtToBullet(partRegistered->getRotation()));
btScalar mass = partRegistered->getSize().length();
if(partRegistered->getAnchored())
mass = 0;
btVector3 localInertia = btVector3(0,0,0);
if(mass)
partShape->calculateLocalInertia(mass, localInertia);
btDefaultMotionState* partMotionState = new btDefaultMotionState(partTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, partMotionState, partShape, localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
body->setUserPointer(partRegistered);
m_dynamicsWorld->addRigidBody(body);
}
void World::deletePhysicsPart(PartInstance* partDelete)
{
}
}

View File

@ -53,6 +53,8 @@ namespace RNR
Humanoid* character_humanoid = new Humanoid();
character_humanoid->setParent(m_character);
m_character->setParent(world->getWorkspace());
character_humanoid->buildJoints();
Camera* player_camera = world->getWorkspace()->getCurrentCamera();
player_camera->setCFrame(CoordinateFrame());