diff --git a/include/Inertia.h b/include/Inertia.h index a9c38c0be129f9874647ec456d8b40c89dab4ff6..3b4418ab59747be589a63181a82a05b04cdaf521 100644 --- a/include/Inertia.h +++ b/include/Inertia.h @@ -17,36 +17,120 @@ public: /*! Return mass and inertia matrix for a sphere of given radius and * density. */ - static void GetMassInertiaSphere(float radius, float density, - bool solid, float& mass, InertiaMatrix &inertia); + static void CalcMassPropertiesSphere( + float radius, + float density, bool solid, + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia); /*! Return mass and inertia matrix for a box of given size and * density. */ - static void GetMassInertiaBox(Vector3 size, float density, - bool solid, float& mass, InertiaMatrix &inertia); + static void CalcMassPropertiesBox( + Vector3 size, + float density, bool solid, + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia); /*! Return mass and inertia matrix for a cylinder of given radius, * height and density. */ - static void GetMassInertiaCylinder(float radius, float height, + static void CalcMassPropertiesCylinder( + Vector3 startAxis, Vector3 endAxis, float radius, float density, bool solid, - float& mass, InertiaMatrix &inertia); + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia); /*! Return mass and inertia matrix for a capsule of given radius, * height and density. */ - static void GetMassInertiaCapsule(float radius, float height, + static void CalcMassPropertiesCapsule( + Vector3 startAxis, Vector3 endAxis, float radius, float density, bool solid, - float& mass, InertiaMatrix &inertia); + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia); - /*! Return mass and inertia matrix for a capsule of given radius, - * height and density. + /*! Return mass and inertia matrix for a complex polyhedron */ - static void GetMassCenterInertiaPolyhedron( - const std::vector<Niflib::Vector3>& vertices, - const std::vector<Niflib::Triangle>& triangles, + static void CalcMassPropertiesPolyhedron( + const vector<Vector3>& vertices, + const vector<Triangle>& triangles, + float density, bool solid, + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia); + + /*! Combine mass properties for a number of objects */ + static void CombineMassProperties( + vector<float> masses, + vector<float> volumes, + vector<Vector3> centers, + vector<InertiaMatrix> inertias, + vector<Matrix44> transforms, + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia); + +public: + + // Define external strategy helpers + + /*! Return mass and inertia matrix for a sphere of given radius and + * density. + */ + typedef void (__stdcall * fnCalcMassPropertiesSphere)( + float radius, + float density, bool solid, + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia); + + /*! Return mass and inertia matrix for a box of given size and + * density. + */ + typedef void (__stdcall * fnCalcMassPropertiesBox)( + Vector3 size, + float density, bool solid, + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia); + + /*! Return mass and inertia matrix for a cylinder of given radius, + * height and density. + */ + typedef void (__stdcall * fnCalcMassPropertiesCylinder)( + Vector3 startAxis, Vector3 endAxis, float radius, float density, bool solid, - float& mass, Niflib::Vector3& center, InertiaMatrix &inertia); + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia); + + /*! Return mass and inertia matrix for a capsule of given radius, + * height and density. + */ + typedef void (__stdcall * fnCalcMassPropertiesCapsule)( + Vector3 startAxis, Vector3 endAxis, float radius, + float density, bool solid, + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia); + + /*! Return mass and inertia matrix for a capsule of given radius, + * height and density. + */ + typedef void (__stdcall * fnCalcMassPropertiesPolyhedron)( + int nVerts, Vector3 const* verts, + int nTris, Triangle const* tris, + float density, bool solid, + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia); + + /*! Combine mass properties for a number of objects */ + typedef void (__stdcall * fnCombineMassProperties)( + int nItems, + float* masses, float* volumes, Vector3* centers, InertiaMatrix* inertias, Matrix44* transforms, + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia); + + /*! Define external MassProperty calculation method for Sphere */ + static void SetCalcMassPropertiesSphere(fnCalcMassPropertiesSphere extRoutine); + + /*! Define external MassProperty calculation method for Box */ + static void SetCalcMassPropertiesBox(fnCalcMassPropertiesBox extRoutine); + + /*! Define external MassProperty calculation method for Cylinder */ + static void SetCalcMassPropertiesCylinder(fnCalcMassPropertiesCylinder extRoutine); + + /*! Define external MassProperty calculation method for Capsule */ + static void SetCalcMassPropertiesCapsule(fnCalcMassPropertiesCapsule extRoutine); + + /*! Define external MassProperty calculation method for Polyhedron */ + static void SetCalcMassPropertiesPolyhedron(fnCalcMassPropertiesPolyhedron extRoutine); + + /*! Define external mass property combination routine */ + static void SetCombineMassProperties(fnCombineMassProperties extRoutine); + private: explicit Inertia(); diff --git a/include/nifqhull.h b/include/nifqhull.h new file mode 100644 index 0000000000000000000000000000000000000000..b46e91401714985a8511874205669bce874b77d6 --- /dev/null +++ b/include/nifqhull.h @@ -0,0 +1,24 @@ +/* Copyright (c) 2006, NIF File Format Library and Tools +All rights reserved. Please see niflib.h for license. */ + +#ifndef _NIF_QHULL_H +#define _NIF_QHULL_H +#pragma once + +//--Includes--// +#include "nif_math.h" +#include <vector> + +namespace Niflib { + +class NifQHull { +public: + + static vector<Triangle> compute_convex_hull(const vector<Vector3>& verts); + +private: + explicit NifQHull(); +}; + +} +#endif \ No newline at end of file diff --git a/include/obj/bhkBoxShape.h b/include/obj/bhkBoxShape.h index c17851aa8d67f0e9689b653c9687bd2bed311907..fabffefeafcb898196bb433404ae6cfaf5fca6b5 100644 --- a/include/obj/bhkBoxShape.h +++ b/include/obj/bhkBoxShape.h @@ -74,7 +74,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// protected: diff --git a/include/obj/bhkBvTreeShape.h b/include/obj/bhkBvTreeShape.h index 03d8c929d3d4238ae80c18f0e24cfa6c3eac1b7e..07ef177ffcdadeaf2ef15531cbe62ebb568f70a1 100644 --- a/include/obj/bhkBvTreeShape.h +++ b/include/obj/bhkBvTreeShape.h @@ -61,7 +61,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--BEGIN MISC CUSTOM CODE--// //--END CUSTOM CODE--// diff --git a/include/obj/bhkCapsuleShape.h b/include/obj/bhkCapsuleShape.h index 6428b717d86316a17cc074c53dd98f8efde22bd2..94cfa5781f87c474db357317a29ca7e0f2b3688b 100644 --- a/include/obj/bhkCapsuleShape.h +++ b/include/obj/bhkCapsuleShape.h @@ -122,7 +122,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// protected: diff --git a/include/obj/bhkConvexShape.h b/include/obj/bhkConvexShape.h index aec3cdb557ba8d93f9b86c1cb96ba54fe48acc21..b33d90741f830daad8ecebb3f903f6e1d146a7e1 100644 --- a/include/obj/bhkConvexShape.h +++ b/include/obj/bhkConvexShape.h @@ -62,7 +62,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// public: diff --git a/include/obj/bhkConvexTransformShape.h b/include/obj/bhkConvexTransformShape.h index 9b8864bcfe9f57550f3b24064e1dd43a54f422f1..743fdba2e6c5dee4ed4f2d04cab6f01b1545563b 100644 --- a/include/obj/bhkConvexTransformShape.h +++ b/include/obj/bhkConvexTransformShape.h @@ -61,7 +61,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// public: diff --git a/include/obj/bhkConvexVerticesShape.h b/include/obj/bhkConvexVerticesShape.h index 3155f085891eeadf991b9c5b13b318fb0538dd2a..496f7f7514fdb307f36ac94c8ca58fd510849f31 100644 --- a/include/obj/bhkConvexVerticesShape.h +++ b/include/obj/bhkConvexVerticesShape.h @@ -119,7 +119,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// protected: diff --git a/include/obj/bhkListShape.h b/include/obj/bhkListShape.h index 7d362570e837d263c26c34da478fba87762112cc..c3d3d099685d329d9fdd7ea6552271506a6844fc 100644 --- a/include/obj/bhkListShape.h +++ b/include/obj/bhkListShape.h @@ -99,7 +99,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// protected: diff --git a/include/obj/bhkMoppBvTreeShape.h b/include/obj/bhkMoppBvTreeShape.h index 8ef6c4f755e3ea3600e8054816276d1fbb777c89..3875391da1c336cfaf3dc4d0cbc4cde66771818c 100644 --- a/include/obj/bhkMoppBvTreeShape.h +++ b/include/obj/bhkMoppBvTreeShape.h @@ -138,7 +138,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// protected: diff --git a/include/obj/bhkMultiSphereShape.h b/include/obj/bhkMultiSphereShape.h index abdf67585fc9b98f8a1ac7bff8a88c845d4dbb97..b570b0e832ed1311c69ec0ce1ce2d81ad6b1105a 100644 --- a/include/obj/bhkMultiSphereShape.h +++ b/include/obj/bhkMultiSphereShape.h @@ -78,7 +78,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// protected: diff --git a/include/obj/bhkNiTriStripsShape.h b/include/obj/bhkNiTriStripsShape.h index f1a5f0b7c1158f96aea6ce9104e2124c6ddfb0ff..a32262d09744482a8653fe084047fbe357ded4a0 100644 --- a/include/obj/bhkNiTriStripsShape.h +++ b/include/obj/bhkNiTriStripsShape.h @@ -139,7 +139,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// protected: diff --git a/include/obj/bhkPackedNiTriStripsShape.h b/include/obj/bhkPackedNiTriStripsShape.h index b9326232e18f3a9a381926700c6a7a2cc6307b67..8b51d95a55b735c6f31d0fede02243d5060ec0d4 100644 --- a/include/obj/bhkPackedNiTriStripsShape.h +++ b/include/obj/bhkPackedNiTriStripsShape.h @@ -91,7 +91,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// protected: diff --git a/include/obj/bhkRigidBody.h b/include/obj/bhkRigidBody.h index b71f1f0ff83add376e884c8adfd1477db11221a4..4e9088710c108b6f133389ab49dfb483ce1c0ccb 100644 --- a/include/obj/bhkRigidBody.h +++ b/include/obj/bhkRigidBody.h @@ -297,7 +297,7 @@ public: // Look at all the objects under this rigid body and update the mass // center of gravity, and inertia tensor accordingly. If the mass parameter // is given then the density argument is ignored. - NIFLIB_API void UpdateMassCenterInertia(float density = 1.0f, bool solid = true, float mass = 0.0f); + NIFLIB_API void UpdateMassProperties(float density = 1.0f, bool solid = true, float mass = 0.0f); //--END CUSTOM CODE--// protected: diff --git a/include/obj/bhkShape.h b/include/obj/bhkShape.h index 6b858a03977cd502c54425a15388256cbb8343c5..ca90737a0b51295958f33fa60a5b76fa51513e1e 100644 --- a/include/obj/bhkShape.h +++ b/include/obj/bhkShape.h @@ -63,7 +63,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// public: diff --git a/include/obj/bhkSphereRepShape.h b/include/obj/bhkSphereRepShape.h index fd08bad8992eeb9788d0f57465149ce5a7f26b1d..89eec01494a20f30b40d642f27deb5ba284162f0 100644 --- a/include/obj/bhkSphereRepShape.h +++ b/include/obj/bhkSphereRepShape.h @@ -89,7 +89,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// protected: diff --git a/include/obj/bhkSphereShape.h b/include/obj/bhkSphereShape.h index 1145b7eea82f1c561178a02e7d98d2e99772ddff..457fbdc550a87d21913f38b1de74f566c4077c2d 100644 --- a/include/obj/bhkSphereShape.h +++ b/include/obj/bhkSphereShape.h @@ -74,7 +74,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// public: diff --git a/include/obj/bhkTransformShape.h b/include/obj/bhkTransformShape.h index 34d7470d6a6ae736a35e31d1e3ee76640c845410..743955984b671d31fac7413db07a77ed5bdf60c4 100644 --- a/include/obj/bhkTransformShape.h +++ b/include/obj/bhkTransformShape.h @@ -102,7 +102,7 @@ public: * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ - NIFLIB_API virtual void CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia); + NIFLIB_API virtual void CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia); //--END CUSTOM CODE--// diff --git a/src/Inertia.cpp b/src/Inertia.cpp index 1b103488effec7447f402cc394d6594da6a21fba..ae8c8fd4186eb07de50ee8e5152e5394537ffe45 100644 --- a/src/Inertia.cpp +++ b/src/Inertia.cpp @@ -2,6 +2,7 @@ All rights reserved. Please see niflib.h for license. */ #include "../include/Inertia.h" +#include "../include/nifqhull.h" #include <vector> #include <algorithm> #include <functional> @@ -10,9 +11,48 @@ All rights reserved. Please see niflib.h for license. */ #include <math.h> using namespace Niflib; -void Inertia::GetMassInertiaSphere(float radius, float density, bool solid, - float& mass, InertiaMatrix &inertia) +static Inertia::fnCalcMassPropertiesSphere extCalcMassPropertiesSphereRoutine = NULL; +static Inertia::fnCalcMassPropertiesBox extCalcMassPropertiesBoxRoutine = NULL; +static Inertia::fnCalcMassPropertiesCylinder extCalcMassPropertiesCylinderRoutine = NULL; +static Inertia::fnCalcMassPropertiesCapsule extCalcMassPropertiesCapsuleRoutine = NULL; +static Inertia::fnCalcMassPropertiesPolyhedron extCalcMassPropertiesPolyhedronRoutine = NULL; +static Inertia::fnCombineMassProperties extCombineMassPropertiesRoutine = NULL; + +void Inertia::SetCalcMassPropertiesSphere(Inertia::fnCalcMassPropertiesSphere extRoutine) { + extCalcMassPropertiesSphereRoutine = extRoutine; +} + +void Inertia::SetCalcMassPropertiesBox(Inertia::fnCalcMassPropertiesBox extRoutine) { + extCalcMassPropertiesBoxRoutine = extRoutine; +} + +void Inertia::SetCalcMassPropertiesCylinder(Inertia::fnCalcMassPropertiesCylinder extRoutine) { + extCalcMassPropertiesCylinderRoutine = extRoutine; +} + +void Inertia::SetCalcMassPropertiesCapsule(Inertia::fnCalcMassPropertiesCapsule extRoutine) { + extCalcMassPropertiesCapsuleRoutine = extRoutine; +} + +void Inertia::SetCalcMassPropertiesPolyhedron(Inertia::fnCalcMassPropertiesPolyhedron extRoutine) { + extCalcMassPropertiesPolyhedronRoutine = extRoutine; +} + +/*! Define external mass property combination routine */ +void Inertia::SetCombineMassProperties(Inertia::fnCombineMassProperties extRoutine) { + extCombineMassPropertiesRoutine = extRoutine; +} + + + +void Inertia::CalcMassPropertiesSphere(float radius, + float density, bool solid, + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia) { + if (extCalcMassPropertiesSphereRoutine) { + extCalcMassPropertiesSphereRoutine(radius, density, solid, mass, volume, center, inertia); + return; + } float inertiaScalar; if (solid) { mass = density * (4.0f * float(M_PI) * pow(radius, 3.0f)) / 3.0f; @@ -21,15 +61,22 @@ void Inertia::GetMassInertiaSphere(float radius, float density, bool solid, mass = density * 4.0f * float(M_PI) * pow(radius, 2.0f); inertiaScalar = (2.0f * mass * pow(radius, 2.0f)) / 3.0f; } + center = Vector3(); inertia = InertiaMatrix::IDENTITY; inertia[0][0] = inertia[1][1] = inertia[2][2] = inertiaScalar; } -void Inertia::GetMassInertiaBox(Vector3 size, float density, bool solid, - float& mass, InertiaMatrix &inertia) +void Inertia::CalcMassPropertiesBox(Vector3 size, + float density, bool solid, + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia) { + if (extCalcMassPropertiesBoxRoutine) { + extCalcMassPropertiesBoxRoutine(size, density, solid, mass, volume, center, inertia); + return; + } + Vector3 tmp; if (solid) { @@ -50,12 +97,20 @@ void Inertia::GetMassInertiaBox(Vector3 size, float density, bool solid, inertia[0][0] = tmp[1] + tmp[2]; inertia[1][1] = tmp[2] + tmp[0]; inertia[2][2] = tmp[0] + tmp[1]; + center = Vector3(); } -void Inertia::GetMassInertiaCylinder(float radius, float height, - float density, bool solid, - float& mass, InertiaMatrix &inertia) +void Inertia::CalcMassPropertiesCylinder(Vector3 startAxis, Vector3 endAxis, float radius, + float density, bool solid, + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia) { + if (extCalcMassPropertiesCylinderRoutine) { + extCalcMassPropertiesCylinderRoutine(startAxis, endAxis, radius, density, solid, mass, volume, center, inertia); + return; + } + float height = (startAxis - endAxis).Magnitude(); + center = (startAxis + endAxis) / 2.0f; + inertia = InertiaMatrix::IDENTITY; if (solid) { @@ -73,10 +128,18 @@ void Inertia::GetMassInertiaCylinder(float radius, float height, } } -void Inertia::GetMassInertiaCapsule(float radius, float height, +void Inertia::CalcMassPropertiesCapsule(Vector3 startAxis, Vector3 endAxis, float radius, float density, bool solid, - float& mass, InertiaMatrix &inertia) + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia) { + if (extCalcMassPropertiesCapsuleRoutine) { + extCalcMassPropertiesCapsuleRoutine(startAxis, endAxis, radius, density, solid, mass, volume, center, inertia); + return; + } + + float height = (startAxis - endAxis).Magnitude(); + center = (startAxis + endAxis) / 2.0f; + // cylinder + caps, and caps have volume of a sphere inertia = InertiaMatrix::IDENTITY; if (solid) @@ -110,11 +173,33 @@ void Inertia::GetMassInertiaCapsule(float radius, float height, // The function is an implementation of the Blow and Binstock algorithm, // extended for the case where the polygon is a surface (set parameter // solid = False). -void Inertia::GetMassCenterInertiaPolyhedron(const vector<Vector3>& vertices, +void Inertia::CalcMassPropertiesPolyhedron(const vector<Vector3>& vertices, const vector<Triangle>& triangles, float density, bool solid, - float& mass, Vector3& center, InertiaMatrix &inertia) + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia) { + if (extCalcMassPropertiesPolyhedronRoutine) { + extCalcMassPropertiesPolyhedronRoutine( + vertices.size(), &vertices[0], + triangles.size(), triangles.empty() ? NULL : &triangles[0], + density, solid, mass, volume, center, inertia); + return; + } + + vector<Triangle>::const_iterator triBegin, triEnd; + vector<Triangle> tris; + if (triangles.size() == 0) + { + tris = NifQHull::compute_convex_hull(vertices); + triBegin = tris.begin(); + triEnd = tris.end(); + } + else + { + triBegin = triangles.begin(); + triEnd = triangles.end(); + } + // 120 times the covariance matrix of the canonical tetrahedron // (0,0,0),(1,0,0),(0,1,0),(0,0,1) // integrate(integrate(integrate(z*z, x=0..1-y-z), y=0..1-z), z=0..1) = 1/120 @@ -132,9 +217,9 @@ void Inertia::GetMassCenterInertiaPolyhedron(const vector<Vector3>& vertices, // construct a tetrahedron from triangle + (0,0,0) // find its matrix, mass, and center (for density = 1, will be corrected at // the end of the algorithm) - for (vector<Triangle>::const_iterator itr=triangles.begin(), end=triangles.end(); itr != end; ++itr) + for (vector<Triangle>::const_iterator itr=triBegin, end=triEnd; itr != end; ++itr) { - // get vertices + // Calc vertices const Vector3& vert0 = vertices[ (*itr)[0] ]; const Vector3& vert1 = vertices[ (*itr)[1] ]; const Vector3& vert2 = vertices[ (*itr)[2] ]; @@ -193,7 +278,7 @@ void Inertia::GetMassCenterInertiaPolyhedron(const vector<Vector3>& vertices, // dimension is probably badly chosen //raise ZeroDivisionError("mass is zero (consider calculating inertia with a lower dimension)") //printf("WARNING: mass is zero"); - mass = 0.0f; + mass = 0.0f, volume = 0.0f; center = Vector3(); inertia = InertiaMatrix::IDENTITY; return; @@ -235,4 +320,31 @@ void Inertia::GetMassCenterInertiaPolyhedron(const vector<Vector3>& vertices, // correct for given density inertia = InertiaMatrix((trace_matrix - total_covariance).GetRotation()) * density; mass *= density; +} + +void Inertia::CombineMassProperties( + vector<float> masses, + vector<float> volumes, + vector<Vector3> centers, + vector<InertiaMatrix> inertias, + vector<Matrix44> transforms, + float& mass, float& volume, Vector3& center, InertiaMatrix &inertia ) +{ + if (extCombineMassPropertiesRoutine) { + extCombineMassPropertiesRoutine( + masses.size(), + &masses[0], &volumes[0], ¢ers[0], &inertias[0], &transforms[0], + mass, volume, center, inertia); + return; + } + + for (size_t i=0; i < masses.size(); ++i) { + mass += masses[i]; + } + // TODO: doubt this inertia calculation is even remotely close + for (size_t i=0; i < masses.size(); ++i) { + center += centers[i] * (masses[i] / mass); + inertia *= inertias[i]; + } + } \ No newline at end of file diff --git a/src/nifqhull.cpp b/src/nifqhull.cpp new file mode 100644 index 0000000000000000000000000000000000000000..453f9d7aa85b15d5f2d7c75858f26c32771b85f6 --- /dev/null +++ b/src/nifqhull.cpp @@ -0,0 +1,91 @@ +/* Copyright (c) 2006, NIF File Format Library and Tools +All rights reserved. Please see niflib.h for license. */ + +#include "../include/nifqhull.h" +#include <vector> +#include <algorithm> +#include <functional> +#include <math.h> +using namespace Niflib; + +extern "C" +{ +#include "../qhull/qhull.h" +#include "../qhull/mem.h" +#include "../qhull/qset.h" +#include "../qhull/geom.h" +#include "../qhull/merge.h" +#include "../qhull/poly.h" +#include "../qhull/io.h" +#include "../qhull/stat.h" + +#include "../qhull/qhull.c" +#include "../qhull/mem.c" +#include "../qhull/qset.c" +#include "../qhull/geom.c" +#include "../qhull/merge.c" +#include "../qhull/poly.c" +#include "../qhull/io.c" +#include "../qhull/stat.c" +#include "../qhull/global.c" +#include "../qhull/user.c" +#include "../qhull/poly2.c" +#include "../qhull/geom2.c" +}; + +using namespace Niflib; + +vector<Triangle> NifQHull::compute_convex_hull(const vector<Vector3>& verts) +{ + vector<Triangle> tris; + + int dim=3; /* dimension of points */ + int numpoints=0; /* number of points */ + coordT *points=0; /* array of coordinates for each point */ + boolT ismalloc=0; /* True if qhull should free points in qh_freeqhull() or reallocation */ + char flags[]= "qhull i Qt"; /* option flags for qhull, see qh_opt.htm */ + FILE *outfile= stdout; /* output from qh_produce_output() + use NULL to skip qh_produce_output() */ + FILE *errfile= stderr; /* error messages from qhull code */ + int exitcode; /* 0 if no error from qhull */ + facetT *facet; /* set by FORALLfacets */ + int curlong, totlong; /* memory remaining after qh_memfreeshort */ + + vertexT *vertex, **vertexp; + setT *vertices; + + numpoints = verts.size(); + points = new coordT[3 * verts.size()]; + for (int i=0, n=verts.size(); i<n; ++i) { + points[i*3 + 0] = verts[i].x; + points[i*3 + 1] = verts[i].y; + points[i*3 + 2] = verts[i].z; + } + + /* initialize dim, numpoints, points[], ismalloc here */ + exitcode= qh_new_qhull (dim, numpoints, points, ismalloc, + flags, outfile, errfile); + if (!exitcode) { /* if no error */ + /* 'qh facet_list' contains the convex hull */ + FORALLfacets { + vertices = qh_facet3vertex (facet); + if (qh_setsize (vertices) == 3) { + Triangle tri; + int i = 0; + FOREACHvertex_(vertices) { + tri[i++] = qh_pointid(vertex->point); + } + tris.push_back(tri); + } + qh_settempfree(&vertices); + } + } + qh_freeqhull(!qh_ALL); + qh_memfreeshort (&curlong, &totlong); + if (curlong || totlong) + fprintf (errfile, "qhull internal warning (main): did not free %d bytes of long memory (%d pieces)\n", + totlong, curlong); + + delete[] points; + return tris; +}; diff --git a/src/obj/bhkBoxShape.cpp b/src/obj/bhkBoxShape.cpp index 86eb94a23838c9decefb21669e69e4ab759785a5..3bcabeb48f86a45dfdfa4771c436ebed6366eaae 100644 --- a/src/obj/bhkBoxShape.cpp +++ b/src/obj/bhkBoxShape.cpp @@ -122,9 +122,8 @@ void bhkBoxShape::SetDimensions(const Vector3 &value) { minimumSize = min( min(value.x, value.y), value.z ); } -void bhkBoxShape::CalcMassCenterInertia( float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia ) +void bhkBoxShape::CalcMassProperties( float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia ) { - center = Vector3(0,0,0); - Inertia::GetMassInertiaBox( dimensions * 2.0f, density, solid, mass, inertia ); + Inertia::CalcMassPropertiesBox( dimensions, density, solid, mass, volume, center, inertia ); } //--END CUSTOM CODE--// diff --git a/src/obj/bhkBvTreeShape.cpp b/src/obj/bhkBvTreeShape.cpp index d115e16024d2607419c4a0c478906a1184d2a704..3ee8603cb1d94f11ad98a8cd0f243e85cf850070 100644 --- a/src/obj/bhkBvTreeShape.cpp +++ b/src/obj/bhkBvTreeShape.cpp @@ -86,10 +86,10 @@ std::list<NiObjectRef> bhkBvTreeShape::GetRefs() const { return refs; } -void bhkBvTreeShape::CalcMassCenterInertia( float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia ) +void bhkBvTreeShape::CalcMassProperties( float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia ) { center = Vector3(0,0,0); - mass = 0.0f; + mass = 0.0f, volume = 0.0f; inertia = InertiaMatrix::IDENTITY; } diff --git a/src/obj/bhkCapsuleShape.cpp b/src/obj/bhkCapsuleShape.cpp index 66d6face748a677825591b39fb33e794a6a273f2..96936240af64dde5f7d5f6e45bdd374190549d87 100644 --- a/src/obj/bhkCapsuleShape.cpp +++ b/src/obj/bhkCapsuleShape.cpp @@ -167,11 +167,9 @@ void bhkCapsuleShape::SetRadius2( float value ) { * \param[out] inertia Mass Inertia Tensor * \return Return mass, center, and inertia tensor. */ -void bhkCapsuleShape::CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia) +void bhkCapsuleShape::CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia) { - center = Vector3(); - float height = (secondPoint - firstPoint).Magnitude(); - Inertia::GetMassInertiaCapsule(radius, height, density, solid, mass, inertia); + Inertia::CalcMassPropertiesCapsule(firstPoint, secondPoint, radius, density, solid, mass, volume, center, inertia); } //--END CUSTOM CODE--// diff --git a/src/obj/bhkConvexShape.cpp b/src/obj/bhkConvexShape.cpp index b762579b2f77fa355edf9d354975d12accfc9f84..301472a5958fa4e532b7c316742ec52d200a8d76 100644 --- a/src/obj/bhkConvexShape.cpp +++ b/src/obj/bhkConvexShape.cpp @@ -88,10 +88,10 @@ std::list<NiObjectRef> bhkConvexShape::GetRefs() const { //--BEGIN MISC CUSTOM CODE--// -void bhkConvexShape::CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia) +void bhkConvexShape::CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia) { center = Vector3(0,0,0); - mass = 0.0f; + mass = 0.0f, volume = 0.0f; inertia = InertiaMatrix::IDENTITY; } diff --git a/src/obj/bhkConvexTransformShape.cpp b/src/obj/bhkConvexTransformShape.cpp index 2cfb3476f51f776f09384877ecad23d535d4032d..8ff666585db55427eaef9be2433f249346acc468 100644 --- a/src/obj/bhkConvexTransformShape.cpp +++ b/src/obj/bhkConvexTransformShape.cpp @@ -87,10 +87,10 @@ std::list<NiObjectRef> bhkConvexTransformShape::GetRefs() const { } //--BEGIN MISC CUSTOM CODE--// -void bhkConvexTransformShape::CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia) +void bhkConvexTransformShape::CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia) { center = Vector3(0,0,0); - mass = 0.0f; + mass = 0.0f, volume = 0.0f; inertia = InertiaMatrix::IDENTITY; } diff --git a/src/obj/bhkConvexVerticesShape.cpp b/src/obj/bhkConvexVerticesShape.cpp index d1dbcd586d29bb91de4b9654a4ae489f6652acd1..1179a1f1e71cd8d0fa1b54ec4ff422760b5e2909 100644 --- a/src/obj/bhkConvexVerticesShape.cpp +++ b/src/obj/bhkConvexVerticesShape.cpp @@ -8,7 +8,6 @@ All rights reserved. Please see niflib.h for license. */ //-----------------------------------NOTICE----------------------------------// //--BEGIN FILE HEAD CUSTOM CODE--// -#include "../../include/nifqhull.h" #include "../../include/Inertia.h" //--END CUSTOM CODE--// @@ -245,13 +244,13 @@ void bhkConvexVerticesShape::SetNormalsAndDist(const vector<Vector4>& value) normals = value; } -void bhkConvexVerticesShape::CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia) +void bhkConvexVerticesShape::CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia) { center = Vector3(0,0,0); - mass = 0.0f; + mass = 0.0f, volume = 0.0f; inertia = InertiaMatrix::IDENTITY; vector<Vector3> verts = GetVertices(); - vector<Triangle> tris = NifQHull::compute_convex_hull(verts); - Inertia::GetMassCenterInertiaPolyhedron(verts, tris, density, solid, mass, center, inertia); + vector<Triangle> tris; // no tris mean convex + Inertia::CalcMassPropertiesPolyhedron(verts, tris, density, solid, mass, volume, center, inertia); } //--END CUSTOM CODE--// diff --git a/src/obj/bhkListShape.cpp b/src/obj/bhkListShape.cpp index b01268fc098c747f4f0b1a39b98e0a37ba7e8a0f..d6ae5144db661ece839e03270b82c11b9d69b6b4 100644 --- a/src/obj/bhkListShape.cpp +++ b/src/obj/bhkListShape.cpp @@ -8,6 +8,7 @@ All rights reserved. Please see niflib.h for license. */ //-----------------------------------NOTICE----------------------------------// //--BEGIN FILE HEAD CUSTOM CODE--// +#include "../../include/Inertia.h" //--END CUSTOM CODE--// #include "../../include/FixLink.h" @@ -198,29 +199,31 @@ void bhkListShape::SetSubShapes(const vector<Ref<bhkShape > >& shapes) { unknownInts.resize(subShapes.size(), 0); } -void bhkListShape::CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia) +void bhkListShape::CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia) { center = Vector3(0,0,0); mass = 0.0f; + volume = 0.0f; inertia = InertiaMatrix::IDENTITY; vector<float> masses; + vector<float> volumes; vector<Vector3> centers; vector<InertiaMatrix> inertias; + vector<Matrix44> transforms; for (vector<bhkShapeRef>::iterator itr = subShapes.begin(); itr != subShapes.end(); ++itr) { - float m; Vector3 c; InertiaMatrix i; - (*itr)->CalcMassCenterInertia(density, solid, m, c, i); + float m; float v; Vector3 c; InertiaMatrix i; + (*itr)->CalcMassProperties(density, solid, m, v, c, i); masses.push_back(m); + volumes.push_back(v); centers.push_back(c); inertias.push_back(i); - mass += m; - } - - // TODO: doubt this inertia calculation is even remotely close - for (size_t i=0; i < masses.size(); ++i) { - center += centers[i] * (masses[i] / mass); - inertia *= inertias[i]; + transforms.push_back(Matrix44::IDENTITY); } + Inertia::CombineMassProperties( + masses, volumes, centers, inertias, transforms, + mass, volume, center, inertia + ); } //--END CUSTOM CODE--// diff --git a/src/obj/bhkMoppBvTreeShape.cpp b/src/obj/bhkMoppBvTreeShape.cpp index 189b17c7507b2898ecb205f7dd5f3932033f24ea..b96d2a1e561f427f3249bc48b270d0203b4f5523 100644 --- a/src/obj/bhkMoppBvTreeShape.cpp +++ b/src/obj/bhkMoppBvTreeShape.cpp @@ -200,14 +200,14 @@ void bhkMoppBvTreeShape::SetMoppScale( float value ) { scale = value; } -void bhkMoppBvTreeShape::CalcMassCenterInertia( float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia ) +void bhkMoppBvTreeShape::CalcMassProperties( float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia ) { center = Vector3(0,0,0); - mass = 0.0f; + mass = 0.0f, volume = 0.0f; inertia = InertiaMatrix::IDENTITY; if (shape != NULL) - shape->CalcMassCenterInertia(density, solid, mass, center, inertia); + shape->CalcMassProperties(density, solid, mass, volume, center, inertia); } //--END CUSTOM CODE--// diff --git a/src/obj/bhkMultiSphereShape.cpp b/src/obj/bhkMultiSphereShape.cpp index da68b33f7af973185cbb83b518f2664f375a279e..cf4081d5d791fef6489aa430125849e1c119d01a 100644 --- a/src/obj/bhkMultiSphereShape.cpp +++ b/src/obj/bhkMultiSphereShape.cpp @@ -126,11 +126,11 @@ void bhkMultiSphereShape::SetSpheres( const vector<SphereBV >& value ) { spheres = value; } -void bhkMultiSphereShape::CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia) +void bhkMultiSphereShape::CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia) { // TODO: Calculate this properly center = Vector3(0,0,0); - mass = 0.0f; + mass = 0.0f, volume = 0.0f; inertia = InertiaMatrix::IDENTITY; } //--END CUSTOM CODE--// diff --git a/src/obj/bhkNiTriStripsShape.cpp b/src/obj/bhkNiTriStripsShape.cpp index 6e0aa018ca9f6dde00c501db5b0e391f460ded16..d861c04c9092d36618cc22cf42d2f3bb8eceee83 100644 --- a/src/obj/bhkNiTriStripsShape.cpp +++ b/src/obj/bhkNiTriStripsShape.cpp @@ -258,10 +258,10 @@ void bhkNiTriStripsShape::SetOblivionFilter( unsigned int index, unsigned char f dataLayers[index].colFilter = filter; } -void bhkNiTriStripsShape::CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia) +void bhkNiTriStripsShape::CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia) { center = Vector3(0,0,0); - mass = 0.0f; + mass = 0.0f, volume = 0.0f; inertia = InertiaMatrix::IDENTITY; vector<Vector3> verts; @@ -276,6 +276,6 @@ void bhkNiTriStripsShape::CalcMassCenterInertia(float density, bool solid, float for (size_t i=0; i<nt; ++i) tris.push_back( Triangle(t[i][0] + nt, t[i][1] + nt, t[i][2] + nt) ); } - Inertia::GetMassCenterInertiaPolyhedron(verts, tris, density, solid, mass, center, inertia); + Inertia::CalcMassPropertiesPolyhedron(verts, tris, density, solid, mass, volume, center, inertia); } //--END CUSTOM CODE--// diff --git a/src/obj/bhkPackedNiTriStripsShape.cpp b/src/obj/bhkPackedNiTriStripsShape.cpp index a6791005baa5268f09ec730e49568684fa33884c..33c919d410847756eb454b56ba22014dd3260204 100644 --- a/src/obj/bhkPackedNiTriStripsShape.cpp +++ b/src/obj/bhkPackedNiTriStripsShape.cpp @@ -182,17 +182,17 @@ void bhkPackedNiTriStripsShape::SetSubShapes( vector<OblivionSubShape>& value ) subShapes = value; } -void bhkPackedNiTriStripsShape::CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia) +void bhkPackedNiTriStripsShape::CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia) { center = Vector3(0,0,0); - mass = 0.0f; + mass = 0.0f, volume = 0.0f; inertia = InertiaMatrix::IDENTITY; if (data != NULL) { vector<Vector3> verts = data->GetVertices(); vector<Triangle> tris = data->GetTriangles(); - Inertia::GetMassCenterInertiaPolyhedron(verts, tris, density, solid, mass, center, inertia); + Inertia::CalcMassPropertiesPolyhedron(verts, tris, density, solid, mass, volume, center, inertia); } } //--END CUSTOM CODE--// diff --git a/src/obj/bhkRigidBody.cpp b/src/obj/bhkRigidBody.cpp index f89c8abbe3cbb98e954eaee0fb7216ce03ad3f3d..a02f167e19cd94e00477d63a16169a85e1ef0b18 100644 --- a/src/obj/bhkRigidBody.cpp +++ b/src/obj/bhkRigidBody.cpp @@ -452,7 +452,7 @@ void bhkRigidBody::ApplyScale(float scale) //ApplyScale(scale) } -void bhkRigidBody::UpdateMassCenterInertia(float density, bool solid, float mass) +void bhkRigidBody::UpdateMassProperties(float density, bool solid, float mass) { // Look at all the objects under this rigid body and update the mass // center of gravity, and inertia tensor accordingly. If the C{mass} parameter @@ -463,8 +463,9 @@ void bhkRigidBody::UpdateMassCenterInertia(float density, bool solid, float mass if (shape != NULL) { + float volume; Vector3 com; - shape->CalcMassCenterInertia(density, solid, this->mass, com, inertia); + shape->CalcMassProperties(density, solid, this->mass, volume, com, inertia); center = com; if (mass != 0.0f) { diff --git a/src/obj/bhkShape.cpp b/src/obj/bhkShape.cpp index fdfac180bb23ee7aa1f3d4f8848fb9700065e736..24b524a4bbd4678b3dc456342f768b2e318c23fd 100644 --- a/src/obj/bhkShape.cpp +++ b/src/obj/bhkShape.cpp @@ -88,10 +88,10 @@ std::list<NiObjectRef> bhkShape::GetRefs() const { //--BEGIN MISC CUSTOM CODE--// -void bhkShape::CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia) +void bhkShape::CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia) { center = Vector3(0,0,0); - mass = 0.0f; + mass = 0.0f, volume = 0.0f; inertia = InertiaMatrix::IDENTITY; } //--END CUSTOM CODE--// diff --git a/src/obj/bhkSphereRepShape.cpp b/src/obj/bhkSphereRepShape.cpp index 3bc83d8d0ce50be1be485b6088b7e241958f1190..dd8e3c48019cacf85028d8e6dfd0f99d00cff5c7 100644 --- a/src/obj/bhkSphereRepShape.cpp +++ b/src/obj/bhkSphereRepShape.cpp @@ -111,12 +111,12 @@ void bhkSphereRepShape::SetRadius( float value ) { radius = value; } -void bhkSphereRepShape::CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia) +void bhkSphereRepShape::CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia) { center = Vector3(0,0,0); - mass = 0.0f; + mass = 0.0f, volume = 0.0f; inertia = InertiaMatrix::IDENTITY; - Inertia::GetMassInertiaSphere(radius, density, solid, mass, inertia); + Inertia::CalcMassPropertiesSphere(radius, density, solid, mass, volume, center, inertia); } //--END CUSTOM CODE--// diff --git a/src/obj/bhkSphereShape.cpp b/src/obj/bhkSphereShape.cpp index c0c378e19df48bdd4b26d290f8f31969a3fef144..ef59048f1aa7a866572294ec080d4333967128ab 100644 --- a/src/obj/bhkSphereShape.cpp +++ b/src/obj/bhkSphereShape.cpp @@ -97,11 +97,11 @@ void bhkSphereShape::SetRadius( float value ) { radius = value; } -void bhkSphereShape::CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia) +void bhkSphereShape::CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia) { center = Vector3(0,0,0); - mass = 0.0f; + mass = 0.0f, volume = 0.0f; inertia = InertiaMatrix::IDENTITY; - Inertia::GetMassInertiaSphere(radius, density, solid, mass, inertia); + Inertia::CalcMassPropertiesSphere(radius, density, solid, mass, volume, center, inertia); } //--END CUSTOM CODE--// diff --git a/src/obj/bhkTransformShape.cpp b/src/obj/bhkTransformShape.cpp index 33fba51a4fb1043f654af7565a930ba6a1097961..4b9731745ef68a35eb58cac093f1e80e53102642 100644 --- a/src/obj/bhkTransformShape.cpp +++ b/src/obj/bhkTransformShape.cpp @@ -156,15 +156,15 @@ void bhkTransformShape::SetTransform(const Matrix44 & value ) { transform = value; } -void bhkTransformShape::CalcMassCenterInertia(float density, bool solid, float &mass, Vector3 ¢er, InertiaMatrix& inertia) +void bhkTransformShape::CalcMassProperties(float density, bool solid, float &mass, float &volume, Vector3 ¢er, InertiaMatrix& inertia) { center = transform.GetTranslation(); - mass = 0.0f; + mass = 0.0f, volume = 0.0f; inertia = InertiaMatrix::IDENTITY; if (shape != NULL) { Matrix44 transform_transposed = transform.Transpose(); - shape->CalcMassCenterInertia(density, solid, mass, center, inertia); + shape->CalcMassProperties(density, solid, mass, volume, center, inertia); center = transform * center; Matrix44 tm(inertia.Submatrix(0, 0));