Character controller

This commit is contained in:
2025-07-26 11:53:20 +02:00
parent f62203973f
commit be4775ff93

View File

@@ -6,6 +6,7 @@ PhysX_Handle :: #type, distinct u32;
PhysX_Actor_Type :: enum {
STATIC;
DYNAMIC;
CHARACTER;
}
PhysX_Actor :: struct {
@@ -16,12 +17,15 @@ PhysX_Actor :: struct {
union {
static: *PhysX.PxRigidStatic;
dynamic: *PhysX.PxRigidDynamic;
controller: *PhysX.PxController;
}
}
PhysX_Scene :: struct {
scene: *PhysX.PxScene;
actors : PArray(PhysX_Actor, PhysX_Handle);
controller_manager: *PhysX.PxControllerManager;
}
init_physx :: () {
@@ -77,7 +81,25 @@ 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();
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);
}
}
}
}
// Simulate
PhysX.PxScene_simulate(scene.scene, dt, null, null, 0, true);
// Sync results back
PhysX.PxScene_fetchResults(scene.scene, true, null);
}
@@ -120,6 +142,7 @@ init_physx_scene :: (game_scene: *Scene) {
physx_scene.scene = scene;
physx_scene.actors.data.allocator = game_scene.allocator;
physx_scene.actors.indices.allocator = game_scene.allocator;
physx_scene.controller_manager = PhysX.PxCreateControllerManager(scene, false);
game_scene.physx_scene = physx_scene;
}
@@ -134,9 +157,11 @@ pre_physx_sync :: (game_scene: *Scene) {
if it.physics.physx_handle != 0 {
// @Incomplete: Update the transform!
physx_actor := parray_get(*game_scene.physx_scene.actors, it.physics.physx_handle);
if physx_actor.type == .DYNAMIC {
if physx_actor.type == {
case .DYNAMIC; {
PhysX.PxRigidDynamic_setLinearVelocity(physx_actor.dynamic, *it.physics.velocity, true);
}
}
} else {
create_physx_actor(it);
}
@@ -167,6 +192,33 @@ post_physx_sync :: (game_scene: *Scene) {
}
create_physx_actor :: (e: *Entity) {
if e.physics.type == .CHARACTER {
material := PhysX.PxPhysics_createMaterial(physics, e.physics.static_friction, e.physics.dynamic_friction, e.physics.restitution);
desc := PhysX.PxCapsuleControllerDesc_new_alloc();
desc.height = e.physics.character.height;
desc.radius = e.physics.character.radius;
desc.stepOffset = 0.2;
desc.slopeLimit = cos(PI * 0.25);
desc.contactOffset = 0.1;
desc.material = material;
desc.position = .{};
desc.density = 10.0;
desc.userData = null;
scene := engine.current_scene.physx_scene;
controller := PhysX.PxControllerManager_createController(scene.controller_manager, desc);
PhysX.PxCapsuleControllerDesc_delete(desc);
physics_actor : PhysX_Actor;
physics_actor.type = .CHARACTER;
physics_actor.sync_rotation_from_physx = false;//e.physics.type != .CAPSULE; // @Incomplete
physics_actor.controller = controller;
e.physics.physx_handle = parray_add(*e.scene.physx_scene.actors, physics_actor);
e.physics.enabled = true;
} else {
actor : *PhysX.PxRigidActor;
transform : PhysX.PxTransform;
position := e.transform.position + e.physics.offset;
@@ -355,6 +407,7 @@ create_physx_actor :: (e: *Entity) {
e.physics.physx_handle = parray_add(*e.scene.physx_scene.actors, physics_actor);
e.physics.enabled = true;
}
}
Hit :: struct {