Physics optional

This commit is contained in:
2025-07-12 17:26:23 +02:00
parent 9dd12a55bf
commit 602dd870df
5 changed files with 66 additions and 23 deletions

View File

@@ -78,18 +78,19 @@ Entity :: struct {
renderable: Renderable; renderable: Renderable;
animator: Animator; @DontSerialize animator: Animator; @DontSerialize
// Physics #if PHYSICS {
physx_handle: PhysX_Handle; // Physics
velocity: Vector3; physx_handle: PhysX_Handle;
velocity: Vector3;
// End physics
}
// End physics #if NETWORKING {
remote_id: Entity_Id; @DontSerialize
#if NETWORKING { client_id: Client_Id; @DontSerialize
remote_id: Entity_Id; @DontSerialize is_proxy: bool; @DontSerialize
client_id: Client_Id; @DontSerialize last_replication_time: float; @DontSerialize
is_proxy: bool; @DontSerialize }
last_replication_time: float; @DontSerialize
}
_locator: Bucket_Locator; @DontSerialize _locator: Bucket_Locator; @DontSerialize
scene: *Scene; @DontSerialize scene: *Scene; @DontSerialize

View File

@@ -40,4 +40,4 @@ find_all_mesh_entities :: () {
path := "../assets/models/level_design/"; path := "../assets/models/level_design/";
visit_files(path, true, *mesh_entity_files, mesh_entity_visitor); visit_files(path, true, *mesh_entity_files, mesh_entity_visitor);
} }

View File

@@ -22,7 +22,9 @@ Scene :: struct {
mode: Engine_Mode; mode: Engine_Mode;
physx_scene: PhysX_Scene; #if PHYSICS {
physx_scene: PhysX_Scene;
}
using custom_fields: _Custom_Scene_Fields; using custom_fields: _Custom_Scene_Fields;
} }

View File

@@ -1,4 +1,4 @@
#module_parameters(WITH_EDITOR := true, WITH_NETWORKING := false, action_type : Type, entity_fields: Type, scene_fields: Type); #module_parameters(WITH_EDITOR := true, WITH_NETWORKING := false, WITH_PHYSICS := false, action_type : Type, entity_fields: Type, scene_fields: Type);
// TODO: Add a fallback, if none we're specified // TODO: Add a fallback, if none we're specified
_Custom_Entity_Fields :: entity_fields; _Custom_Entity_Fields :: entity_fields;
@@ -8,6 +8,7 @@ Action :: action_type;
EDITOR :: WITH_EDITOR; EDITOR :: WITH_EDITOR;
DEBUG :: true; DEBUG :: true;
NETWORKING :: WITH_NETWORKING; NETWORKING :: WITH_NETWORKING;
PHYSICS :: WITH_PHYSICS;
#if EDITOR { #if EDITOR {
//#load "../editor/scene_editor.jai"; //#load "../editor/scene_editor.jai";
@@ -83,7 +84,9 @@ coven_init :: (window_title: string, window_width: u32, window_height: u32, full
init_audio_system(); init_audio_system();
init_console(); init_console();
init_physx(); #if PHYSICS {
init_physx();
}
#if EDITOR { #if EDITOR {
init_editor(); init_editor();
@@ -158,11 +161,14 @@ coven_run :: (game_update_proc: (float), game_editor_update_proc: (float), game_
if engine.current_scene != null && !engine.paused { if engine.current_scene != null && !engine.paused {
update_animators(clamped_dt); update_animators(clamped_dt);
pre_physx_sync(engine.current_scene); #if PHYSICS {
tick_physx(*engine.current_scene.physx_scene, clamped_dt); pre_physx_sync(engine.current_scene);
post_physx_sync(engine.current_scene); tick_physx(*engine.current_scene.physx_scene, clamped_dt);
post_physx_sync(engine.current_scene);
game_update_post_physics_proc(clamped_dt); // TODO: Move this out into engine.procs
game_update_post_physics_proc(clamped_dt);
}
} }
} else { } else {
game_editor_update_proc(clamped_dt); game_editor_update_proc(clamped_dt);
@@ -214,6 +220,10 @@ switch_engine_mode :: (to_mode: Engine_Mode) {
} }
} }
#if PHYSICS {
#load "physics/physx.jai";
}
#if NETWORKING { #if NETWORKING {
#load "networking/networking.jai"; #load "networking/networking.jai";
} }
@@ -238,7 +248,6 @@ switch_engine_mode :: (to_mode: Engine_Mode) {
#load "core/console.jai"; #load "core/console.jai";
#load "audio/audio.jai"; #load "audio/audio.jai";
#load "core/fps.jai"; #load "core/fps.jai";
#load "physics/physx.jai";
#scope_export #scope_export

View File

@@ -1,4 +1,4 @@
PHYSX_TEST :: false; PHYSX_DEFAULT_SHAPE_FLAGS :: cast(u8)(PhysX.PxShapeFlags.Visualization | PhysX.PxShapeFlags.SceneQueryShape | PhysX.PxShapeFlags.SimulationShape); PHYSX_DEFAULT_SHAPE_FLAGS :: cast(u8)(PhysX.PxShapeFlags.Visualization | PhysX.PxShapeFlags.SceneQueryShape | PhysX.PxShapeFlags.SimulationShape);
PhysX_Handle :: #type, distinct u32; PhysX_Handle :: #type, distinct u32;
@@ -116,6 +116,38 @@ post_physx_sync :: (game_scene: *Scene) {
} }
} }
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);
material := PhysX.PxPhysics_createMaterial(physics, 0.1, 0.1, 0.5);
//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);
PhysX.PxBase_release(material);
physics_actor : PhysX_Actor;
physics_actor.type = .DYNAMIC;
physics_actor.sync_rotation_from_physx = true;
physics_actor.dynamic = actor;
entity.physx_handle = parray_add(*entity.scene.physx_scene.actors, physics_actor);
return entity.physx_handle;
}
add_physx_capsule :: (entity: *Entity, half_height: float, radius: float) -> PhysX_Handle { add_physx_capsule :: (entity: *Entity, half_height: float, radius: float) -> PhysX_Handle {
geo := PhysX.PxCapsuleGeometry_new(radius, half_height-radius); geo := PhysX.PxCapsuleGeometry_new(radius, half_height-radius);
@@ -145,8 +177,7 @@ add_physx_capsule :: (entity: *Entity, half_height: float, radius: float) -> Phy
PhysX.PxScene_addActor(entity.scene.physx_scene.scene, actor, null); PhysX.PxScene_addActor(entity.scene.physx_scene.scene, actor, null);
PhysX.PxShape_release(shape); PhysX.PxShape_release(shape);
// @Incomplete PhysX.PxBase_release(material);
//PhysX.PxMaterial_release(material);
physics_actor : PhysX_Actor; physics_actor : PhysX_Actor;
physics_actor.type = .DYNAMIC; physics_actor.type = .DYNAMIC;