PhysX hello world now working

This commit is contained in:
2025-07-11 00:52:44 +02:00
parent 038b007c70
commit 7e354b0417

View File

@@ -13,7 +13,7 @@ init_physx :: () {
PhysX.PxPvd_connect(pvd, transport, cast(u8)PhysX.PxPvdInstrumentationFlags.ALL); PhysX.PxPvd_connect(pvd, transport, cast(u8)PhysX.PxPvdInstrumentationFlags.ALL);
tolerance_scale : PhysX.PxTolerancesScale; tolerance_scale : PhysX.PxTolerancesScale;
tolerance_scale.length = 100; tolerance_scale.length = 1;
tolerance_scale.speed = 981; tolerance_scale.speed = 981;
physics = PhysX.PxCreatePhysics(PhysX.PX_PHYSICS_VERSION, foundation, *tolerance_scale, true, pvd, null); physics = PhysX.PxCreatePhysics(PhysX.PX_PHYSICS_VERSION, foundation, *tolerance_scale, true, pvd, null);
@@ -22,20 +22,23 @@ init_physx :: () {
} }
tick_physx :: (scene: *PhysX.PxScene, dt: float) { tick_physx :: (scene: *PhysX.PxScene, dt: float) {
PhysX.PxScene_simulate(scene, dt, null, null, 0, false); PhysX.PxScene_simulate(scene, 1.0/60.0, null, null, 0, true);
PhysX.PxScene_fetchResults(scene, true, null); PhysX.PxScene_fetchResults(scene, true, null);
} }
plane : Plane3;
create_physx_scene :: () -> *PhysX.PxScene { create_physx_scene :: () -> *PhysX.PxScene {
tolerance_scale : PhysX.PxTolerancesScale; tolerance_scale : PhysX.PxTolerancesScale;
tolerance_scale.length = 100; tolerance_scale.length = 1;
tolerance_scale.speed = 981; tolerance_scale.speed = 10;
scene_desc := PhysX.PxSceneDesc_new(*tolerance_scale); scene_desc := PhysX.PxSceneDesc_new(*tolerance_scale);
scene_desc.gravity.y = -9.81; scene_desc.gravity.y = -9.81;
scene_desc.cpuDispatcher = xx dispatcher; scene_desc.cpuDispatcher = xx dispatcher;
PhysX.set_default_filter_shader(*scene_desc); PhysX.set_default_filter_shader(*scene_desc);
scene := PhysX.PxPhysics_createScene(physics, *scene_desc); scene := PhysX.PxPhysics_createScene(physics, *scene_desc);
// @Incomplete: If debug // @Incomplete: If debug
@@ -44,45 +47,42 @@ create_physx_scene :: () -> *PhysX.PxScene {
PhysX.PxPvdSceneClient_setScenePvdFlag(pvd_client, xx PhysX.PxPvdSceneFlag.TRANSMIT_CONSTRAINTS, true); PhysX.PxPvdSceneClient_setScenePvdFlag(pvd_client, xx PhysX.PxPvdSceneFlag.TRANSMIT_CONSTRAINTS, true);
PhysX.PxPvdSceneClient_setScenePvdFlag(pvd_client, xx PhysX.PxPvdSceneFlag.TRANSMIT_CONTACTS, true); PhysX.PxPvdSceneClient_setScenePvdFlag(pvd_client, xx PhysX.PxPvdSceneFlag.TRANSMIT_CONTACTS, true);
PhysX.PxPvdSceneClient_setScenePvdFlag(pvd_client, xx PhysX.PxPvdSceneFlag.TRANSMIT_SCENEQUERIES, true); PhysX.PxPvdSceneClient_setScenePvdFlag(pvd_client, xx PhysX.PxPvdSceneFlag.TRANSMIT_SCENEQUERIES, true);
//PhysX.PxScene_setVisualizationParameter(scene, 0, 1.0);
//PhysX.PxScene_setVisualizationParameter(scene, 1, 2.0);
} }
{ {
plane := PhysX.PxPlane_new(0.0,-1.0,0.0,50.0); plane_point := Vector3.{0,0,0};
ground_plane := PhysX.PxCreatePlane(physics, *plane, material); plane_normal := Vector3.{0,1,0};
PhysX.PxScene_addActor(scene, ground_plane, null); plane = PhysX.PxPlane_new(*plane_point, *plane_normal);
} ground_plane := PhysX.PxCreatePlane(physics, *plane, material);
PhysX.PxScene_addActor(scene, ground_plane, null);
{ }
plane := PhysX.PxPlane_new(0.0,-1.0,0.0,50.0);
ground_plane := PhysX.PxCreatePlane(physics, *plane, material);
PhysX.PxScene_addActor(scene, ground_plane, null);
}
{
plane := PhysX.PxPlane_new(0.0,-1.0,0.0,50.0);
ground_plane := PhysX.PxCreatePlane(physics, *plane, material);
PhysX.PxScene_addActor(scene, ground_plane, null);
}
{
plane := PhysX.PxPlane_new(0.0,-1.0,0.0,50.0);
ground_plane := PhysX.PxCreatePlane(physics, *plane, material);
PhysX.PxScene_addActor(scene, ground_plane, null);
}
stack_z := 10.0; stack_z := 0.0;
for i: 0..4 { for i: 0..4 {
stack_pos := Vector3.{0,100,stack_z}; stack_pos := Vector3.{0,10,stack_z};
stack_z -= 10.0; stack_z -= 10.0;
create_stack(scene, PhysX.PxTransform_new(*stack_pos), 10, 2.0); create_stack(scene, PhysX.PxTransform_new(*stack_pos), 10, 2.0);
} }
{
pos := Vector3.{0,20,100};
geo := PhysX.PxSphereGeometry_new(5);
vel := Vector3.{0,-25,-100};
ball := create_dynamic(scene, PhysX.PxTransform_new(*pos), *geo, vel);
density := 1000.0;
PhysX.PxRigidBodyExt_updateMassAndInertia(ball, density, null, true);
}
return scene; return scene;
} }
PxShapeFlags :: enum_flags u8 {
SimulationShape :: 1 << 0;
SceneQueryShape :: 1 << 1;
TriggerShape :: 1 << 2;
Visualization :: 1 << 3;
}
create_stack :: (scene: *PhysX.PxScene, t: PhysX.PxTransform, size: u32, half_extent: float) { create_stack :: (scene: *PhysX.PxScene, t: PhysX.PxTransform, size: u32, half_extent: float) {
shape := PhysX.PxPhysics_createShape(physics, PhysX.PxBoxGeometry_new(half_extent, half_extent, half_extent), material, false, 0); shape := PhysX.PxPhysics_createShape(physics, PhysX.PxBoxGeometry_new(half_extent, half_extent, half_extent), material, false, xx (PhysX.PxShapeFlags.Visualization | PhysX.PxShapeFlags.SceneQueryShape | PhysX.PxShapeFlags.SimulationShape));
for i: 0..size-1 { for i: 0..size-1 {
for j: 0..size-i-1 { for j: 0..size-i-1 {
pos := Vector3.{cast(float)(j*2) - cast(float)(size-i), cast(float)(i*2+1), 0} * half_extent; pos := Vector3.{cast(float)(j*2) - cast(float)(size-i), cast(float)(i*2+1), 0} * half_extent;
@@ -98,9 +98,10 @@ create_stack :: (scene: *PhysX.PxScene, t: PhysX.PxTransform, size: u32, half_ex
} }
create_dynamic :: (scene: *PhysX.PxScene, t: PhysX.PxTransform, geometry: PhysX.PxGeometry, velocity: Vector3 = .{}) -> *PhysX.PxRigidDynamic { create_dynamic :: (scene: *PhysX.PxScene, t: PhysX.PxTransform, geometry: *PhysX.PxGeometry, velocity: Vector3 = .{}) -> *PhysX.PxRigidDynamic {
p := Vector3.{0,0,0}; p := Vector3.{0,0,0};
dynamic := PhysX.PxCreateDynamic(physics, *t, *geometry, material, 10.0, *PhysX.PxTransform_new(cast(s32)PhysX.PxIDENTITY.PxIdentity)); transform := PhysX.PxTransform_new(cast(s32)PhysX.PxIDENTITY.PxIdentity);
dynamic := PhysX.PxCreateDynamic(physics, *t, geometry, material, 10.0, *transform);
PhysX.PxRigidBody_setAngularDamping(dynamic, 0.5); PhysX.PxRigidBody_setAngularDamping(dynamic, 0.5);
PhysX.PxRigidDynamic_setLinearVelocity(dynamic, *velocity, true); PhysX.PxRigidDynamic_setLinearVelocity(dynamic, *velocity, true);
PhysX.PxScene_addActor(scene, dynamic, null); PhysX.PxScene_addActor(scene, dynamic, null);