Skip to content

Commit

Permalink
refactor(gfx/Transformable): cleaned up GetWorldMatrix()
Browse files Browse the repository at this point in the history
  • Loading branch information
tomezpl committed Oct 16, 2024
1 parent 9e0382e commit 2da67c9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 30 deletions.
9 changes: 3 additions & 6 deletions src/examples/demo/DemoApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,9 @@ class DemoApp : public system::BaseApp
KeyboardState keys = {false, false, false, false, false};

float deltaTime = 0.f;
auto transform = cube2.GetTransform();
// transform->Origin(lepus::types::Vector3(0.f, 0.f, -2.f));
// transform->SetScale(0.5f, 0.25f, 1.f);

// Parent the second cube to the first cube.
// cubeNode->AddChild(&cube2);
cube2.GetTransform()->SetScale(1.f, 0.5f, 1.f);
cube3.GetTransform()->SetScale(1.f, 2.f, 1.f);

while (isRunning)
{
Expand All @@ -213,7 +210,7 @@ class DemoApp : public system::BaseApp
cube.GetTransform()->Rotate(lepus::types::Quaternion(lepus::types::Vector3(0.f, 1.f, 0.f), PI * -0.25f));
}
cube.GetTransform()->Rotate(lepus::types::Quaternion(lepus::types::Vector3(0.f, 1.f, 0.f), -deltaTime));
cube2.GetTransform()->Rotate(lepus::types::Quaternion(lepus::types::Vector3(1.f, 0.f, 0.f), -deltaTime));
// cube2.GetTransform()->Rotate(lepus::types::Quaternion(lepus::types::Vector3(1.f, 0.f, 0.f), -deltaTime));
cube3.GetTransform()->Rotate(lepus::types::Quaternion(lepus::types::Vector3(1.f, 1.f, 1.f), -deltaTime));

// Move the child cube back and forth along the parent's Z-axis
Expand Down
32 changes: 9 additions & 23 deletions src/lepus/gfx/SceneGraph/Transformable.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ namespace lepus
{
auto parentTransform = *(leaves[i - 1]->GetTransformable()->GetTransform());

lepus::types::Vector4 rotated(leaves[i]->GetTransformable()->GetTransform()->Origin() * accScale);
rotated.w(1.f);
lepus::types::Vector3 scaled = leaves[i]->GetTransformable()->GetTransform()->Origin() * accScale;

accScale.Multiply(parentTransform.Scale());
//

parentTransform.Origin(lepus::types::Vector3());
//

lepus::types::Quaternion normParentRot = parentTransform.Rotation().Normalised();
lepus::types::Vector3 axis = normParentRot.Axis();
float angle = normParentRot.Angle();
Expand All @@ -88,31 +88,17 @@ namespace lepus
lepus::types::Vector4 rotatedAxis = mat.Multiply(lepus::types::Vector4(axis));

parentTransform.Rotation(lepus::types::Quaternion(rotatedAxis.x(), rotatedAxis.y(), rotatedAxis.z(), angle));
mat = parentTransform.BuildMatrix();
// rotated = mat.Multiply(rotated);
// rotated = accRot.Rotate(rotated);

accRot = accRot * (parentTransform.Rotation());
parentTransform.Rotation(accRot);
mat = parentTransform.BuildMatrix();
auto fwd = mat.Multiply(lepus::types::Vector4(0, 0, 1, 1)),
rgt = mat.Multiply(lepus::types::Vector4(1, 0, 0, 1)),
auto forward = mat.Multiply(lepus::types::Vector4(0, 0, 1, 1)),
right = mat.Multiply(lepus::types::Vector4(1, 0, 0, 1)),
up = mat.Multiply(lepus::types::Vector4(0, 1, 0, 1));

auto forward = lepus::types::Vector3(fwd.x(), fwd.y(), fwd.z());
auto right = lepus::types::Vector3(rgt.x(), rgt.y(), rgt.z());
auto newUp = lepus::types::Vector3(up.x(), up.y(), up.z());

// if (i == 1)
// {
// accPos.x(accPos.x() + rotated.x());
// accPos.y(accPos.y() + rotated.y());
// accPos.z(accPos.z() + rotated.z());
// }
// else
{
accPos = accPos + forward * rotated.z() + right * rotated.x() + newUp * rotated.y();
}
accPos.x(accPos.x() + forward.x() * scaled.z() + right.x() * scaled.x() + up.x() * scaled.y());
accPos.y(accPos.y() + forward.y() * scaled.z() + right.y() * scaled.x() + up.y() * scaled.y());
accPos.z(accPos.z() + forward.z() * scaled.z() + right.z() * scaled.x() + up.z() * scaled.y());
}

lepus::math::Transform worldTransform = lepus::math::Transform();
Expand Down
2 changes: 1 addition & 1 deletion tests/L3D/SceneGraph/SceneGraphTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ TEST(SceneGraphTest, SceneGraphChildIsAddedCorrectly)
/// C is also rotated counter-clockwise around the Y axis by 50 degrees. Before applying A's rotation, D ends up at approx. (-3.1, 0, -0.55) away from A in world units.
/// Once A's 90deg rotation is applied then, D should end up at approx (-0.57, 0, -5.06) in world space.
///
/// (it may be helpful to draw this on a piece of paper)
/// (it may be helpful to draw this on a piece of paper - which means this test will also not cover everything as it's limited to the XZ plane)
TEST(SceneGraphTest, SceneGraphChildTransformsCreateCorrectWorldCoords)
{
lepus::gfx::SceneGraph sceneGraph = lepus::gfx::SceneGraph();
Expand Down

0 comments on commit 2da67c9

Please sign in to comment.