Skip to content

Commit

Permalink
Tutorials update for HARFANG v3.1.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofra committed Dec 31, 2021
1 parent 446d854 commit f7e0383
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:

Expand Down
35 changes: 26 additions & 9 deletions physics_impulse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion physics_kapla.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
2 changes: 1 addition & 1 deletion physics_kapla.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
1 change: 1 addition & 0 deletions scene_aaa.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions scene_aaa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f7e0383

Please sign in to comment.