Externalize the freecam
This commit is contained in:
parent
1cf4895743
commit
6f082083a0
@ -1,25 +1,19 @@
|
||||
package.path = package.path .. ";../scripts/?.lua"
|
||||
local freecam = require("freecam")
|
||||
|
||||
local min = math.min
|
||||
local max = math.max
|
||||
local cube
|
||||
local physics_ball
|
||||
local character
|
||||
local ball
|
||||
local camera
|
||||
local die_cube
|
||||
|
||||
local total = 0.0
|
||||
|
||||
local vx = 0.0
|
||||
local vy = 0.0
|
||||
local vz = 0.0
|
||||
|
||||
local character_move_vec = couch.Vector3(0.0, 0.0, 0.0)
|
||||
|
||||
local ballvy = -1.0
|
||||
|
||||
local cam_rot_x = 0.0
|
||||
local cam_rot_y = 0.0
|
||||
|
||||
local SPEED = 30
|
||||
|
||||
local WHITE = couch.Vector3(1.0, 1.0, 1.0)
|
||||
@ -31,10 +25,9 @@ local light
|
||||
function init()
|
||||
local material
|
||||
local transform
|
||||
|
||||
camera = couch.Camera()
|
||||
camera:MakeCurrent()
|
||||
camera:Translate(couch.Vector3(0.0, 0.0, 10.0))
|
||||
|
||||
freecam.init_camera()
|
||||
freecam.camera:Translate(couch.Vector3(0.0, 0.0, 10.0))
|
||||
|
||||
local light = couch.DirectionalLight()
|
||||
light:SetDirection(couch.Vector3(0.0, -1.0, -1.0))
|
||||
@ -144,20 +137,8 @@ end
|
||||
|
||||
function update(delta)
|
||||
total = total + delta
|
||||
local move_vec = couch.Vector3()
|
||||
local camera_transform = camera:GetTransform()
|
||||
move_vec = camera_transform.position + camera_transform:Forward() * delta * vz * SPEED
|
||||
move_vec = move_vec + camera_transform:Right() * delta * vx * SPEED
|
||||
move_vec = move_vec + camera_transform:Up() * delta * vy * SPEED
|
||||
camera_transform.position = move_vec
|
||||
|
||||
camera_transform.rotation.y = camera_transform.rotation.y - cam_rot_x * delta
|
||||
cam_rot_x = 0.0
|
||||
camera_transform.rotation.x = camera_transform.rotation.x - cam_rot_y * delta
|
||||
camera_transform.rotation.x = min(max(camera_transform.rotation.x, -3.14 / 2.0), 3.14 / 2.0)
|
||||
cam_rot_y = 0.0
|
||||
|
||||
camera:SetTransform(camera_transform)
|
||||
freecam.update_camera(delta)
|
||||
|
||||
local loc = ball:GetTransform().position
|
||||
if loc.y > 4.0 then
|
||||
@ -211,9 +192,7 @@ function action_dir(key, action, pos, neg, curr)
|
||||
end
|
||||
|
||||
function onkey(key, code, action, mod)
|
||||
vz = action_dir(key, action, couch.KEY_W, couch.KEY_S, vz)
|
||||
vx = action_dir(key, action, couch.KEY_D, couch.KEY_A, vx)
|
||||
vy = action_dir(key, action, couch.KEY_SPACE, couch.KEY_LEFT_CONTROL, vy)
|
||||
freecam.onkey(key, code, action, mod)
|
||||
|
||||
if key == couch.KEY_J and action == couch.ACTION_PRESS then
|
||||
physics_ball:ApplyImpulse(couch.Vector3(0.0, 1.0, 0.0) * 10)
|
||||
@ -223,8 +202,7 @@ function onkey(key, code, action, mod)
|
||||
end
|
||||
|
||||
function onmousemotion(_, _, relx, rely)
|
||||
cam_rot_x = relx
|
||||
cam_rot_y = rely
|
||||
freecam.onmousemotion(relx, rely)
|
||||
end
|
||||
|
||||
function make_ground()
|
||||
|
12
demo/resources/inverted_cube.mtl
Normal file
12
demo/resources/inverted_cube.mtl
Normal file
@ -0,0 +1,12 @@
|
||||
# Blender MTL File: 'None'
|
||||
# Material Count: 1
|
||||
|
||||
newmtl Material
|
||||
Ns 323.999994
|
||||
Ka 1.000000 1.000000 1.000000
|
||||
Kd 0.800000 0.800000 0.800000
|
||||
Ks 0.500000 0.500000 0.500000
|
||||
Ke 0.000000 0.000000 0.000000
|
||||
Ni 1.450000
|
||||
d 1.000000
|
||||
illum 2
|
40
demo/resources/inverted_cube.obj
Normal file
40
demo/resources/inverted_cube.obj
Normal file
@ -0,0 +1,40 @@
|
||||
# Blender v2.92.0 OBJ File: ''
|
||||
# www.blender.org
|
||||
mtllib inverted_cube.mtl
|
||||
o Cube
|
||||
v 1.000000 1.000000 -1.000000
|
||||
v 1.000000 -1.000000 -1.000000
|
||||
v 1.000000 1.000000 1.000000
|
||||
v 1.000000 -1.000000 1.000000
|
||||
v -1.000000 1.000000 -1.000000
|
||||
v -1.000000 -1.000000 -1.000000
|
||||
v -1.000000 1.000000 1.000000
|
||||
v -1.000000 -1.000000 1.000000
|
||||
vt 0.625000 0.500000
|
||||
vt 0.625000 0.750000
|
||||
vt 0.875000 0.750000
|
||||
vt 0.875000 0.500000
|
||||
vt 0.375000 0.750000
|
||||
vt 0.375000 1.000000
|
||||
vt 0.625000 1.000000
|
||||
vt 0.375000 0.000000
|
||||
vt 0.375000 0.250000
|
||||
vt 0.625000 0.250000
|
||||
vt 0.625000 0.000000
|
||||
vt 0.125000 0.500000
|
||||
vt 0.125000 0.750000
|
||||
vt 0.375000 0.500000
|
||||
vn 0.0000 -1.0000 0.0000
|
||||
vn 0.0000 0.0000 -1.0000
|
||||
vn 1.0000 0.0000 0.0000
|
||||
vn 0.0000 1.0000 0.0000
|
||||
vn -1.0000 0.0000 0.0000
|
||||
vn 0.0000 0.0000 1.0000
|
||||
usemtl Material
|
||||
s off
|
||||
f 1/1/1 3/2/1 7/3/1 5/4/1
|
||||
f 4/5/2 8/6/2 7/7/2 3/2/2
|
||||
f 8/8/3 6/9/3 5/10/3 7/11/3
|
||||
f 6/12/4 8/13/4 4/5/4 2/14/4
|
||||
f 2/14/5 4/5/5 3/2/5 1/1/5
|
||||
f 6/9/6 2/14/6 1/1/6 5/10/6
|
62
demo/scripts/freecam.lua
Normal file
62
demo/scripts/freecam.lua
Normal file
@ -0,0 +1,62 @@
|
||||
local freecam = {}
|
||||
|
||||
local min = math.min
|
||||
local max = math.max
|
||||
|
||||
freecam.camera = nil
|
||||
freecam.speed = 30
|
||||
|
||||
local vx = 0.0
|
||||
local vy = 0.0
|
||||
local vz = 0.0
|
||||
|
||||
local cam_rot_x = 0.0
|
||||
local cam_rot_y = 0.0
|
||||
|
||||
function freecam.init_camera()
|
||||
freecam.camera = couch.Camera()
|
||||
freecam.camera:MakeCurrent()
|
||||
end
|
||||
|
||||
function freecam.update_camera(delta)
|
||||
local camera_transform = freecam.camera:GetTransform()
|
||||
|
||||
local move_vec = couch.Vector3()
|
||||
move_vec = camera_transform.position + camera_transform:Forward() * delta * vz * freecam.speed
|
||||
move_vec = move_vec + camera_transform:Right() * delta * vx * freecam.speed
|
||||
move_vec = move_vec + camera_transform:Up() * delta * vy * freecam.speed
|
||||
camera_transform.position = move_vec
|
||||
|
||||
camera_transform.rotation.y = camera_transform.rotation.y - cam_rot_x * delta
|
||||
cam_rot_x = 0.0
|
||||
camera_transform.rotation.x = camera_transform.rotation.x - cam_rot_y * delta
|
||||
camera_transform.rotation.x = min(max(camera_transform.rotation.x, -3.14 / 2.0), 3.13 / 2.0)
|
||||
cam_rot_y = 0.0
|
||||
|
||||
freecam.camera:SetTransform(camera_transform)
|
||||
end
|
||||
|
||||
function action_dir(key, action, pos, neg, curr)
|
||||
if key == pos and action == couch.ACTION_PRESS then
|
||||
return 1.0
|
||||
elseif key == neg and action == couch.ACTION_PRESS then
|
||||
return -1.0
|
||||
elseif (key == pos or key == neg) and action == couch.ACTION_RELEASE then
|
||||
return 0.0
|
||||
else
|
||||
return curr
|
||||
end
|
||||
end
|
||||
|
||||
function freecam.onkey(key, code, action, mod)
|
||||
vz = action_dir(key, action, couch.KEY_W, couch.KEY_S, vz)
|
||||
vx = action_dir(key, action, couch.KEY_D, couch.KEY_A, vx)
|
||||
vy = action_dir(key, action, couch.KEY_SPACE, couch.KEY_LEFT_CONTROL, vy)
|
||||
end
|
||||
|
||||
function freecam.onmousemotion(relx, rely)
|
||||
cam_rot_x = relx
|
||||
cam_rot_y = rely
|
||||
end
|
||||
|
||||
return freecam
|
Loading…
Reference in New Issue
Block a user