Improve physics bindings

This commit is contained in:
luboslenco
2024-11-27 15:21:42 +01:00
parent a97ee53f39
commit 3c69cdad0d
3 changed files with 38 additions and 27 deletions
+30 -22
View File
@@ -7,6 +7,7 @@
#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>
@@ -156,27 +157,22 @@ void _jolt_world_destroy() {
Factory::sInstance = nullptr;
}
void *_jolt_body_create(int shape, float mass, float dimx, float x, float y, float z, void *f32a_triangles) {
void *_jolt_body_create(int shape, float mass, float dimx, float dimy, float dimz, float x, float y, float z, void *f32a_triangles) {
BodyInterface &body_interface = physics_system->GetBodyInterface();
Body *body;
ShapeSettings::ShapeResult result;
if (shape == 0) {
// Box
BoxShapeSettings shape_settings(RVec3(dimx, dimy, dimz));
shape_settings.SetEmbedded();
result = shape_settings.Create();
}
else if (shape == 1) {
// Sphere
SphereShapeSettings shape_settings(dimx);
shape_settings.SetEmbedded();
ShapeSettings::ShapeResult result = shape_settings.Create();
ShapeRefC shape = result.Get();
BodyCreationSettings settings(shape, RVec3(x, y, z), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
settings.mMotionQuality = EMotionQuality::LinearCast; // CCD
MassProperties mass_prop;
mass_prop.ScaleToMass(mass);
settings.mMassPropertiesOverride = mass_prop;
settings.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
body = body_interface.CreateBody(settings);
body_interface.AddBody(body->GetID(), EActivation::Activate);
result = shape_settings.Create();
}
else {
// Mesh
@@ -191,14 +187,26 @@ void *_jolt_body_create(int shape, float mass, float dimx, float x, float y, flo
}
MeshShapeSettings shape_settings(triangles);
shape_settings.SetEmbedded();
ShapeSettings::ShapeResult result = shape_settings.Create();
ShapeRefC shape = result.Get();
BodyCreationSettings settings(shape, RVec3(x, y, z), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
body = body_interface.CreateBody(settings);
body_interface.AddBody(body->GetID(), EActivation::Activate);
result = shape_settings.Create();
}
ShapeRefC shape_c = result.Get();
BodyCreationSettings settings(shape_c, RVec3(x, y, z), Quat::sIdentity(), mass == 0 ? EMotionType::Static : EMotionType::Dynamic,
mass == 0 ? Layers::NON_MOVING : Layers::MOVING);
// if (ccd) {
if (shape != 2) { // != Mesh
settings.mMotionQuality = EMotionQuality::LinearCast;
}
MassProperties mass_prop;
mass_prop.ScaleToMass(mass);
settings.mMassPropertiesOverride = mass_prop;
settings.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
body = body_interface.CreateBody(settings);
body_interface.AddBody(body->GetID(), EActivation::Activate);
return body;
}
@@ -246,8 +254,8 @@ extern "C" {
_jolt_world_destroy();
}
void *jolt_body_create(int shape, float mass, float dimx, float x, float y, float z, void *f32a_triangles) {
return _jolt_body_create(shape, mass, dimx, x, y, z, f32a_triangles);
void *jolt_body_create(int shape, float mass, float dimx, float dimy, float dimz, float x, float y, float z, void *f32a_triangles) {
return _jolt_body_create(shape, mass, dimx, dimy, dimz, x, y, z, f32a_triangles);
}
void jolt_body_apply_impulse(void *body, float x, float y, float z) {
+1 -1
View File
@@ -16,7 +16,7 @@ void jolt_world_update();
physics_pair_t *jolt_world_get_contact_pairs();
void jolt_world_destroy();
void* jolt_body_create(int shape, float mass, float dimx, float x, float y, float z, void *f32a_triangles);
void* jolt_body_create(int shape, float mass, float dimx, float dimy, float dimz, float x, float y, float z, void *f32a_triangles);
void jolt_body_apply_impulse(void *b, float x, float y, float z);
void jolt_body_get_pos(void *b, void *p);
void jolt_body_get_rot(void *b, void *r);
+7 -4
View File
@@ -3,7 +3,7 @@
///include <phys_jolt.h>
declare function jolt_body_create(shape: i32, mass: f32, dimx: f32, x: f32, y: f32, z: f32, triangles: f32[]): any;
declare function jolt_body_create(shape: i32, mass: f32, dimx: f32, dimy: f32, dimz: f32, x: f32, y: f32, z: f32, triangles: f32[]): any;
declare function jolt_body_apply_impulse(body: any, x: f32, y: f32, z: f32): void;
declare function jolt_body_get_pos(body: any, pos: vec4_t): void;
declare function jolt_body_get_rot(body: any, rot: quat_t): void;
@@ -13,12 +13,15 @@ type physics_body_t = {
shape?: physics_shape_t;
mass?: f32;
dimx?: f32;
dimy?: f32;
dimz?: f32;
obj?: object_t;
};
enum physics_shape_t {
SPHERE = 0,
MESH = 1,
BOX = 0,
SPHERE = 1,
MESH = 2,
}
let physics_body_object_map: map_t<i32, physics_body_t> = map_create();
@@ -69,7 +72,7 @@ function physics_body_init(body: physics_body_t, obj: object_t) {
}
let loc: vec4_t = obj.transform.loc;
body._body = jolt_body_create(body.shape, body.mass, body.dimx, loc.x, loc.y, loc.z, triangles);
body._body = jolt_body_create(body.shape, body.mass, body.dimx, body.dimy, body.dimz, loc.x, loc.y, loc.z, triangles);
}
function physics_body_apply_impulse(body: physics_body_t, dir: vec4_t) {