Added width to trigger line rendering, abstracted PhysX away a bit

This commit is contained in:
2025-07-19 13:02:38 +02:00
parent 2f6ca9d15b
commit 9353bc3e07
5 changed files with 297 additions and 91 deletions

View File

@@ -1,4 +1,5 @@
PHYSX_DEFAULT_SHAPE_FLAGS :: cast(u8)(PhysX.PxShapeFlags.Visualization | PhysX.PxShapeFlags.SceneQueryShape | PhysX.PxShapeFlags.SimulationShape);
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_Handle :: #type, distinct u32;
@@ -44,6 +45,33 @@ init_physx :: () {
dispatcher = PhysX.PxDefaultCpuDispatcherCreate(2);
material = PhysX.PxPhysics_createMaterial(physics, 0.0, 0.0, 0.6);
// Callbacks
info : PhysX.SimulationEventCallbackInfo;
info.triggerCallback = on_physx_trigger;
event_callback = PhysX.create_simulation_event_callbacks(*info);
}
internal_on_trigger_enter :: (trigger: *Entity, other: *Entity) {
print("% entered by %\n", trigger.type, other.type);
}
internal_on_trigger_exit :: (trigger: *Entity, other: *Entity) {
print("% exited by %\n", trigger.type, other.type);
}
on_physx_trigger :: (_u: *void, pair: *PhysX.PxTriggerPair, count: u32) #c_call {
push_context {
trigger := cast(*Entity)pair.triggerActor.userData;
other := cast(*Entity)pair.otherActor.userData;
status := cast(PhysX.PxPairFlags)pair.status;
if status & .NotifyTouchFound {
internal_on_trigger_enter(trigger, other);
} else if status & .NotifyTouchLost {
internal_on_trigger_exit(trigger, other);
}
}
}
tick_physx :: (scene: *PhysX_Scene, dt: float) {
@@ -60,6 +88,7 @@ init_physx_scene :: (game_scene: *Scene) {
scene_desc.gravity.y = -9.81;
scene_desc.cpuDispatcher = xx dispatcher;
scene_desc.simulationEventCallback = event_callback;
PhysX.set_default_filter_shader(*scene_desc);
scene := PhysX.PxPhysics_createScene(physics, *scene_desc);
@@ -86,11 +115,15 @@ deinit_physx_scene :: (game_scene: *Scene) {
pre_physx_sync :: (game_scene: *Scene) {
for game_scene.entities {
if it.physx_handle != 0 {
// @Incomplete: Update the transform!
physx_actor := parray_get(*game_scene.physx_scene.actors, it.physx_handle);
if physx_actor.type == .DYNAMIC {
PhysX.PxRigidDynamic_setLinearVelocity(physx_actor.dynamic, *it.velocity, true);
if it.flags & .PHYSICS {
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 {
PhysX.PxRigidDynamic_setLinearVelocity(physx_actor.dynamic, *it.physics.velocity, true);
}
} else {
create_physx_actor(it);
}
}
}
@@ -98,33 +131,106 @@ pre_physx_sync :: (game_scene: *Scene) {
post_physx_sync :: (game_scene: *Scene) {
for game_scene.entities {
if it.physx_handle != 0 {
physx_actor := parray_get(*game_scene.physx_scene.actors, it.physx_handle);
if physx_actor.type == .DYNAMIC {
vel := PhysX.PxRigidDynamic_getLinearVelocity(physx_actor.dynamic);
it.velocity = vel;
if it.flags & .PHYSICS {
if it.physics.physx_handle != 0 {
physx_actor := parray_get(*game_scene.physx_scene.actors, it.physics.physx_handle);
if physx_actor.type == .DYNAMIC {
vel := PhysX.PxRigidDynamic_getLinearVelocity(physx_actor.dynamic);
it.physics.velocity = vel;
transform := PhysX.PxRigidActor_getGlobalPose(physx_actor.dynamic);
transform := PhysX.PxRigidActor_getGlobalPose(physx_actor.dynamic);
if physx_actor.sync_rotation_from_physx {
set_position_rotation(it, transform.p, transform.q);
} else {
set_position(it, transform.p);
if physx_actor.sync_rotation_from_physx {
set_position_rotation(it, transform.p, transform.q);
} else {
set_position(it, transform.p);
}
}
}
}
}
}
create_physx_actor :: (e: *Entity) {
actor : *PhysX.PxRigidActor;
transform : PhysX.PxTransform;
if e.physics.type == .CAPSULE {
angle := PI * 0.5;
half_angle := angle * 0.5;
sin_half := sin(half_angle);
cos_half := cos(half_angle);
rotation := Quaternion.{0, 0, sin(-PI * 0.25), cos(-PI * 0.25)};
transform = PhysX.PxTransform_new(*e.transform.position, *rotation);
} else {
transform = PhysX.PxTransform_new(*e.transform.position, *e.transform.orientation);
}
if e.physics.dynamic {
dynamic := PhysX.PxPhysics_createRigidDynamic(physics, *transform);
actor = dynamic;
if e.physics.lock & .ANGULAR_X {
PhysX.PxRigidDynamic_setRigidDynamicLockFlag(dynamic, xx PhysX.PxRigidDynamicLockFlags.LockAngularX, true);
}
if e.physics.lock & .ANGULAR_Y {
PhysX.PxRigidDynamic_setRigidDynamicLockFlag(dynamic, xx PhysX.PxRigidDynamicLockFlags.LockAngularY, true);
}
if e.physics.lock & .ANGULAR_Z {
PhysX.PxRigidDynamic_setRigidDynamicLockFlag(dynamic, xx PhysX.PxRigidDynamicLockFlags.LockAngularZ, true);
}
} else {
actor = PhysX.PxPhysics_createRigidStatic(physics, *transform);
}
material := PhysX.PxPhysics_createMaterial(physics, e.physics.static_friction, e.physics.dynamic_friction, e.physics.restitution);
geo : *PhysX.PxGeometry;
actor.userData = e;
if e.physics.type == {
case .SPHERE; {
geo = PhysX.PxSphereGeometry_new(e.physics.sphere.radius);
}
case .BOX; {
geo = PhysX.PxBoxGeometry_new(e.physics.box.half_extent*e.transform.scale);
}
case .CAPSULE; {
geo = PhysX.PxCapsuleGeometry_new(e.physics.capsule.radius, e.physics.capsule.half_height-e.physics.capsule.radius);
}
}
shape := PhysX.PxPhysics_createShape(physics, geo, material, false, ifx e.physics.trigger then PHYSX_DEFAULT_TRIGGER_SHAPE_FLAGS else PHYSX_DEFAULT_SIMULATION_SHAPE_FLAGS);
PhysX.PxRigidActor_attachShape(actor, shape);
PhysX.PxScene_addActor(e.scene.physx_scene.scene, actor, null);
PhysX.PxShape_release(shape);
PhysX.PxBase_release(material);
physics_actor : PhysX_Actor;
physics_actor.type = ifx e.physics.dynamic then .DYNAMIC else .STATIC;
physics_actor.sync_rotation_from_physx = e.physics.type != .CAPSULE; // @Incomplete
if physics_actor.type == .DYNAMIC {
physics_actor.dynamic = xx actor;
} else {
physics_actor.static = xx actor;
}
e.physics.physx_handle = parray_add(*e.scene.physx_scene.actors, physics_actor);
e.physics.enabled = true;
}
add_physx_sphere :: (entity: *Entity, radius: float) -> PhysX_Handle {
geo := PhysX.PxSphereGeometry_new(radius);
transform := PhysX.PxTransform_new(*entity.transform.position);
actor := PhysX.PxPhysics_createRigidDynamic(physics, *transform);
//PhysX.PxRigidDynamic_setRigidDynamicLockFlag(actor, xx PhysX.PxRigidDynamicLockFlags.LockAngularX, true);
//PhysX.PxRigidDynamic_setRigidDynamicLockFlag(actor, xx PhysX.PxRigidDynamicLockFlags.LockAngularY, true);
//PhysX.PxRigidDynamic_setRigidDynamicLockFlag(actor, xx PhysX.PxRigidDynamicLockFlags.LockAngularZ, true);
actor.userData = entity;
material := PhysX.PxPhysics_createMaterial(physics, 0.1, 0.1, 0.5);
@@ -160,7 +266,10 @@ add_physx_capsule :: (entity: *Entity, half_height: float, radius: float) -> Phy
rotation := Quaternion.{0, 0, sin(-PI * 0.25), cos(-PI * 0.25)};
transform := PhysX.PxTransform_new(*entity.transform.position, *rotation);
actor := PhysX.PxPhysics_createRigidDynamic(physics, *transform);
actor.userData = entity;
PhysX.PxRigidDynamic_setRigidDynamicLockFlag(actor, xx PhysX.PxRigidDynamicLockFlags.LockAngularX, true);
PhysX.PxRigidDynamic_setRigidDynamicLockFlag(actor, xx PhysX.PxRigidDynamicLockFlags.LockAngularY, true);
PhysX.PxRigidDynamic_setRigidDynamicLockFlag(actor, xx PhysX.PxRigidDynamicLockFlags.LockAngularZ, true);
@@ -184,7 +293,7 @@ add_physx_capsule :: (entity: *Entity, half_height: float, radius: float) -> Phy
physics_actor.dynamic = actor;
entity.physx_handle = parray_add(*entity.scene.physx_scene.actors, physics_actor);
return entity.physx_handle;
return entity.physics.physx_handle;
}
add_physx_box :: (entity: *Entity, half_extent: Vector3, offset: Vector3 = .{}) -> PhysX_Handle {
@@ -193,6 +302,7 @@ add_physx_box :: (entity: *Entity, half_extent: Vector3, offset: Vector3 = .{})
t := PhysX.PxTransform_new(*pos, *entity.transform.orientation);
body := PhysX.PxPhysics_createRigidStatic(physics, *t);
body.userData = entity;
PhysX.PxRigidActor_attachShape(body, shape);
PhysX.PxScene_addActor(entity.scene.physx_scene.scene, body, null);
@@ -203,7 +313,7 @@ add_physx_box :: (entity: *Entity, half_extent: Vector3, offset: Vector3 = .{})
physics_actor.static = body;
entity.physx_handle = parray_add(*entity.scene.physx_scene.actors, physics_actor);
return entity.physx_handle;
return entity.physics.physx_handle;
}
@@ -254,4 +364,5 @@ default_allocator : PhysX.PxAllocatorCallback;
default_error_callback : PhysX.PxErrorCallback;
default_filter_shader : PhysX.SimulationFilterShader;
dispatcher : *PhysX.PxDefaultCpuDispatcher;
event_callback : *PhysX.PxSimulationEventCallback;