From 50e3ddf0dd5d77681141a582b1a4fcf5b2549a0d Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Fri, 30 Dec 2022 17:59:59 +0800 Subject: [PATCH] Fixed box inertia tensor calculation --- Assets/Scenes/PhysicsSandbox.shade | 5 ++++- .../CollisionShapes/SHBoxCollisionShape.cpp | 14 +++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Assets/Scenes/PhysicsSandbox.shade b/Assets/Scenes/PhysicsSandbox.shade index 67d35bb5..4e5e40c2 100644 --- a/Assets/Scenes/PhysicsSandbox.shade +++ b/Assets/Scenes/PhysicsSandbox.shade @@ -19,7 +19,7 @@ Interpolate: true Sleeping Enabled: true Freeze Position X: false - Freeze Position Y: false + Freeze Position Y: true Freeze Position Z: false Freeze Rotation X: false Freeze Rotation Y: false @@ -29,6 +29,7 @@ DrawColliders: false Colliders: - Is Trigger: false + Collision Tag: 1 Type: Sphere Radius: 1 Friction: 0.400000006 @@ -90,6 +91,7 @@ DrawColliders: false Colliders: - Is Trigger: false + Collision Tag: 1 Type: Sphere Radius: 2.5 Friction: 0.400000006 @@ -130,6 +132,7 @@ DrawColliders: false Colliders: - Is Trigger: false + Collision Tag: 1 Type: Box Half Extents: {x: 1, y: 1, z: 1} Friction: 0.400000006 diff --git a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBoxCollisionShape.cpp b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBoxCollisionShape.cpp index 6d7308da..f94e850f 100644 --- a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBoxCollisionShape.cpp +++ b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBoxCollisionShape.cpp @@ -236,9 +236,17 @@ namespace SHADE { static constexpr float ONE_OVER_TWELVE = (1.0f / 12.0f); - const float H2_PLUS_D2 = Extents.y * Extents.y + Extents.z * Extents.z; - const float W2_PLUS_H2 = Extents.x * Extents.x + Extents.y * Extents.y; - const float W2_PLUS_D2 = Extents.x * Extents.x + Extents.z * Extents.z; + const float WIDTH = 2.0f * Extents.x; + const float HEIGHT = 2.0f * Extents.y; + const float DEPTH = 2.0f * Extents.z; + + const float WIDTH_SQUARED = WIDTH * WIDTH; + const float HEIGHT_SQUARED = HEIGHT * HEIGHT; + const float DEPTH_SQUARED = DEPTH * DEPTH; + + const float H2_PLUS_D2 = HEIGHT_SQUARED + DEPTH_SQUARED; + const float W2_PLUS_H2 = WIDTH_SQUARED + HEIGHT_SQUARED; + const float W2_PLUS_D2 = WIDTH_SQUARED + DEPTH_SQUARED; SHMatrix result; result.m[0][0] = ONE_OVER_TWELVE * mass * H2_PLUS_D2;