summaryrefslogtreecommitdiff
path: root/scripts/capsuleMove.lua
blob: cfc4c851bc77e672df64c02bf6e2321119ddf428 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'honey.std'

return function(entities, id, dt)
	local self = honey.ecs.Accessor(entities, id)
	local pressed = function(key)
		return glfw.GetKey(window.win, key) == glfw.PRESS
	end

	local float = self.node._child.spring.spring.F
	ode.BodyAddForce(self.physics._body, 0, float, 0)
	self.node._child.spring.spring.F = 0

	local yaw = math.rad(
		self.node._child.p
			.node._child.pivot
				.pitchyaw.yaw)
	local forward = Vec3{-math.sin(yaw), 0, -math.cos(yaw)}
	local left = Vec3{-math.cos(yaw), 0, math.sin(yaw)}

	local vel = Vec3{0,0,0}

	if pressed(glfw.KEY_W) then
		vel = vel + forward
	end
	if pressed(glfw.KEY_A) then
		vel = vel + left
	end
	if pressed(glfw.KEY_S) then
		vel = vel - forward
	end
	if pressed(glfw.KEY_D) then
		vel = vel - left
	end

	vel = 2000 * vel:normalize()
		
	x, y, z = ode.BodyGetLinearVel(self.physics._body)
	if Vec3{x,y,z}:norm2() < 500 then
		ode.BodyAddForce(self.physics._body, vel[1], vel[2], vel[3])
	end

	-- slow down
	local horizontalDamping = 100
	local verticalDamping = 5
	ode.BodyAddForce(self.physics._body, 
		-horizontalDamping*x, 
		-verticalDamping*y,
		-horizontalDamping*z)

	if y < 0.3 and float > 0 then
		ode.BodySetLinearVel(self.physics._body, x, 0, z)
	end
end