function LocalPlayer:constructor()
self:init()
self:set_speed(10)
- self:set_fov(45)
- self:set_yaw(180)
+ self:set_fov(86.1)
+ self:set_yaw(-90)
self:set_pitch(0)
self:add_event_listener("after_set_position", function(event) self:set_position_callback(event) end)
RenderEngine:add_event_listener("keypress", function(event) self:key_press_callback(event) end)
function LocalPlayer:key_press_callback(event)
local keys = event.keys
local speed = self.speed * event.dtime
- local hvec, vvec = self.horizontal_look, RenderEngine.camera.up
+ local camera = RenderEngine.camera
+ local front, right, up = camera.front, camera.right, camera.up
+ if not RenderEngine.pitch_move then
+ front = glm.vec3(front.x, 0, front.z):normalize()
+ up = glm.vec3(0, up.y, 0):normalize()
+ end
if keys["w"] then
- self:move( speed * hvec)
+ self:move( speed * front)
elseif keys["s"] then
- self:move(-speed * hvec)
+ self:move(-speed * front)
end
- if keys["a"] then
- self:move(-speed * (hvec % vvec):normalize())
- elseif keys["d"] then
- self:move( speed * (hvec % vvec):normalize())
+ if keys["d"] then
+ self:move( speed * right)
+ elseif keys["a"] then
+ self:move(-speed * right)
end
- if keys["left shift"] then
- self:move(-speed * vvec)
- elseif keys["space"] then
- self:move( speed * vvec)
+ if keys["space"] then
+ self:move( speed * up)
+ elseif keys["left shift"] then
+ self:move(-speed * up)
end
end
function LocalPlayer:mouse_move_callback(event)
- self.yaw = self.yaw - event.x
+ self.yaw = self.yaw + event.x
self.pitch = self.pitch - event.y
- self:update_look()
+ self:update_camera()
end
function LocalPlayer:set_position_callback(event)
end
function LocalPlayer:set_yaw(yaw)
- self.yaw = math.rad(yaw)
- self:update_look()
+ self.yaw = yaw
+ self:update_camera()
end
function LocalPlayer:set_pitch(pitch)
- self.pitch = math.rad(pitch)
- self:update_look()
+ self.pitch = pitch
+ self:update_camera()
end
-function LocalPlayer:update_look()
- self.horizontal_look = glm.vec3(math.sin(self.yaw), 0, math.cos(self.yaw)):normalize()
- RenderEngine.camera.front = glm.vec3(math.sin(self.yaw or 0), math.sin(self.pitch or 0), math.cos(self.yaw or 0)):normalize()
+function LocalPlayer:update_camera()
+ RenderEngine.camera:update(self.yaw or 0, self.pitch or 0)
end
return LocalPlayer
local camera = {
pos = glm.vec3(0, 0, 0),
- front = glm.vec3(1, 0, 0),
up = glm.vec3(0, 1, 0),
+ world_up = glm.vec3(0, 1, 0),
+ front = glm.vec3(0, 0, -1),
}
function camera:get_view_matrix()
return glm.look_at(self.pos, self.pos + self.front, self.up)
end
+function camera:update(yaw, pitch)
+ yaw, pitch = math.rad(yaw), math.rad(glm.clamp(pitch, -89.0, 89.0))
+ self.front = glm.vec3(math.cos(yaw)*math.cos(pitch), math.sin(pitch), math.sin(yaw)*math.cos(pitch)):normalize()
+ --self.front = glm.vec3(math.cos(yaw), math.sin(pitch), math.sin(yaw)):normalize()
+ self.right = (self.front % self.world_up):normalize()
+ self.up = (self.right % self.front):normalize()
+end
+
return camera