PhysX work
This commit is contained in:
@@ -1,3 +1,28 @@
|
||||
PHYSX_TEST :: false; PHYSX_DEFAULT_SHAPE_FLAGS :: cast(u8)(PhysX.PxShapeFlags.Visualization | PhysX.PxShapeFlags.SceneQueryShape | PhysX.PxShapeFlags.SimulationShape);
|
||||
|
||||
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();
|
||||
@@ -14,21 +39,19 @@ init_physx :: () {
|
||||
|
||||
tolerance_scale : PhysX.PxTolerancesScale;
|
||||
tolerance_scale.length = 1;
|
||||
tolerance_scale.speed = 981;
|
||||
tolerance_scale.speed = 10;
|
||||
physics = PhysX.PxCreatePhysics(PhysX.PX_PHYSICS_VERSION, foundation, *tolerance_scale, true, pvd, null);
|
||||
|
||||
dispatcher = PhysX.PxDefaultCpuDispatcherCreate(2);
|
||||
material = PhysX.PxPhysics_createMaterial(physics, 0.5, 0.5, 0.6);
|
||||
material = PhysX.PxPhysics_createMaterial(physics, 0.0, 0.0, 0.6);
|
||||
}
|
||||
|
||||
tick_physx :: (scene: *PhysX.PxScene, dt: float) {
|
||||
PhysX.PxScene_simulate(scene, 1.0/60.0, null, null, 0, true);
|
||||
PhysX.PxScene_fetchResults(scene, true, null);
|
||||
tick_physx :: (scene: *PhysX_Scene, dt: float) {
|
||||
PhysX.PxScene_simulate(scene.scene, dt, null, null, 0, true);
|
||||
PhysX.PxScene_fetchResults(scene.scene, true, null);
|
||||
}
|
||||
|
||||
plane : Plane3;
|
||||
|
||||
create_physx_scene :: () -> *PhysX.PxScene {
|
||||
init_physx_scene :: (game_scene: *Scene) {
|
||||
tolerance_scale : PhysX.PxTolerancesScale;
|
||||
tolerance_scale.length = 1;
|
||||
tolerance_scale.speed = 10;
|
||||
@@ -48,41 +71,113 @@ create_physx_scene :: () -> *PhysX.PxScene {
|
||||
PhysX.PxPvdSceneClient_setScenePvdFlag(pvd_client, xx PhysX.PxPvdSceneFlag.TRANSMIT_CONTACTS, true);
|
||||
PhysX.PxPvdSceneClient_setScenePvdFlag(pvd_client, xx PhysX.PxPvdSceneFlag.TRANSMIT_SCENEQUERIES, true);
|
||||
}
|
||||
{
|
||||
plane_point := Vector3.{0,0,0};
|
||||
plane_normal := Vector3.{0,1,0};
|
||||
plane = PhysX.PxPlane_new(*plane_point, *plane_normal);
|
||||
ground_plane := PhysX.PxCreatePlane(physics, *plane, material);
|
||||
PhysX.PxScene_addActor(scene, ground_plane, null);
|
||||
}
|
||||
|
||||
stack_z := 0.0;
|
||||
for i: 0..4 {
|
||||
stack_pos := Vector3.{0,10,stack_z};
|
||||
stack_z -= 10.0;
|
||||
create_stack(scene, PhysX.PxTransform_new(*stack_pos), 10, 2.0);
|
||||
}
|
||||
physx_scene : PhysX_Scene;
|
||||
physx_scene.scene = scene;
|
||||
physx_scene.actors.data.allocator = game_scene.allocator;
|
||||
physx_scene.actors.indices.allocator = game_scene.allocator;
|
||||
|
||||
{
|
||||
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;
|
||||
game_scene.physx_scene = physx_scene;
|
||||
}
|
||||
PxShapeFlags :: enum_flags u8 {
|
||||
SimulationShape :: 1 << 0;
|
||||
SceneQueryShape :: 1 << 1;
|
||||
TriggerShape :: 1 << 2;
|
||||
Visualization :: 1 << 3;
|
||||
|
||||
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.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
add_physx_capsule :: (entity: *Entity, half_height: float, radius: float) -> PhysX_Handle {
|
||||
geo := PhysX.PxCapsuleGeometry_new(radius, half_height-radius);
|
||||
|
||||
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(*entity.transform.position, *rotation);
|
||||
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);
|
||||
|
||||
material := PhysX.PxPhysics_createMaterial(physics, 0.0, 0.0, 0.0);
|
||||
|
||||
PhysX.PxMaterial_setRestitutionCombineMode(material,1); // Turn off restitution no matter the other material
|
||||
|
||||
shape := PhysX.PxPhysics_createShape(physics, *geo, material, false, PHYSX_DEFAULT_SHAPE_FLAGS);
|
||||
PhysX.PxRigidActor_attachShape(actor, shape);
|
||||
|
||||
|
||||
PhysX.PxRigidBodyExt_updateMassAndInertia(actor, 0.1, null, false);
|
||||
PhysX.PxScene_addActor(entity.scene.physx_scene.scene, actor, null);
|
||||
|
||||
PhysX.PxShape_release(shape);
|
||||
// @Incomplete
|
||||
//PhysX.PxMaterial_release(material);
|
||||
|
||||
physics_actor : PhysX_Actor;
|
||||
physics_actor.type = .DYNAMIC;
|
||||
physics_actor.sync_rotation_from_physx = false;
|
||||
physics_actor.dynamic = actor;
|
||||
|
||||
entity.physx_handle = parray_add(*entity.scene.physx_scene.actors, physics_actor);
|
||||
return entity.physx_handle;
|
||||
}
|
||||
|
||||
add_physx_box :: (entity: *Entity, half_extent: Vector3) -> PhysX_Handle {
|
||||
shape := PhysX.PxPhysics_createShape(physics, PhysX.PxBoxGeometry_new(half_extent), material, false, PHYSX_DEFAULT_SHAPE_FLAGS);
|
||||
t := PhysX.PxTransform_new(*entity.transform.position);
|
||||
|
||||
body := PhysX.PxPhysics_createRigidStatic(physics, *t);
|
||||
PhysX.PxRigidActor_attachShape(body, shape);
|
||||
PhysX.PxScene_addActor(entity.scene.physx_scene.scene, body, null);
|
||||
|
||||
PhysX.PxShape_release(shape);
|
||||
|
||||
physics_actor : PhysX_Actor;
|
||||
physics_actor.type = .STATIC;
|
||||
physics_actor.static = body;
|
||||
|
||||
entity.physx_handle = parray_add(*entity.scene.physx_scene.actors, physics_actor);
|
||||
return entity.physx_handle;
|
||||
|
||||
}
|
||||
|
||||
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, xx (PhysX.PxShapeFlags.Visualization | PhysX.PxShapeFlags.SceneQueryShape | PhysX.PxShapeFlags.SimulationShape));
|
||||
shape := PhysX.PxPhysics_createShape(physics, PhysX.PxBoxGeometry_new(half_extent, half_extent, half_extent), material, false, PHYSX_DEFAULT_SHAPE_FLAGS);
|
||||
for i: 0..size-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;
|
||||
@@ -97,6 +192,16 @@ create_stack :: (scene: *PhysX.PxScene, t: PhysX.PxTransform, size: u32, half_ex
|
||||
//shape->release();
|
||||
}
|
||||
|
||||
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, .{};
|
||||
}
|
||||
|
||||
create_dynamic :: (scene: *PhysX.PxScene, t: PhysX.PxTransform, geometry: *PhysX.PxGeometry, velocity: Vector3 = .{}) -> *PhysX.PxRigidDynamic {
|
||||
p := Vector3.{0,0,0};
|
||||
|
||||
Reference in New Issue
Block a user