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

View File

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

View File

@@ -38,7 +38,9 @@ namespace OpenEngine {
tmp_allocator = CreateRef<TempAllocatorImpl>(10 * 1024 * 1024); 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( physics_system.Init(
1024, 1024,
@@ -49,10 +51,10 @@ namespace OpenEngine {
object_vs_broadphase_layer_filter, object_vs_broadphase_layer_filter,
object_vs_object_layer_filter); object_vs_object_layer_filter);
physics_system.SetBodyActivationListener(&body_activation_listener); //physics_system.SetBodyActivationListener(&body_activation_listener);
physics_system.SetContactListener(&contact_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(); physics_system.OptimizeBroadPhase();
} }

View File

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