added rotation

This commit is contained in:
Erris
2026-03-04 11:18:16 +01:00
parent eaef554b10
commit f45091349e
4 changed files with 58 additions and 45 deletions

View File

@@ -1,6 +1,6 @@
[Window][WindowOverViewport_11111111]
Pos=0,24
Size=2552,1363
Size=2560,1371
Collapsed=0
[Window][Debug##Default]
@@ -10,31 +10,31 @@ Collapsed=0
[Window][Statistics]
Pos=0,24
Size=224,437
Size=224,439
Collapsed=0
DockId=0x00000003,0
[Window][Properties]
Pos=2102,24
Size=450,805
Pos=2110,24
Size=450,810
Collapsed=0
DockId=0x00000007,0
[Window][Viewport]
Pos=226,61
Size=1874,956
Size=1882,964
Collapsed=0
DockId=0x00000012,0
[Window][Dear ImGui Demo]
Pos=2102,831
Size=450,556
Pos=2110,836
Size=450,559
Collapsed=0
DockId=0x00000008,0
[Window][Scene]
Pos=0,463
Size=224,924
Pos=0,465
Size=224,930
Collapsed=0
DockId=0x00000004,0
@@ -143,8 +143,8 @@ Collapsed=0
DockId=0x00000012,1
[Window][Assets]
Pos=226,1019
Size=1874,368
Pos=226,1027
Size=1882,368
Collapsed=0
DockId=0x0000000C,0
@@ -156,12 +156,12 @@ DockId=0x0000000F,0
[Window][##play_state_bar]
Pos=226,24
Size=1874,35
Size=1882,35
Collapsed=0
DockId=0x00000011,0
[Docking][Data]
DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,24 Size=2552,1363 Split=X
DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,24 Size=2560,1371 Split=X
DockNode ID=0x00000005 Parent=0x08BD597D SizeRef=820,1386 Split=X
DockNode ID=0x00000001 Parent=0x00000005 SizeRef=224,1386 Split=Y Selected=0xE601B12F
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=255,417 Selected=0x553E127E

View File

@@ -119,17 +119,15 @@ namespace OpenEngine {
struct PhysicsBodyComponent
{
enum class BodyType { Static = 0, Kinematic, Dynamic };
BodyID body;
BodyType type = BodyType::Dynamic;
float linear_damping = 0.05f;
float angular_damping = 0.05f;
float gravity_factor = 1.0f;
EMotionType type = EMotionType::Dynamic;
EActivation initial_activation_state = EActivation::Activate;
ObjectLayer layer = Layers::MOVING;
PhysicsBodyComponent() = default;
PhysicsBodyComponent(const PhysicsBodyComponent&) = default;

View File

@@ -38,7 +38,9 @@ namespace OpenEngine {
tmp_allocator = CreateRef<TempAllocatorImpl>(10 * 1024 * 1024);
job_system = CreateRef<JobSystemThreadPool>(cMaxPhysicsJobs, cMaxPhysicsBarriers, thread::hardware_concurrency() - 1);
job_system = CreateRef<JobSystemThreadPool>(cMaxPhysicsJobs,
cMaxPhysicsBarriers,
thread::hardware_concurrency() - 1);
physics_system.Init(
1024,
@@ -49,10 +51,10 @@ namespace OpenEngine {
object_vs_broadphase_layer_filter,
object_vs_object_layer_filter);
physics_system.SetBodyActivationListener(&body_activation_listener);
physics_system.SetContactListener(&contact_listener);
//physics_system.SetBodyActivationListener(&body_activation_listener);
//physics_system.SetContactListener(&contact_listener);
// TODO: Move out of here and check the comment on Jolt's example
// TODO: Check the comment on Jolt's example
physics_system.OptimizeBroadPhase();
}

View File

@@ -11,6 +11,7 @@
#include <Jolt/Physics/Collision/Shape/SphereShape.h>
#include <Jolt/Physics/EActivation.h>
#include <Jolt/Math/Real.h>
#include <Jolt/Math/Quat.h>
#include <cstdint>
#include <entt/entity/fwd.hpp>
@@ -36,34 +37,40 @@ namespace OpenEngine {
// Add it to the world
body_interface->AddBody(floor->GetID(), EActivation::DontActivate);
auto view = registry.view<PhysicsBodyComponent>();
// TODO: Cleanup components aquisition
auto view = registry.view<TransformComponent, PhysicsBodyComponent>();
for (auto e : view) {
Entity entity = { e, this };
TransformComponent* tc;
if (entity.HasComponent<TransformComponent>())
tc = &entity.GetComponents<TransformComponent>();
auto& pbc = entity.GetComponents<PhysicsBodyComponent>();
auto& tc = entity.GetComponents<TransformComponent>();
PhysicsShapeComponent* psc;
PhysicsShapeComponent* psc = nullptr;
if (entity.HasComponent<PhysicsShapeComponent>())
psc = &entity.GetComponents<PhysicsShapeComponent>();
glm::vec3& pos = tc->translation;
glm::vec3& pos = tc.translation;
glm::vec3& scale = tc.scale;
glm::vec3& rot = tc.rotation;
Quat quat = Quat::sEulerAngles(Vec3(rot.x, rot.y, rot.z));
BodyCreationSettings settings(
new BoxShape(Vec3(0.5f, 0.5f, 0.5f)),
new BoxShape(Vec3(scale.x * 0.5, scale.y * 0.5f, scale.z * 0.5f)),
Vec3(pos.x, pos.y, pos.z),
Quat::sIdentity(),
EMotionType::Dynamic,
Layers::MOVING);
quat,
pbc.type,
pbc.layer);
settings.mLinearDamping = pbc.linear_damping;
settings.mAngularDamping = pbc.angular_damping;
settings.mGravityFactor = pbc.gravity_factor;
settings.mRestitution = psc->restitution;
settings.mFriction = psc->friction;
if (psc != nullptr) {
settings.mRestitution = psc->restitution;
settings.mFriction = psc->friction;
}
pbc.body = body_interface->CreateAndAddBody(settings, EActivation::Activate);
pbc.body = body_interface->CreateAndAddBody(settings, pbc.initial_activation_state);
}
}
@@ -75,8 +82,8 @@ namespace OpenEngine {
auto& pbc = entity.GetComponents<PhysicsBodyComponent>();
physics_engine.GetBodyInterface().RemoveBody(pbc.body);
physics_engine.GetBodyInterface().DestroyBody(pbc.body);
body_interface->RemoveBody(pbc.body);
body_interface->DestroyBody(pbc.body);
}
}
@@ -156,7 +163,6 @@ namespace OpenEngine {
body_interface->SetRestitution(body.body, shape->restitution);
body_interface->SetFriction(body.body, shape->friction);
body_interface->SetGravityFactor(body.body, body.gravity_factor);
}
}
@@ -169,10 +175,17 @@ namespace OpenEngine {
auto& transform = entity.GetComponents<TransformComponent>();
auto& body = entity.GetComponents<PhysicsBodyComponent>();
auto position = physics_engine.GetBodyInterface().GetPosition(body.body);
auto position = body_interface->GetPosition(body.body);
auto rotation = body_interface->GetRotation(body.body).GetEulerAngles();
transform.translation.x = position.GetX();
transform.translation.y = position.GetY();
transform.translation.z = position.GetZ();
transform.rotation.x = rotation.GetX();
transform.rotation.y = rotation.GetY();
transform.rotation.z = rotation.GetZ();
body_interface->SetGravityFactor(body.body, body.gravity_factor);
}
SceneCamera* main_camera = nullptr;
@@ -194,16 +207,16 @@ namespace OpenEngine {
auto view = registry.view<TransformComponent, MeshComponent>();
for (const auto& entity : view) {
auto [transform, mesh] = view.get<TransformComponent, MeshComponent>(entity);
for (const auto& e : view) {
auto [transform, mesh] = view.get<TransformComponent, MeshComponent>(e);
Material material;
Material* material;
Entity _entity(entity, this);
if (_entity.HasComponent<MaterialComponent>())
material = _entity.GetComponents<MaterialComponent>().material;
Entity entity(e, this);
if (entity.HasComponent<MaterialComponent>())
material = &entity.GetComponents<MaterialComponent>().material;
Renderer3D::DrawMesh(mesh.mesh, material, GetTransformFromComp(transform));
Renderer3D::DrawMesh(mesh.mesh, *material, GetTransformFromComp(transform));
/*
if (sprite.texture)
Renderer2D::DrawQuad(GetTransformFromComp(transform),