From df060177d9f3942f506df2b670b271f7daa9ce1a Mon Sep 17 00:00:00 2001 From: Allanis Date: Sun, 19 Nov 2017 22:45:44 +0000 Subject: [PATCH] [Add] ModelBody::SetRotation(). --- src/body.h | 2 +- src/dynamic_body.cpp | 6 ++++++ src/dynamic_body.h | 1 + src/matrix4x4.h | 4 ++-- src/model_body.cpp | 14 +++++++++----- src/model_body.h | 8 +++----- src/ship.cpp | 3 ++- src/space_station.cpp | 9 +++------ 8 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/body.h b/src/body.h index 64dae8f..f46bf40 100644 --- a/src/body.h +++ b/src/body.h @@ -24,7 +24,7 @@ public: void SetLabel(const char* label) { m_label = label; } std::string& GetLabel(void) { return m_label; } unsigned int GetFlags(void) { return m_flags; } - /* Return true if we should apply damage. */ + /* Return true if we should do collision response and apply damange. */ virtual bool OnCollision(Body* b, Uint32 flags) { return false; } void SetProjectedPos(const vector3d& projectedPos) { m_projectedPos = projectedPos; } /* Only valid if IsOnScreen() is true. */ diff --git a/src/dynamic_body.cpp b/src/dynamic_body.cpp index b35456d..a7c7d3f 100644 --- a/src/dynamic_body.cpp +++ b/src/dynamic_body.cpp @@ -23,6 +23,12 @@ void DynamicBody::Disable(void) { dBodyDisable(m_body); } +void DynamicBody::SetRotation(const matrix4x4d &r) { + dMatrix3 _m; + r.SaveToOdeMatrix(_m); + dBodySetRotation(m_body, _m); +} + void DynamicBody::SetMassDistributionFromCollMesh(const CollMesh* m) { vector3d min = vector3d(FLT_MAX); vector3d max = vector3d(-FLT_MAX); diff --git a/src/dynamic_body.h b/src/dynamic_body.h index aebe3b9..f210a80 100644 --- a/src/dynamic_body.h +++ b/src/dynamic_body.h @@ -10,6 +10,7 @@ class DynamicBody : public ModelBody { public: DynamicBody(void); virtual ~DynamicBody(void); + virtual void SetRotation(const matrix4x4d& r); void SetVelocity(vector3d v); void SetAngVelocity(vector3d v); void SetMesh(ObjMesh* m); diff --git a/src/matrix4x4.h b/src/matrix4x4.h index f9d8775..f9cec86 100644 --- a/src/matrix4x4.h +++ b/src/matrix4x4.h @@ -33,13 +33,13 @@ public: } /* Row-major. */ - void SaveTo3x3Matrix(T* r) { + void SaveTo3x3Matrix(T* r) const { r[0] = cell[0]; r[1] = cell[4]; r[2] = cell[8]; r[3] = cell[1]; r[4] = cell[5]; r[5] = cell[9]; r[6] = cell[2]; r[7] = cell[6]; r[8] = cell[10]; } - void SaveToOdeMatrix(T r[12]) { + void SaveToOdeMatrix(T r[12]) const { r[0] = cell[0]; r[1] = cell[4]; r[ 2] = cell[ 8]; r[ 3] = 0; r[4] = cell[1]; r[5] = cell[5]; r[ 6] = cell[ 9]; r[ 7] = 0; r[8] = cell[2]; r[9] = cell[6]; r[10] = cell[10]; r[11] = 0; diff --git a/src/model_body.cpp b/src/model_body.cpp index 1105f49..47cbcde 100644 --- a/src/model_body.cpp +++ b/src/model_body.cpp @@ -36,7 +36,7 @@ void ModelBody::GeomsSetBody(dBodyID body) { } } -void ModelBody::SetGeomFromSBREModel(int sbreModel, ObjParams* params) { +void ModelBody::SetModel(int sbreModel) { assert(geoms.size() == 0); CollMeshSet* mset = GetModelCollMeshSet(sbreModel); @@ -57,15 +57,19 @@ void ModelBody::SetPosition(vector3d p) { } } -void ModelBody::SetVelocity(vector3d v) { - assert(0); -} - vector3d ModelBody::GetPosition(void) { const dReal* pos = dGeomGetPosition(geoms[0]); return vector3d(pos[0], pos[1], pos[2]); } +void ModelBody::SetRotation(const matrix4x4d& r) { + dMatrix3 _m; + r.SaveToOdeMatrix(_m); + for(unsigned int i = 0; i < geoms.size(); i++) { + dGeomSetRotation(geoms[i], _m); + } +} + void ModelBody::GetRotMatrix(matrix4x4d& m) { m.LoadFromOdeMatrix(dGeomGetRotation(geoms[0])); } diff --git a/src/model_body.h b/src/model_body.h index 7b0dd8e..f245dec 100644 --- a/src/model_body.h +++ b/src/model_body.h @@ -12,10 +12,8 @@ public: ModelBody(void); virtual ~ModelBody(void); void SetPosition(vector3d p); - /* Not valid to do SetVelocity on these. They are for huge things like - * space stations and will be static relative to their frame of reference. - */ - void SetVelocity(vector3d v); + virtual void SetRotation(const matrix4x4d& r); + /* Not valid to SetVelocity on these. If you want them to move, use a DynamicBody. */ vector3d GetPosition(void); void TransformToModelCoords(const Frame* camFrame); void TransformCameraTo(void); @@ -28,7 +26,7 @@ public: virtual void Enable(void); void TriMeshUpdateLastPos(void); - void SetGeomFromSBREModel(int sbreModel, ObjParams* params); + void SetModel(int sbreModel); void RenderSbreModel(const Frame* camFrame, int model, ObjParams* params); diff --git a/src/ship.cpp b/src/ship.cpp index 6086806..6115de2 100644 --- a/src/ship.cpp +++ b/src/ship.cpp @@ -38,7 +38,7 @@ Ship::Ship(ShipType::Type shipType) : DynamicBody() { } memset(m_thrusters, 0, sizeof(m_thrusters)); const ShipType& stype = GetShipType(); - SetGeomFromSBREModel(stype.sbreModel, ¶ms); + SetModel(stype.sbreModel); SetMassDistributionFromCollMesh(GetModelSBRECollMesh(stype.sbreModel)); GeomsSetBody(m_body); UpdateMass(); @@ -207,6 +207,7 @@ static void render_coll_mesh(const CollMesh* m) { } void Ship::Render(const Frame* camFrame) { + if(!dBodyIsEnabled(m_body)) return; const ShipType& stype = GetShipType(); params.angthrust[0] = m_angThrusters[0]; params.angthrust[1] = m_angThrusters[1]; diff --git a/src/space_station.cpp b/src/space_station.cpp index fb1c186..1140c5f 100644 --- a/src/space_station.cpp +++ b/src/space_station.cpp @@ -21,12 +21,9 @@ static ObjParams params = { }; SpaceStation::SpaceStation(void) : ModelBody() { - SetGeomFromSBREModel(STATION_SBRE_MODEL, ¶ms); - matrix4x4d m = matrix4x4d::RotateYMatrix(-M_PI/4); - dMatrix3 _m; - m.SaveToOdeMatrix(_m); - //dGeomSetRotation(m_geom, _m); - //dGeomSetBody(m_geom, 0); + SetModel(STATION_SBRE_MODEL); + matrix4x4d m = matrix4x4d::RotateYMatrix(M_PI); + SetRotation(m); } SpaceStation::~SpaceStation(void) {