Skip to content

Commit

Permalink
Removed type from 3D shape creation
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Oct 24, 2023
1 parent 11fa540 commit 4f03fab
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 19 deletions.
3 changes: 2 additions & 1 deletion engine/core/component/Body3DComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace Supernova{

struct Body3DComponent{
JPH::Body *body = NULL;


BodyType type = BodyType::DYNAMIC;
bool newBody = true;
};

Expand Down
21 changes: 15 additions & 6 deletions engine/core/object/physics/Body3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ Object Body3D::getAttachedObject(){
return Object(scene, entity);
}

void Body3D::createBoxShape(BodyType type, float width, float height, float depth){
scene->getSystem<PhysicsSystem>()->createBoxShape3D(entity, type, width, height, depth);
void Body3D::createBoxShape(float width, float height, float depth){
scene->getSystem<PhysicsSystem>()->createBoxShape3D(entity, width, height, depth);
}

void Body3D::createSphereShape(BodyType type, float radius){
scene->getSystem<PhysicsSystem>()->createSphereShape3D(entity, type, radius);
void Body3D::createSphereShape(float radius){
scene->getSystem<PhysicsSystem>()->createSphereShape3D(entity, radius);
}

void Body3D::setType(BodyType type){
Expand All @@ -70,8 +70,17 @@ void Body3D::setType(BodyType type){
JPH::PhysicsSystem* world = scene->getSystem<PhysicsSystem>()->getWorld3D();
JPH::BodyInterface &body_interface = world->GetBodyInterface();

//TODO: activate?
body_interface.SetMotionType(body.body->GetID(), getBodyTypeToJolt(type), JPH::EActivation::Activate);
body.type = type;

if (body.body){
JPH::EActivation activation = JPH::EActivation::DontActivate;

if (type != BodyType::STATIC){
activation = JPH::EActivation::Activate;
}

body_interface.SetMotionType(body.body->GetID(), getBodyTypeToJolt(type), activation);
}
}

BodyType Body3D::getType() const{
Expand Down
4 changes: 2 additions & 2 deletions engine/core/object/physics/Body3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ namespace Supernova{

Object getAttachedObject();

void createBoxShape(BodyType type, float width, float height, float depth);
void createSphereShape(BodyType type, float radius);
void createBoxShape(float width, float height, float depth);
void createSphereShape(float radius);

void setType(BodyType type);
BodyType getType() const;
Expand Down
54 changes: 47 additions & 7 deletions engine/core/subsystem/PhysicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ void PhysicsSystem::updateBody3DPosition(Signature signature, Entity entity, Bod
}
}

void PhysicsSystem::createGenericJoltBody(Entity entity, Body3DComponent& body, BodyType type, const JPH::Shape* shape){
void PhysicsSystem::createGenericJoltBody(Entity entity, Body3DComponent& body, const JPH::Shape* shape){
JPH::ObjectLayer layer = Layers::NON_MOVING;
JPH::EMotionType joltType = JPH::EMotionType::Static;
JPH::EActivation activation = JPH::EActivation::DontActivate;
if (type == BodyType::DYNAMIC){
if (body.type == BodyType::DYNAMIC){
layer = Layers::MOVING;
joltType = JPH::EMotionType::Dynamic;
activation = JPH::EActivation::Activate;
}else if (type == BodyType::KINEMATIC){
}else if (body.type == BodyType::KINEMATIC){
layer = Layers::MOVING;
joltType = JPH::EMotionType::Kinematic;
activation = JPH::EActivation::Activate;
Expand Down Expand Up @@ -418,7 +418,7 @@ void PhysicsSystem::removeBody3D(Entity entity){
}
}

void PhysicsSystem::createBoxShape3D(Entity entity, BodyType type, float width, float height, float depth){
void PhysicsSystem::createBoxShape3D(Entity entity, float width, float height, float depth){
Body3DComponent* body = scene->findComponent<Body3DComponent>(entity);

if (body){
Expand All @@ -428,14 +428,14 @@ void PhysicsSystem::createBoxShape3D(Entity entity, BodyType type, float width,
if (shape_result.IsValid()){
JPH::ShapeRefC shape = shape_result.Get();

createGenericJoltBody(entity, *body, type, shape.GetPtr());
createGenericJoltBody(entity, *body, shape.GetPtr());
}else{
Log::error("Cannot create shape for 3D Body: %u", entity);
}
}
}

void PhysicsSystem::createSphereShape3D(Entity entity, BodyType type, float radius){
void PhysicsSystem::createSphereShape3D(Entity entity, float radius){
Body3DComponent* body = scene->findComponent<Body3DComponent>(entity);

if (body){
Expand All @@ -445,13 +445,53 @@ void PhysicsSystem::createSphereShape3D(Entity entity, BodyType type, float radi
if (shape_result.IsValid()){
JPH::ShapeRefC shape = shape_result.Get();

createGenericJoltBody(entity, *body, type, shape.GetPtr());
createGenericJoltBody(entity, *body, shape.GetPtr());
}else{
Log::error("Cannot create shape for 3D Body: %u", entity);
}
}
}

void PhysicsSystem::createMeshShape3D(Entity entity, float radius){
Body3DComponent* body = scene->findComponent<Body3DComponent>(entity);

if (body){

uint32 max_material_index = 0;
JPH::TriangleList triangles;
for (int x = -10; x < 10; ++x){
for (int z = -10; z < 10; ++z)
{
float x1 = 10.0f * x;
float z1 = 10.0f * z;
float x2 = x1 + 10.0f;
float z2 = z1 + 10.0f;

JPH::Float3 v1 = JPH::Float3(x1, 0, z1);
JPH::Float3 v2 = JPH::Float3(x2, 0, z1);
JPH::Float3 v3 = JPH::Float3(x1, 0, z2);
JPH::Float3 v4 = JPH::Float3(x2, 0, z2);

uint32 material_index = uint32((JPH::Vec3(v1) + JPH::Vec3(v2) + JPH::Vec3(v3) + JPH::Vec3(v4)).Length() / 40.0f);
max_material_index = JPH::max(max_material_index, material_index);
triangles.push_back(JPH::Triangle(v1, v3, v4, material_index));
triangles.push_back(JPH::Triangle(v1, v4, v2, material_index));
}
}

//JPH::MeshShapeSettings shape_settings(triangles);

//JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create();
//if (shape_result.IsValid()){
// JPH::ShapeRefC shape = shape_result.Get();

// createGenericJoltBody(entity, *body, shape.GetPtr());
//}else{
// Log::error("Cannot create shape for 3D Body: %u", entity);
//}
}
}

b2World* PhysicsSystem::getWorld2D() const{
return world2D;
}
Expand Down
7 changes: 4 additions & 3 deletions engine/core/subsystem/PhysicsSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace Supernova{
void updateBody2DPosition(Signature signature, Entity entity, Body2DComponent& body, bool updateAnyway);
void updateBody3DPosition(Signature signature, Entity entity, Body3DComponent& body, bool updateAnyway);

void createGenericJoltBody(Entity entity, Body3DComponent& body, BodyType type, const JPH::Shape* shape);
void createGenericJoltBody(Entity entity, Body3DComponent& body, const JPH::Shape* shape);

public:
PhysicsSystem(Scene* scene);
Expand Down Expand Up @@ -104,8 +104,9 @@ namespace Supernova{
void createBody3D(Entity entity);
void removeBody3D(Entity entity);

void createBoxShape3D(Entity entity, BodyType type, float width, float height, float depth);
void createSphereShape3D(Entity entity, BodyType type, float radius);
void createBoxShape3D(Entity entity, float width, float height, float depth);
void createSphereShape3D(Entity entity, float radius);
void createMeshShape3D(Entity entity, float radius);

b2World* getWorld2D() const;
JPH::PhysicsSystem* getWorld3D() const;
Expand Down
3 changes: 3 additions & 0 deletions engine/core/util/JoltPhysicsAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
#include "Jolt/Core/JobSystemThreadPool.h"
#include "Jolt/Physics/PhysicsSettings.h"
#include "Jolt/Physics/PhysicsSystem.h"

#include "Jolt/Physics/Collision/Shape/BoxShape.h"
#include "Jolt/Physics/Collision/Shape/SphereShape.h"
#include "Jolt/Physics/Collision/Shape/MeshShape.h"

#include "Jolt/Physics/Body/BodyCreationSettings.h"
#include "Jolt/Physics/Body/BodyActivationListener.h"
#include "Jolt/Physics/Collision/GroupFilterTable.h"
Expand Down

0 comments on commit 4f03fab

Please sign in to comment.