From f7e0383457f3f7cdef185dbeb4b90647ce65fcd1 Mon Sep 17 00:00:00 2001 From: Astrofra Date: Fri, 31 Dec 2021 14:18:14 +0100 Subject: [PATCH] Tutorials update for HARFANG v3.1.1. --- README.md | 4 ++-- physics_impulse.lua | 35 ++++++++++++++++++++++++++--------- physics_kapla.lua | 2 +- physics_kapla.py | 2 +- scene_aaa.lua | 1 + scene_aaa.py | 1 + 6 files changed, 32 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 3fac490..b087596 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# HARFANG® 3.1.0 Tutorials +# HARFANG® Tutorials -These **tutorials** demonstrate the usage of the HARFANG 3.1.0 API in **Python** and **Lua**. +These **tutorials** demonstrate the usage of the HARFANG API in **Python** and **Lua**. To run the tutorials: diff --git a/physics_impulse.lua b/physics_impulse.lua index 7ff062e..69ef2b0 100644 --- a/physics_impulse.lua +++ b/physics_impulse.lua @@ -6,7 +6,7 @@ hg.InputInit() hg.WindowSystemInit() res_x, res_y = 1280, 720 -win = hg.RenderInit('Harfang - Physics Impulse', res_x, res_y, hg.RF_VSync | hg.RF_MSAA4X) +win = hg.RenderInit('Harfang - Physics Force/Impulse (Press space to alternate)', res_x, res_y, hg.RF_VSync | hg.RF_MSAA4X) pipeline = hg.CreateForwardPipeline() res = hg.PipelineResources() @@ -45,22 +45,39 @@ physics_step = hg.time_from_sec_f(1 / 60) -- main loop keyboard = hg.Keyboard() +use_force = true + while not keyboard:Down(hg.K_Escape) do keyboard:Update() dt = hg.TickClock() - if keyboard:Pressed(hg.K_I) then - physics:NodeWake(cube_node) - world_pos = hg.GetT(cube_node:GetTransform():GetWorld()) - physics:NodeAddImpulse(cube_node, hg.Vec3(0, 8, 0), world_pos) + if keyboard:Pressed(hg.K_Space) then + use_force = not use_force end - if keyboard:Pressed(hg.K_F) then - physics:NodeWake(cube_node) - world_pos = hg.GetT(cube_node:GetTransform():GetWorld()) - physics:NodeAddForce(cube_node, hg.Vec3(0, 8, 0), world_pos) + + world_pos = hg.GetT(cube_node:GetTransform():GetWorld()) + dist_to_ground = world_pos.y - 0.5 + + if dist_to_ground < 1.0 then + k = -(dist_to_ground - 1.0) + + if use_force then + F = hg.Vec3(0, 1, 0) * k * 80 -- apply a force inversely proportional to the distance to the ground + physics:NodeAddForce(cube_node, F, world_pos) + else + stiffness = 10 + + cur_velocity = physics:NodeGetLinearVelocity(cube_node) + tgt_velocity = hg.Vec3(0, 1, 0) * k * stiffness -- compute a velocity that brings us to 1 meter above the ground + + I = tgt_velocity - cur_velocity -- an impulse is an instantaneous change in velocity + physics:NodeAddImpulse(cube_node, I, world_pos) + end end + physics:NodeWake(cube_node) + hg.SceneUpdateSystems(scene, clocks, dt, physics, physics_step, 3) hg.SubmitSceneToPipeline(0, scene, hg.IntRect(0, 0, res_x, res_y), true, pipeline, res) diff --git a/physics_kapla.lua b/physics_kapla.lua index faf20ec..c7e1de2 100644 --- a/physics_kapla.lua +++ b/physics_kapla.lua @@ -6,7 +6,7 @@ hg.InputInit() hg.WindowSystemInit() res_x, res_y = 1280, 720 -win = hg.RenderInit('Harfang - Kapla', res_x, res_y, hg.RF_VSync | hg.RF_MSAA4X) +win = hg.RenderInit('Harfang - Kapla - Press SPACEBAR', res_x, res_y, hg.RF_VSync | hg.RF_MSAA4X) hg.AddAssetsFolder('resources_compiled') diff --git a/physics_kapla.py b/physics_kapla.py index a59e6ce..2d22cb9 100644 --- a/physics_kapla.py +++ b/physics_kapla.py @@ -7,7 +7,7 @@ hg.WindowSystemInit() res_x, res_y = 1280, 720 -win = hg.RenderInit('Harfang - Kapla', res_x, res_y, hg.RF_VSync | hg.RF_MSAA4X) +win = hg.RenderInit('Harfang - Kapla - Press SPACEBAR', res_x, res_y, hg.RF_VSync | hg.RF_MSAA4X) hg.AddAssetsFolder('resources_compiled') diff --git a/scene_aaa.lua b/scene_aaa.lua index 7bd10c5..93c8745 100644 --- a/scene_aaa.lua +++ b/scene_aaa.lua @@ -23,6 +23,7 @@ hg.LoadSceneFromAssets("car_engine/engine.scn", scene, res, hg.GetForwardPipelin -- AAA pipeline pipeline_aaa_config = hg.ForwardPipelineAAAConfig() pipeline_aaa = hg.CreateForwardPipelineAAAFromAssets("core", pipeline_aaa_config, hg.BR_Equal, hg.BR_Equal) +pipeline_aaa_config.sample_count = 1 -- main loop frame = 0 diff --git a/scene_aaa.py b/scene_aaa.py index a45467a..192713f 100644 --- a/scene_aaa.py +++ b/scene_aaa.py @@ -22,6 +22,7 @@ # AAA pipeline pipeline_aaa_config = hg.ForwardPipelineAAAConfig() pipeline_aaa = hg.CreateForwardPipelineAAAFromAssets("core", pipeline_aaa_config, hg.BR_Equal, hg.BR_Equal) +pipeline_aaa_config.sample_count = 1 # main loop frame = 0