diff --git a/utils/fpscontrols/fpscontrols.go b/utils/fpscontrols/fpscontrols.go index 04783b6..2b31768 100644 --- a/utils/fpscontrols/fpscontrols.go +++ b/utils/fpscontrols/fpscontrols.go @@ -14,12 +14,12 @@ type FPSControls struct { position mgl32.Vec3 rotation mgl32.Vec2 - currentMove mgl32.Vec3 - currentLook mgl32.Vec2 lastMousePos mgl32.Vec2 view mgl32.Mat4 + window *glfw.Window + lastTime float64 } @@ -28,19 +28,54 @@ var instance *FPSControls func (c *FPSControls) Update() { time := glfw.GetTime() - deltaTime := time - c.lastTime + var deltaTime float32 = float32(time - c.lastTime) c.lastTime = time - forward, right, up := c.directions() - + _, right, _ := c.directions() globalUp := mgl32.Vec3{0, 1, 0} - move := right.Mul(c.currentMove.X()).Add(globalUp.Mul(c.currentMove.Y())).Add(forward.Mul(c.currentMove.Z())) - c.position = c.position.Add(move.Mul(float32(deltaTime) * c.moveSpeed)) - c.rotation = c.rotation.Add(c.currentLook.Mul(float32(deltaTime) * c.lookSpeed)) - c.currentLook = mgl32.Vec2{} - c.view = mgl32.LookAtV(c.position, c.position.Add(forward), up) + move := mgl32.Vec3{0, 0, 0} + if c.window.GetKey(glfw.KeyA) == glfw.Press { + move[0] += 1 + } + if c.window.GetKey(glfw.KeyD) == glfw.Press { + move[0] -= 1 + } + + if c.window.GetKey(glfw.KeySpace) == glfw.Press { + move[1] += 1 + } + if c.window.GetKey(glfw.KeyLeftShift) == glfw.Press { + move[1] -= 1 + } + + if c.window.GetKey(glfw.KeyW) == glfw.Press { + move[2] += 1 + } + if c.window.GetKey(glfw.KeyS) == glfw.Press { + move[2] -= 1 + } + + move = right.Mul(move.X()).Add(globalUp.Mul(move.Y())).Add(right.Cross(globalUp).Mul(move.Z())) + c.position = c.position.Add(move.Mul(deltaTime * c.moveSpeed)) + + x, y := c.window.GetCursorPos() + look := mgl32.Vec2{ + float32(y) - c.lastMousePos.X(), + c.lastMousePos.Y() - float32(x), + }.Mul(deltaTime * c.lookSpeed) + + c.rotation = c.rotation.Add(look) + c.rotation = mgl32.Vec2{ + min(max(-89.9, c.rotation.X()), 89.9), + float32(int32(c.rotation.Y())%360) + c.rotation.Y() - float32(int32(c.rotation.Y())), + } + c.lastMousePos[0] = float32(y) + c.lastMousePos[1] = float32(x) + + forward, _, _ := c.directions() + c.view = mgl32.LookAtV(c.position, c.position.Add(forward), globalUp) } func Get(lookSpeed, moveSpeed float32, position mgl32.Vec3, rotation mgl32.Vec2, window *glfw.Window) *FPSControls { @@ -53,43 +88,9 @@ func Get(lookSpeed, moveSpeed float32, position mgl32.Vec3, rotation mgl32.Vec2, lookSpeed: lookSpeed, position: position, rotation: rotation, + window: window, } - window.SetKeyCallback(func(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) { - var val float32 - - switch action { - case glfw.Repeat: - return - case glfw.Press: - val = 1 - case glfw.Release: - val = 0 - } - - switch key { - case glfw.KeyW: - instance.currentMove[2] = val - case glfw.KeyS: - instance.currentMove[2] = -val - case glfw.KeyA: - instance.currentMove[0] = val - case glfw.KeyD: - instance.currentMove[0] = -val - case glfw.KeySpace: - instance.currentMove[1] = val - case glfw.KeyLeftShift: - instance.currentMove[1] = -val - } - }) - - window.SetCursorPosCallback(func(window *glfw.Window, y float64, x float64) { - pos := mgl32.Vec2{float32(x), float32(y)} - instance.currentLook = instance.lastMousePos.Sub(pos) - instance.currentLook[0] *= -1 - instance.lastMousePos = pos - }) - window.SetInputMode(glfw.CursorMode, glfw.CursorDisabled) return instance