From 88f4b7e6a770df14d03805eabc7369fe19aa7eb2 Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Sun, 8 Apr 2018 17:01:03 +0100 Subject: [PATCH] [Change] Initial ode-less dynamic body work. [Remove] Removed ODE dep. [Missing] No collisions yet. --- configure.ac | 2 - src/dynamic_body.cpp | 115 ++++++++++++++++++++++------------- src/dynamic_body.h | 20 ++++-- src/frame.cpp | 4 +- src/frame.h | 4 -- src/libs.h | 5 -- src/main.cpp | 1 - src/model_body.cpp | 3 - src/model_body.h | 2 - src/model_coll_mesh_data.cpp | 9 +-- src/model_coll_mesh_data.h | 1 - src/planet.cpp | 7 --- src/planet.h | 1 - src/ship.cpp | 5 +- src/space.cpp | 9 +-- src/space.h | 1 - 16 files changed, 92 insertions(+), 97 deletions(-) diff --git a/configure.ac b/configure.ac index 90ac1fa..dcd2966 100644 --- a/configure.ac +++ b/configure.ac @@ -23,8 +23,6 @@ CFLAGS="$CFLAGS `pkg-config --cflags sigc++-2.0`" LIBS="$LIBS `pkg-config --libs sigc++-2.0` -lGL -lGLU" CFLAGS="$CFLAGS `pkg-config --cflags freetype2`" LIBS="$LIBS `pkg-config --libs freetype2`" -CFLAGS="$CFLAGS `ode-config --cflags`" -LIBS="$LIBS `ode-config --libs`" PKG_CHECK_MODULES(SDL, sdl) CFLAGS="$CFLAGS $SDL_CFLAGS" diff --git a/src/dynamic_body.cpp b/src/dynamic_body.cpp index 80832cd..246bf77 100644 --- a/src/dynamic_body.cpp +++ b/src/dynamic_body.cpp @@ -6,35 +6,44 @@ DynamicBody::DynamicBody(void) : ModelBody() { m_flags = Body::FLAG_CAN_MOVE_FRAME; - m_body = dBodyCreate(Space::world); - dMassSetBox(&m_mass, 1, 50, 50, 50); - dMassAdjust(&m_mass, 1.0f); - dBodySetMass(m_body, &m_mass); + m_orient = matrix4x4d::Identity(); + m_force = vector3d(0.0); + m_torque = vector3d(0.0); + m_vel = vector3d(0.0); + m_angVel = vector3d(0.0); + m_mass = 1; + m_angInertia = 1; + m_massRadius = 1; + m_enabled = true; } void DynamicBody::SetForce(const vector3d f) { - dBodySetForce(m_body, f.x, f.y, f.z); + m_force = f; } void DynamicBody::AddForce(const vector3d f) { - dBodyAddForce(m_body, f.x, f.y, f.z); + m_force += f; } void DynamicBody::AddForceAtPos(const vector3d force, const vector3d pos) { + assert(0); + /* dBodyAddForceAtPos(m_body, force.x, force.y, force.z, pos.x, pos.y, pos.z); + */ } void DynamicBody::AddRelForce(const vector3d f) { - dBodyAddRelForce(m_body, f.x, f.y, f.z); + m_force += m_orient.ApplyRotationOnly(f); } void DynamicBody::AddRelTorque(const vector3d t) { - dBodyAddRelTorque(m_body, t.x, t.y, t.z); + m_torque += m_orient.ApplyRotationOnly(t); } void DynamicBody::Save(void) { + assert(0); /* Add new dynamics stuff. */ using namespace Serializer::Write; ModelBody::Save(); wr_vector3d(GetAngVelocity()); @@ -49,48 +58,73 @@ void DynamicBody::Load(void) { } void DynamicBody::SetTorque(const vector3d t) { - dBodySetTorque(m_body, t.x, t.y, t.z); + m_torque = t; } void DynamicBody::SetMass(double mass) { - dMassAdjust(&m_mass, mass); - dBodySetMass(m_body, &m_mass); + m_mass = mass; + /* This is solid sphere mass distribution yo'! */ + m_angInertia = (2/5.0)*m_mass*m_massRadius*m_massRadius; + printf("radius %f, angInert %f\n", m_massRadius, m_angInertia); } void DynamicBody::SetPosition(vector3d p) { - dBodySetPosition(m_body, p.x, p.y, p.z); + m_orient[12] = p.x; + m_orient[13] = p.y; + m_orient[14] = p.z; ModelBody::SetPosition(p); } -void DynamicBody::TimeStepUpdate(const float timeStep) { - matrix4x4d t; - GetRotMatrix(t); - const dReal* v = dBodyGetPosition(m_body); - t[12] = v[0]; - t[13] = v[1]; - t[14] = v[2]; +vector3d DynamicBody::GetPosition(void) const { + return vector3d(m_orient[12], m_orient[13], m_orient[14]); +} - TriMeshUpdateLastPos(t); +void DynamicBody::TimeStepUpdate(const float timeStep) { + if(m_enabled) { + m_vel += timeStep * m_force * (1.0/m_mass); + m_angVel += timeStep * m_torque * (1.0 / m_angInertia); + + const vector3d pos = GetPosition(); + /* Applying angular velocity :/ */ + { + double len = m_angVel.Length(); + if(len != 0) { + vector3d rotAxis = m_angVel * (1.0 / len); + matrix4x4d rotMatrix = matrix4x4d::RotateMatrix(len * timeStep, + rotAxis.x, rotAxis.y, rotAxis.z); + m_orient = rotMatrix * m_orient; + } + } + + SetPosition(pos + m_vel * timeStep); + m_force = vector3d(0.0); + m_torque = vector3d(0.0); + + TriMeshUpdateLastPos(m_orient); + } } void DynamicBody::Enable(void) { ModelBody::Enable(); - dBodyEnable(m_body); + //dBodyEnable(m_body); } void DynamicBody::Disable(void) { ModelBody::Disable(); - dBodyDisable(m_body); + //dBodyDisable(m_body); } void DynamicBody::SetRotMatrix(const matrix4x4d& r) { - dMatrix3 _m; - r.SaveToOdeMatrix(_m); - dBodySetRotation(m_body, _m); + vector3d pos = GetPosition(); + m_orient = r; + SetPosition(pos); } void DynamicBody::GetRotMatrix(matrix4x4d& m) { - m.LoadFromOdeMatrix(dBodyGetRotation(m_body)); + m = m_orient; + m[12] = 0; + m[13] = 0; + m[14] = 0; } void DynamicBody::SetMassDistributionFromCollMesh(const CollMesh* m) { @@ -108,37 +142,32 @@ void DynamicBody::SetMassDistributionFromCollMesh(const CollMesh* m) { max.y = MAX(m->pVertex[i+1], max.y); max.z = MAX(m->pVertex[i+2], max.z); } - float size = ((max.x-min.x) + (max.y-min.y) + (max.z-min.z)) / 6.0f; - dMassSetSphere(&m_mass, 1, size); - /* Boxes go mental after a while due to inertia tensor being fishy. */ - //dMassSetBox(&m_mass, 1, max.x-min.x, max.y-min.y, max.z-min.z); - dBodySetMass(m_body, &m_mass); + const vector3d size = max-min; + m_massRadius = (size.x + size.y + size.z) * 0.333; + SetMass(m_mass); } vector3d DynamicBody::GetAngularMomentum(void) { - matrix4x4d I; - I.LoadFromOdeMatrix(m_mass.I); - return I * vector3d(dBodyGetAngularVel(m_body)); -} - -vector3d DynamicBody::GetAngVelocity(void) { - return vector3d(dBodyGetAngularVel(m_body)); + return m_angInertia * m_angVel; } DynamicBody::~DynamicBody(void) { - dBodyDestroy(m_body); + +} + +vector3d DynamicBody::GetAngVelocity(void) { + return m_angVel; } vector3d DynamicBody::GetVelocity(void) { - const dReal* v = dBodyGetLinearVel(m_body); - return vector3d(v[0], v[1], v[2]); + return m_vel; } void DynamicBody::SetVelocity(vector3d v) { - dBodySetLinearVel(m_body, v.x, v.y, v.z); + m_vel = v; } void DynamicBody::SetAngVelocity(vector3d v) { - dBodySetAngularVel(m_body, v.x, v.y, v.z); + m_angVel = v; } diff --git a/src/dynamic_body.h b/src/dynamic_body.h index d4a2285..3642280 100644 --- a/src/dynamic_body.h +++ b/src/dynamic_body.h @@ -13,6 +13,7 @@ public: virtual void GetRotMatrix(matrix4x4d& m); virtual void SetVelocity(vector3d v); virtual void SetPosition(vector3d p); + virtual vector3d GetPosition(void) const; virtual vector3d GetVelocity(void); vector3d GetAngVelocity(void); void SetAngVelocity(vector3d v); @@ -20,11 +21,11 @@ public: virtual bool OnCollision(Body* b, Uint32 flags) { return true; } vector3d GetAngularMomentum(void); void SetMassDistributionFromCollMesh(const CollMesh* m); - void DisableBodyOnly(void) { dBodyDisable(m_body); } - bool IsEnabled(void) { return dBodyIsEnabled(m_body); } + void DisableBodyOnly(void) { m_enabled = false; } + bool IsEnabled(void) { return m_enabled; } virtual void Disable(void); virtual void Enable(void); - virtual double GetMass(void) const { return m_mass.mass; } + virtual double GetMass(void) const { return m_mass; } virtual void TimeStepUpdate(const float timeStep); void SetMass(double); @@ -36,12 +37,19 @@ public: void AddRelForce(const vector3d); void AddRelTorque(const vector3d); - dBodyID m_body; - protected: virtual void Save(void); virtual void Load(void); private: - dMass m_mass; + /* New odeless stuff. */ + matrix4x4d m_orient; /* Contains pos. */ + vector3d m_force; + vector3d m_torque; + vector3d m_vel; + vector3d m_angVel; + double m_mass; + double m_massRadius; /* Set in a silly fashion from the collision mesh and used to calculate m_angInertia. */ + double m_angInertia; /* Always spehere mass distribution. */ + bool m_enabled; }; diff --git a/src/frame.cpp b/src/frame.cpp index 561dc98..04f9b5e 100644 --- a/src/frame.cpp +++ b/src/frame.cpp @@ -74,7 +74,6 @@ void Frame::Init(Frame* parent, const char* label, unsigned int flags) { m_vel = vector3d(0.0); m_angVel = vector3d(0.0); m_orient = matrix4x4d::Identity(); - m_dSpaceID = dHashSpaceCreate(0); m_collisionSpace = new CollisionSpace(); if(m_parent) { m_parent->m_children.push_back(this); @@ -83,7 +82,6 @@ void Frame::Init(Frame* parent, const char* label, unsigned int flags) { } Frame::~Frame(void) { - dSpaceDestroy(m_dSpaceID); delete m_collisionSpace; for(std::list::iterator i = m_children.begin(); i != m_children.end(); ++i) delete *i; } @@ -129,6 +127,6 @@ void Frame::RotateInTimestep(double step) { vector3d rotAxis = vector3d::Normalize(m_angVel); matrix4x4d rotMatrix = matrix4x4d::RotateMatrix(ang, rotAxis.x, rotAxis.y, rotAxis.z); - m_orient = m_orient * rotMatrix; + m_orient = rotMatrix * m_orient; } diff --git a/src/frame.h b/src/frame.h index 7020cea..827198d 100644 --- a/src/frame.h +++ b/src/frame.h @@ -32,12 +32,9 @@ public: void SetOrientation(const matrix4x4d& m) { m_orient = m; } void SetRadius(double radius) { m_radius = radius; } void RemoveChild(Frame* f); - void _AddGeom(dGeomID g) { dSpaceAdd(m_dSpaceID, g); } - void _RemoveGeom(dGeomID g) { dSpaceRemove(m_dSpaceID, g); } void AddGeom(Geom*); void RemoveGeom(Geom*); void SetPlanetGeom(double radius, Body*); - dSpaceID GetSpaceID(void) const { return m_dSpaceID; } CollisionSpace* GetCollisionSpace(void) const { return m_collisionSpace; } void RotateInTimestep(double step); @@ -69,7 +66,6 @@ private: std::string m_label; double m_radius; int m_flags; - dSpaceID m_dSpaceID; CollisionSpace* m_collisionSpace; }; diff --git a/src/libs.h b/src/libs.h index cb4708b..36dc3f0 100644 --- a/src/libs.h +++ b/src/libs.h @@ -24,11 +24,6 @@ #include "date.h" -#ifndef dDOUBLE -#error LibODE is not compiled with double-precision floating point. Please get/compile libode with \ - double-precision floating point. -#endif - #define DEBUG #define UNIVERSE_SEED 0xabcd1234 diff --git a/src/main.cpp b/src/main.cpp index 51bf19c..4e5a609 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -101,7 +101,6 @@ void L3D::Init(IniConfig& config) { InitOpenGL(); - dInitODE(); GLFTInit(); Space::Init(); diff --git a/src/model_body.cpp b/src/model_body.cpp index 5d3bc69..25a2254 100644 --- a/src/model_body.cpp +++ b/src/model_body.cpp @@ -37,9 +37,6 @@ void ModelBody::Enable(void) { m_geom->Enable(); } -void ModelBody::GeomsSetBody(dBodyID body) { -} - void ModelBody::GetAabb(Aabb& aabb) { aabb = m_collMeshSet->aabb; } diff --git a/src/model_body.h b/src/model_body.h index 6b6ed4e..a3e95fc 100644 --- a/src/model_body.h +++ b/src/model_body.h @@ -16,13 +16,11 @@ public: virtual ~ModelBody(void); void SetPosition(vector3d p); virtual void SetRotMatrix(const matrix4x4d& r); - /* Not valid to SetVelocity on these. If you want them to move, use a DynamicBody. */ vector3d GetPosition(void) const; virtual double GetRadius(void) const; void TransformToModelCoords(const Frame* camFrame); void GetRotMatrix(matrix4x4d& m) const; virtual void SetFrame(Frame* f); - void GeomsSetBody(dBodyID body); /* To remove from simulation for a period. */ virtual void Disable(void); virtual void Enable(void); diff --git a/src/model_coll_mesh_data.cpp b/src/model_coll_mesh_data.cpp index 5ee61b2..c3408e1 100644 --- a/src/model_coll_mesh_data.cpp +++ b/src/model_coll_mesh_data.cpp @@ -42,7 +42,6 @@ void CollMeshSet::GetMeshParts(void) { curFlag = triIndices[i].flags; } //printf("%d parts\n", numMeshParts); - meshParts = new dTriMeshDataID[numMeshParts]; meshInfo = new meshinfo_t[numMeshParts]; int tidx = 0; @@ -51,16 +50,10 @@ void CollMeshSet::GetMeshParts(void) { int len = 0; int flag = triIndices[tidx].flags; while((len < sbreCollMesh->ni/3) && (triIndices[tidx+len].flags == flag)) len++; - //printf("%d: len %d\n", tidx, len); - - dTriMeshDataID triMeshDataID = dGeomTriMeshDataCreate(); - dGeomTriMeshDataBuildSingle(triMeshDataID, (void*)sbreCollMesh->pVertex, - 3*sizeof(float), sbreCollMesh->nv, (void*)&triIndices[tidx], - 3*len, 4*sizeof(int)); meshInfo[midx].flags = flag; meshInfo[midx].triStart = tidx; meshInfo[midx].numTris = len; - meshParts[midx++] = triMeshDataID; + midx++; tidx += len; } while(tidx < sbreCollMesh->ni/3); } diff --git a/src/model_coll_mesh_data.h b/src/model_coll_mesh_data.h index d4b9370..1b82500 100644 --- a/src/model_coll_mesh_data.h +++ b/src/model_coll_mesh_data.h @@ -18,7 +18,6 @@ class CollMeshSet { public: CollMesh* sbreCollMesh; coltri_t* triIndices; - dTriMeshDataID* meshParts; meshinfo_t* meshInfo; int numMeshParts; Aabb aabb; diff --git a/src/planet.cpp b/src/planet.cpp index ae4d883..ee341ba 100644 --- a/src/planet.cpp +++ b/src/planet.cpp @@ -12,8 +12,6 @@ Planet::Planet(StarSystem::SBody* sbody) : Body() { } void Planet::Init(void) { - geom = dCreateSphere(0, sbody->GetRadius()); - dGeomSetData(geom, static_cast(this)); m_mass = sbody->GetMass(); crudDList = 0; } @@ -34,7 +32,6 @@ void Planet::Load(void) { } Planet::~Planet(void) { - dGeomDestroy(geom); } double Planet::GetRadius(void) const { @@ -47,12 +44,10 @@ vector3d Planet::GetPosition(void) const { void Planet::SetPosition(vector3d p) { pos = p; - dGeomSetPosition(geom, p.x, p.y, p.z); } void Planet::SetRadius(double radius) { assert(0); - //dGeomSpehereSetRadius(geom, radius); } static void subdivide(vector3d& v1, vector3d& v2, vector3d& v3, vector3d& v4, int depth) { @@ -908,12 +903,10 @@ void Planet::Render(const Frame* a_camFrame) { void Planet::SetFrame(Frame* f) { if(GetFrame()) { - GetFrame()->_RemoveGeom(geom); GetFrame()->SetPlanetGeom(0,0); } Body::SetFrame(f); if(f) { - f->_AddGeom(geom); GetFrame()->SetPlanetGeom(GetRadius(), this); } } diff --git a/src/planet.h b/src/planet.h index 1fba988..6b2ec06 100644 --- a/src/planet.h +++ b/src/planet.h @@ -30,7 +30,6 @@ private: double m_mass; vector3d pos; - dGeomID geom; StarSystem::SBody* sbody; GLuint crudDList; }; diff --git a/src/ship.cpp b/src/ship.cpp index c1198bc..e4c2819 100644 --- a/src/ship.cpp +++ b/src/ship.cpp @@ -77,7 +77,6 @@ void Ship::Init(void) { const ShipType& stype = GetShipType(); SetModel(stype.sbreModel); SetMassDistributionFromCollMesh(GetModelSBRECollMesh(stype.sbreModel)); - GeomsSetBody(m_body); UpdateMass(); } @@ -300,7 +299,7 @@ void Ship::TimeStepUpdate(const float timeStep) { /* LASERZ!! */ for(int i = 0; i < ShipType::GUNMOUNT_MAX; i++) { /* Free old temp laser geoms. */ - if(m_tempLaserGeom[i]) dGeomDestroy(m_tempLaserGeom[i]); + /*if(m_tempLaserGeom[i]) dGeomDestroy(m_tempLaserGeom[i]); m_tempLaserGeom[i] = 0; if(!m_gunState[i]) continue; dGeomID ray = dCreateRay(GetFrame()->GetSpaceID(), 10000); @@ -312,7 +311,7 @@ void Ship::TimeStepUpdate(const float timeStep) { dir = m.ApplyRotationOnly(dir); dGeomRaySet(ray, pos.x, pos.y, pos.z, dir.x, dir.y, dir.z); dGeomSetData(ray, static_cast(&m_laserCollisionObj)); - m_tempLaserGeom[i] = ray; + m_tempLaserGeom[i] = ray; */ } if(m_wheelTransition != 0.0f) { diff --git a/src/space.cpp b/src/space.cpp index e81d68d..9eebe0a 100644 --- a/src/space.cpp +++ b/src/space.cpp @@ -14,18 +14,13 @@ #include "serializer.h" #include "collider/collider.h" -dWorldID Space::world; std::list Space::bodies; Frame* Space::rootFrame; -static dJointGroupID _contactgroup; std::list Space::corpses; void Space::Init(void) { - world = dWorldCreate(); rootFrame = new Frame(NULL, "System"); rootFrame->SetRadius(FLT_MAX); - _contactgroup = dJointGroupCreate(0); - //dWorldSetGravity(world, 0, -9.81, 0); } void Space::Serialize(void) { @@ -342,6 +337,7 @@ static int contact_num; static void hitCallback(CollisionContact* c) { if(contact_num++ >= MAX_CONTACTS) return; printf("AUCH!! %x\n", SDL_GetTicks()); +#if 0 dContact contact; contact.surface.mode = dContactBounce; @@ -378,6 +374,7 @@ static void hitCallback(CollisionContact* c) { dJointID j = dJointCreateContact(Space::world, _contactgroup, &contact); dJointAttach(j, b1, b2); +#endif } #if 0 @@ -468,8 +465,6 @@ void Space::TimeStep(float step) { contact_num = 0; CollideFrame(rootFrame); - dWorldQuickStep(world, step); - dJointGroupEmpty(_contactgroup); /* TODO: Does not need to be done this often. */ UpdateFramesOfReference(); diff --git a/src/space.h b/src/space.h index 3dc0a12..b172d76 100644 --- a/src/space.h +++ b/src/space.h @@ -21,7 +21,6 @@ public: static void Render(const Frame* cam_frame); static Frame* GetRootFrame(void) { return rootFrame; } - static dWorldID world; static std::list bodies; typedef std::list::iterator bodiesIter_t; static Frame* rootFrame;