From ada1acb402eaf0d24cb4ffa1300ca366dbf923dd Mon Sep 17 00:00:00 2001 From: Daniel Bross Date: Sat, 26 Jul 2025 15:02:39 +0200 Subject: [PATCH] Character controller --- core/entity.jai | 5 +++++ physics/physx.jai | 21 +++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/core/entity.jai b/core/entity.jai index d3c7da1..892bc12 100644 --- a/core/entity.jai +++ b/core/entity.jai @@ -59,6 +59,7 @@ MAX_CHILDREN :: 16; CAPSULE; TRIANGLE_MESH; CONVEX_MESH; + CHARACTER; } Physics_Lock :: enum_flags u8 { @@ -97,6 +98,10 @@ MAX_CHILDREN :: 16; radius: float; half_height: float; } + character : struct { + radius: float; + height: float; + } } physx_handle: PhysX_Handle; diff --git a/physics/physx.jai b/physics/physx.jai index 4b4cffd..feada36 100644 --- a/physics/physx.jai +++ b/physics/physx.jai @@ -1,6 +1,8 @@ PHYSX_DEFAULT_SIMULATION_SHAPE_FLAGS :: cast(u8)(PhysX.PxShapeFlags.Visualization | PhysX.PxShapeFlags.SceneQueryShape | PhysX.PxShapeFlags.SimulationShape); PHYSX_DEFAULT_TRIGGER_SHAPE_FLAGS :: cast(u8)(PhysX.PxShapeFlags.Visualization | PhysX.PxShapeFlags.SceneQueryShape | PhysX.PxShapeFlags.TriggerShape); +PHYSX_GRAVITY :: Vector3.{0, -9.81, 0}; + PhysX_Handle :: #type, distinct u32; PhysX_Actor_Type :: enum { @@ -83,14 +85,21 @@ on_physx_trigger :: (_u: *void, pair: *PhysX.PxTriggerPair, count: u32) #c_call tick_physx :: (scene: *PhysX_Scene, dt: float) { // Move all character controllers first filter_data := PhysX.PxFilterData_new(); - filter := PhysX.PxControllerFilters_new(); + filter := PhysX.PxControllerFilters_new(*filter_data, null, null); for e: engine.current_scene.entities { if e.flags & .PHYSICS { if e.physics.physx_handle != 0 { physx_actor := parray_get(*engine.current_scene.physx_scene.actors, e.physics.physx_handle); - if physx_actor.type == .CHARACTER { - movement := e.physics.velocity * dt; - flags := PhysX.PxController_move(physx_actor.controller, *movement, 0.001, dt, *filter, null); + if physx_actor { + if physx_actor.type == .CHARACTER { + vel := e.physics.velocity + PHYSX_GRAVITY * dt;; + movement := vel * dt; + flags := PhysX.PxController_move(physx_actor.controller, *movement, 0.001, dt, *filter, null); + new_position := PhysX.PxController_getPosition(physx_actor.controller); + position := Vector3.{xx new_position.x, xx new_position.y, xx new_position.z}; + e.physics.velocity = (position - e.transform.position) / dt; + set_position(e, position); + } } } } @@ -200,9 +209,9 @@ create_physx_actor :: (e: *Entity) { desc.radius = e.physics.character.radius; desc.stepOffset = 0.2; desc.slopeLimit = cos(PI * 0.25); - desc.contactOffset = 0.1; + desc.contactOffset = 0.01; desc.material = material; - desc.position = .{}; + desc.position = .{xx e.transform.position.x, xx e.transform.position.y, xx e.transform.position.z}; desc.density = 10.0; desc.userData = null;