Files
XCEngine/tests/Components/test_physics_components.cpp

84 lines
2.8 KiB
C++

#include <gtest/gtest.h>
#include <XCEngine/Components/BoxColliderComponent.h>
#include <XCEngine/Components/CapsuleColliderComponent.h>
#include <XCEngine/Components/RigidbodyComponent.h>
#include <XCEngine/Components/SphereColliderComponent.h>
#include <XCEngine/Physics/PhysicsTypes.h>
#include <sstream>
using namespace XCEngine::Components;
namespace {
TEST(PhysicsComponents_Test, Rigidbody_SerializeRoundTripPreservesFields) {
RigidbodyComponent source;
source.SetBodyType(XCEngine::Physics::PhysicsBodyType::Kinematic);
source.SetMass(3.5f);
source.SetLinearDamping(0.2f);
source.SetAngularDamping(0.8f);
source.SetLinearVelocity(XCEngine::Math::Vector3(1.0f, 2.0f, 3.0f));
source.SetAngularVelocity(XCEngine::Math::Vector3(-4.0f, 5.0f, -6.0f));
source.SetUseGravity(false);
source.SetEnableCCD(true);
std::stringstream stream;
source.Serialize(stream);
RigidbodyComponent target;
target.Deserialize(stream);
EXPECT_EQ(target.GetBodyType(), XCEngine::Physics::PhysicsBodyType::Kinematic);
EXPECT_FLOAT_EQ(target.GetMass(), 3.5f);
EXPECT_FLOAT_EQ(target.GetLinearDamping(), 0.2f);
EXPECT_FLOAT_EQ(target.GetAngularDamping(), 0.8f);
EXPECT_EQ(target.GetLinearVelocity(), XCEngine::Math::Vector3(1.0f, 2.0f, 3.0f));
EXPECT_EQ(target.GetAngularVelocity(), XCEngine::Math::Vector3(-4.0f, 5.0f, -6.0f));
EXPECT_FALSE(target.GetUseGravity());
EXPECT_TRUE(target.GetEnableCCD());
}
TEST(PhysicsComponents_Test, BoxCollider_SerializeRoundTripPreservesFields) {
BoxColliderComponent source;
source.SetTrigger(true);
source.SetCenter(XCEngine::Math::Vector3(1.0f, 2.0f, 3.0f));
source.SetStaticFriction(0.7f);
source.SetDynamicFriction(0.2f);
source.SetRestitution(0.4f);
source.SetSize(XCEngine::Math::Vector3(2.0f, 4.0f, 6.0f));
std::stringstream stream;
source.Serialize(stream);
BoxColliderComponent target;
target.Deserialize(stream);
EXPECT_TRUE(target.IsTrigger());
EXPECT_EQ(target.GetCenter(), XCEngine::Math::Vector3(1.0f, 2.0f, 3.0f));
EXPECT_FLOAT_EQ(target.GetStaticFriction(), 0.7f);
EXPECT_FLOAT_EQ(target.GetDynamicFriction(), 0.2f);
EXPECT_FLOAT_EQ(target.GetRestitution(), 0.4f);
EXPECT_EQ(target.GetSize(), XCEngine::Math::Vector3(2.0f, 4.0f, 6.0f));
}
TEST(PhysicsComponents_Test, SphereCollider_InvalidRadiusIsSanitized) {
SphereColliderComponent sphere;
sphere.SetRadius(-10.0f);
EXPECT_GT(sphere.GetRadius(), 0.0f);
}
TEST(PhysicsComponents_Test, CapsuleCollider_HeightStaysAtLeastDiameter) {
CapsuleColliderComponent capsule;
capsule.SetRadius(1.25f);
capsule.SetHeight(1.0f);
capsule.SetAxis(ColliderAxis::Z);
EXPECT_FLOAT_EQ(capsule.GetRadius(), 1.25f);
EXPECT_FLOAT_EQ(capsule.GetHeight(), 2.5f);
EXPECT_EQ(capsule.GetAxis(), ColliderAxis::Z);
}
} // namespace