added rotation
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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& scale = tc.scale;
|
||||
glm::vec3& rot = tc.rotation;
|
||||
|
||||
glm::vec3& pos = tc->translation;
|
||||
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),
|
||||
|
||||
Reference in New Issue
Block a user