Skip to content

Commit

Permalink
Merge pull request #1799 from AlesBorovicka/physics-mass-tests
Browse files Browse the repository at this point in the history
physics mass properties computation fix

(Internal change: 2223140)
  • Loading branch information
pixar-oss committed Mar 25, 2022
2 parents e04b228 + ac291f8 commit e4ee01d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pxr/usd/usdPhysics/massProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ GfQuatf UsdPhysicsIndexedRotation(uint32_t axis, float s, float c)
{
float v[3] = { 0, 0, 0 };
v[axis] = s;
return GfQuatf(v[0], v[1], v[2], c);
return GfQuatf(c, v[0], v[1], v[2]);
}

uint32_t UsdPhysicsGetNextIndex3(uint32_t i)
Expand All @@ -59,7 +59,7 @@ GfVec3f UsdPhysicsDiagonalize(const GfMatrix3f& m, GfQuatf& massFrame)
for (uint32_t i = 0; i < MAX_ITERS; i++)
{
GfMatrix3f axes(q);
d = axes.GetTranspose() * m * axes;
d = axes * m * axes.GetTranspose();

float d0 = fabs(d[1][2]), d1 = fabs(d[0][2]), d2 = fabs(d[0][1]);
uint32_t a = uint32_t(d0 > d1 && d0 > d2 ? 0 : d1 > d2 ? 1 : 2); // rotation axis index, from largest
Expand Down Expand Up @@ -157,7 +157,7 @@ class UsdPhysicsMassProperties
s.SetColumn(1, GfVec3f(-t[2], 0, t[0]));
s.SetColumn(2, GfVec3f(t[1], -t[0], 0));

GfMatrix3f translatedIT = s.GetTranspose() * s * mass + inertia;
GfMatrix3f translatedIT = s * s.GetTranspose() * mass + inertia;
return translatedIT;
}

Expand All @@ -169,7 +169,7 @@ class UsdPhysicsMassProperties
USDPHYSICS_API static GfMatrix3f RotateInertia(const GfMatrix3f& inertia, const GfQuatf& q)
{
GfMatrix3f m(q);
GfMatrix3f rotatedIT = m * inertia * m.GetTranspose();
GfMatrix3f rotatedIT = m.GetTranspose() * inertia * m;
return rotatedIT;
}

Expand Down
43 changes: 43 additions & 0 deletions pxr/usd/usdPhysics/testenv/testUsdPhysicsRigidBodyAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,49 @@ def test_mass_rigid_body_cube_inertia_precedence(self):

self.compare_mass_information(rigidBodyAPI, 1000.0, expectedInertia=Gf.Vec3f(2.0))

# inertia test, test compound body
def test_mass_rigid_body_cube_rigid_body_compound(self):
self.setup_scene()

# top level xform - rigid body
self.xform = UsdGeom.Xform.Define(self.stage, "/xform")
rigidBodyAPI = UsdPhysics.RigidBodyAPI.Apply(self.xform.GetPrim())

size = 1.0
scale = Gf.Vec3f(3.0, 2.0, 3.0)

# Create test collider cube0
cube = UsdGeom.Cube.Define(self.stage, "/xform/cube0")
cube.CreateSizeAttr(size)
cube.AddTranslateOp().Set(Gf.Vec3f(100.0, 20.0, 10.0))
cube0RotateOp = cube.AddRotateXYZOp()
cube0RotateOp.Set(Gf.Vec3f(0.0,0.0,45.0))
cube.AddScaleOp().Set(scale)
UsdPhysics.CollisionAPI.Apply(cube.GetPrim())

# Create test collider cube1
cube = UsdGeom.Cube.Define(self.stage, "/xform/cube1")
cube.CreateSizeAttr(size)
cube.AddTranslateOp().Set(Gf.Vec3f(-100.0, 20.0, 10.0))
cube1RotateOp = cube.AddRotateXYZOp()
cube1RotateOp.Set(Gf.Vec3f(0.0,0.0,45.0))
cube.AddScaleOp().Set(scale)
UsdPhysics.CollisionAPI.Apply(cube.GetPrim())

self.rigidBodyWorldTransform = UsdGeom.Xformable(self.xform.GetPrim()).ComputeLocalToWorldTransform(Usd.TimeCode.Default())
self.rigidBodyPrim = self.xform.GetPrim()

mass, inertia_compare, centerOfMass, principalAxes = rigidBodyAPI.ComputeMassProperties(self.mass_information_fn)

cube0RotateOp.Set(Gf.Vec3f(0.0,90.0,45.0))
cube1RotateOp.Set(Gf.Vec3f(0.0,0.0,45.0))

mass, inertia, centerOfMass, principalAxes = rigidBodyAPI.ComputeMassProperties(self.mass_information_fn)

toleranceEpsilon = 1
self.assertTrue(Gf.IsClose(inertia, inertia_compare, toleranceEpsilon))


# principal axes tests
# principal axes test, applied principal axis to a body
def test_mass_rigid_body_cube_rigid_body_principal_axes(self):
Expand Down

0 comments on commit e4ee01d

Please sign in to comment.