-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vehicle.lua
71 lines (61 loc) · 1.76 KB
/
Vehicle.lua
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
local m = {}
function m:init()
vehicle = self.pool:queue{
id = 'cc0fe2b7-70bf-4a53-b25d-1a42c8ec2bf9',
name = 'S-1',
vehicle_type = 'scooter',
initial_position = {0, 5, 3},
}
end
function m:addToGroup(group_name, e)
if group_name == 'vehicle' then
local x, y, z = unpack(e.initial_position or {})
local w, h, d
if e.vehicle_type == 'scooter' then
w, h, d = 1.5, 1.4, 2
end
e.collider = self.pool.data.world:newBoxCollider(x, y, z, w, h, d)
e.collider:setFriction(1)
e.collider:setMass(10)
end
end
function m:update(dt)
for i, e in ipairs(self.pool.groups.vehicle.entities) do
local force = 3000
local torque = 600
if lovr.system.isKeyDown('lshift') then
force = force * 3
end
if lovr.system.isKeyDown('w') then
local v = quat(e.collider:getOrientation()):direction()
v:mul(dt * force)
e.collider:applyForce(v)
end
if lovr.system.isKeyDown('s') then
local v = quat(e.collider:getOrientation()):direction()
v:mul(dt * -force)
e.collider:applyForce(v)
end
if lovr.system.isKeyDown('a') then
local v = quat(e.collider:getOrientation()):mul(vec3(0, dt * torque, 0))
e.collider:applyTorque(v)
end
if lovr.system.isKeyDown('d') then
local v = quat(e.collider:getOrientation()):mul(vec3(0, dt * -torque, 0))
e.collider:applyTorque(v)
end
if lovr.system.isKeyDown('space') then
e.collider:setPose(0, 5, 3)
end
end
end
function m:draw()
local pose = mat4()
for i, e in ipairs(self.pool.groups.vehicle.entities) do
pose:set(e.collider:getPose()):scale(0.5, 0.5, 1.2)
lovr.graphics.setLineWidth(15)
lovr.graphics.setColor(0.4, 0.4, 0.4, 0.1)
lovr.graphics.box('line', pose)
end
end
return m