Files
coven/physics/physx.jai

384 lines
14 KiB
Plaintext

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;
PhysX_Actor_Type :: enum {
STATIC;
DYNAMIC;
}
PhysX_Actor :: struct {
type : PhysX_Actor_Type;
sync_rotation_from_physx: bool = true;
union {
static: *PhysX.PxRigidStatic;
dynamic: *PhysX.PxRigidDynamic;
}
}
PhysX_Scene :: struct {
scene: *PhysX.PxScene;
actors : PArray(PhysX_Actor, PhysX_Handle);
}
init_physx :: () {
default_allocator = PhysX.get_default_allocator();
default_error_callback = PhysX.get_default_error_callback();
foundation := PhysX.PxCreateFoundation(PhysX.PX_PHYSICS_VERSION, *default_allocator, *default_error_callback);
if foundation == null {
log_error("Could not initialize PhysX\n");
return;
}
pvd := PhysX.PxCreatePvd(foundation);
transport := PhysX.PxDefaultPvdSocketTransportCreate("127.0.0.1", 5425, 10);
PhysX.PxPvd_connect(pvd, transport, cast(u8)PhysX.PxPvdInstrumentationFlags.ALL);
tolerance_scale : PhysX.PxTolerancesScale;
tolerance_scale.length = 1;
tolerance_scale.speed = 10;
physics = PhysX.PxCreatePhysics(PhysX.PX_PHYSICS_VERSION, foundation, *tolerance_scale, true, pvd, null);
cooking_params = PhysX.PxCookingParams_new(*tolerance_scale);
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) {
PhysX.PxScene_simulate(scene.scene, dt, null, null, 0, true);
PhysX.PxScene_fetchResults(scene.scene, true, null);
}
//custom_filter_shader :: (attributes0: u32, filterData0: PhysX.PxFilterData, attributes1: u32, filterData1: PhysX.PxFilterData, pairFlags: *PhysX.PxPairFlags, constantBlock: *void, constantBlockSize: u32) -> PhysX.PxFilterFlags #c_call {
// pairFlags.* = PhysX.PxPairFlags.ContactDefault;
// return PhysX.PxFilterFlags.Default;
//}
custom_filter_shader :: (attributes0: *u32, filterData0: *PhysX.PxFilterData, attributes1: *u32, filterData1: *PhysX.PxFilterData, pairFlags: *PhysX.PxPairFlags) -> u16 #c_call {
pairFlags.* = PhysX.PxPairFlags.ContactDefault;
return xx PhysX.PxFilterFlags.Default;
}
init_physx_scene :: (game_scene: *Scene) {
tolerance_scale : PhysX.PxTolerancesScale;
tolerance_scale.length = 1;
tolerance_scale.speed = 10;
scene_desc := PhysX.PxSceneDesc_new(*tolerance_scale);
scene_desc.gravity.y = -9.81;
scene_desc.cpuDispatcher = xx dispatcher;
scene_desc.simulationEventCallback = event_callback;
PhysX.set_custom_filter_shader(*scene_desc, PhysX.create_custom_filter_shader(custom_filter_shader));
//scene_desc.filterShader = custom_filter_shader;//
//scene_desc.filterShaderData = null;
//scene_desc.filterShaderDataSize = 0;
scene := PhysX.PxPhysics_createScene(physics, *scene_desc);
// @Incomplete: If debug
pvd_client := PhysX.PxScene_getScenePvdClient(scene);
if pvd_client {
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_SCENEQUERIES, true);
}
physx_scene : PhysX_Scene;
physx_scene.scene = scene;
physx_scene.actors.data.allocator = game_scene.allocator;
physx_scene.actors.indices.allocator = game_scene.allocator;
game_scene.physx_scene = physx_scene;
}
deinit_physx_scene :: (game_scene: *Scene) {
PhysX.PxScene_release(game_scene.physx_scene.scene);
}
pre_physx_sync :: (game_scene: *Scene) {
for game_scene.entities {
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);
}
}
}
}
post_physx_sync :: (game_scene: *Scene) {
for game_scene.entities {
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);
if physx_actor.sync_rotation_from_physx || it.physics.type == .SPHERE {
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;
position := e.transform.position + e.physics.offset;
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(*position, *rotation);
} else {
transform = PhysX.PxTransform_new(*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);
}
case .CONVEX_MESH; {
if e.flags & .RENDERABLE {
points : [..] Vector3;
points.allocator = temp;
indices : [..] u32;
indices.allocator = temp;
model := get_model_by_handle(e.renderable.model);
for node, node_index: model.nodes {
render_data := e.renderable.nodes[node_index];
success, inv_matrix := inverse(e.transform.model_matrix);
// We need to undo the local to world part of every world matrix
matrix := inv_matrix * render_data.transform.world_matrix;
if node.meshes.count > 0 {
for m, mi: node.meshes {
index_start : u32 = xx indices.count;
mesh := parray_get(*engine.renderer.meshes, m);
for v: mesh.positions {
array_add(*points, v);//transform_position(v, matrix));
}
for i: mesh.indices {
array_add(*indices, index_start + i);
}
}
}
}
mesh_desc := PhysX.PxConvexMeshDesc_new();
mesh_desc.points.count = xx points.count;
mesh_desc.points.stride = size_of(Vector3);
mesh_desc.points.data = points.data;
mesh_desc.polygons.count = cast(u32)(indices.count / 3);
mesh_desc.polygons.stride = 3 * size_of(u32);
mesh_desc.polygons.data = indices.data;
if !PhysX.PxValidateConvexMesh(*cooking_params, *mesh_desc) {
assert(false);
}
stream : PhysX.PxOutputStream;
callback := PhysX.PxGetStandaloneInsertionCallback();
//read_buffer : PhysX.PxDefaultMemoryInputData_new(;
cond : s32;
mesh := PhysX.PxCreateConvexMesh(*cooking_params, *mesh_desc, callback, null);
scale := PhysX.PxMeshScale_new(*e.transform.scale);
geo = PhysX.PxConvexMeshGeometry_new(mesh, *scale, 0);
}
}
case .TRIANGLE_MESH; {
if e.flags & .RENDERABLE {
points : [..] Vector3;
points.allocator = temp;
indices : [..] u32;
indices.allocator = temp;
model := get_model_by_handle(e.renderable.model);
for node, node_index: model.nodes {
render_data := e.renderable.nodes[node_index];
success, inv_matrix := inverse(e.transform.model_matrix);
// We need to undo the local to world part of every world matrix
matrix := inv_matrix * render_data.transform.world_matrix;
if node.meshes.count > 0 {
print("NUM MESHES %\n", node.meshes.count);
for m, mi: node.meshes {
index_start : u32 = xx indices.count;
mesh := parray_get(*engine.renderer.meshes, m);
for v: mesh.positions {
array_add(*points, v);//transform_position(v, matrix));
}
for i: mesh.indices {
array_add(*indices, index_start + i);
}
}
}
}
mesh_desc : PhysX.PxTriangleMeshDesc;
mesh_desc.points.count = xx points.count;
mesh_desc.points.stride = size_of(Vector3);
mesh_desc.points.data = points.data;
mesh_desc.triangles.count = cast(u32)(indices.count / 3);
mesh_desc.triangles.stride = 3 * size_of(u32);
mesh_desc.triangles.data = indices.data;
//if !PhysX.PxValidateTriangleMesh(*cooking_params, *mesh_desc) {
// assert(false);
//}
callback := PhysX.PxGetStandaloneInsertionCallback();
mesh := PhysX.PxCreateTriangleMesh(*cooking_params, *mesh_desc, callback, null);
scale := PhysX.PxMeshScale_new(*e.transform.scale);
geo = PhysX.PxTriangleMeshGeometry_new(mesh, *scale, 0);
}
}
}
shape := PhysX.PxPhysics_createShape(physics, geo, material, false, ifx e.physics.trigger then PHYSX_DEFAULT_TRIGGER_SHAPE_FLAGS else PHYSX_DEFAULT_SIMULATION_SHAPE_FLAGS);
// Setup layers
filter_data := PhysX.PxFilterData_new();
filter_data.word0 = 1;
filter_data.word1 = 1;
filter_data.word2 = 1;
filter_data.word3 = 1;
//PhysX.PxShape_setSimulationFilterData(shape, *filter_data);
//PhysX.PxShape_setQueryFilterData(shape, *filter_data);
PhysX.PxRigidActor_attachShape(actor, shape);
if e.physics.dynamic {
PhysX.PxRigidBodyExt_updateMassAndInertia(cast(*PhysX.PxRigidBody)actor, 1000.0, null, false);
}
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;
}
Hit :: struct {
}
physx_raycast :: (origin: Vector3, direction: Vector3, max_distance: float = 1000.0) -> bool, Hit {
hit : PhysX.PxRaycastHit;
filter_data := PhysX.PxQueryFilterData_new();
has_hit := PhysX.PxSceneQueryExt_raycastSingle(engine.current_scene.physx_scene.scene, *origin, *direction, max_distance, 0, *hit, *filter_data, null, null);
return has_hit, .{};
}
PhysX :: #import "PhysX";
#scope_file
physics : *PhysX.PxPhysics;
cooking_params: PhysX.PxCookingParams;
material : *PhysX.PxMaterial;
default_allocator : PhysX.PxAllocatorCallback;
default_error_callback : PhysX.PxErrorCallback;
default_filter_shader : PhysX.SimulationFilterShader;
dispatcher : *PhysX.PxDefaultCpuDispatcher;
event_callback : *PhysX.PxSimulationEventCallback;