2024-10-12 16:25:54 +00:00
|
|
|
package fpscontrols
|
|
|
|
|
|
|
|
import (
|
2024-10-15 14:52:21 +00:00
|
|
|
"math"
|
|
|
|
|
2024-10-12 16:25:54 +00:00
|
|
|
"github.com/go-gl/glfw/v3.3/glfw"
|
|
|
|
"github.com/go-gl/mathgl/mgl32"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FPSControls struct {
|
|
|
|
lookSpeed float32
|
|
|
|
moveSpeed float32
|
|
|
|
|
|
|
|
position mgl32.Vec3
|
|
|
|
rotation mgl32.Vec2
|
|
|
|
|
|
|
|
currentMove mgl32.Vec3
|
|
|
|
currentLook mgl32.Vec2
|
|
|
|
lastMousePos mgl32.Vec2
|
2024-10-15 14:52:21 +00:00
|
|
|
|
|
|
|
view mgl32.Mat4
|
|
|
|
|
|
|
|
lastTime float64
|
2024-10-12 16:25:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var instance *FPSControls
|
|
|
|
|
2024-10-15 14:52:21 +00:00
|
|
|
func (c *FPSControls) Update() {
|
|
|
|
time := glfw.GetTime()
|
|
|
|
|
|
|
|
deltaTime := time - c.lastTime
|
|
|
|
c.lastTime = time
|
|
|
|
|
|
|
|
forward, right, up := 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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-10-12 16:25:54 +00:00
|
|
|
func Get(lookSpeed, moveSpeed float32, position mgl32.Vec3, rotation mgl32.Vec2, window *glfw.Window) *FPSControls {
|
|
|
|
if instance != nil {
|
|
|
|
return instance
|
|
|
|
}
|
|
|
|
|
|
|
|
instance = &FPSControls{
|
|
|
|
moveSpeed: moveSpeed,
|
|
|
|
lookSpeed: lookSpeed,
|
|
|
|
position: position,
|
|
|
|
rotation: rotation,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-10-15 14:52:21 +00:00
|
|
|
case glfw.KeyS:
|
|
|
|
instance.currentMove[2] = -val
|
2024-10-12 16:25:54 +00:00
|
|
|
case glfw.KeyA:
|
|
|
|
instance.currentMove[0] = val
|
2024-10-15 14:52:21 +00:00
|
|
|
case glfw.KeyD:
|
|
|
|
instance.currentMove[0] = -val
|
2024-10-12 16:25:54 +00:00
|
|
|
case glfw.KeySpace:
|
|
|
|
instance.currentMove[1] = val
|
2024-10-15 14:52:21 +00:00
|
|
|
case glfw.KeyLeftShift:
|
|
|
|
instance.currentMove[1] = -val
|
2024-10-12 16:25:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-10-15 14:52:21 +00:00
|
|
|
window.SetCursorPosCallback(func(window *glfw.Window, y float64, x float64) {
|
2024-10-12 16:25:54 +00:00
|
|
|
pos := mgl32.Vec2{float32(x), float32(y)}
|
|
|
|
instance.currentLook = instance.lastMousePos.Sub(pos)
|
2024-10-15 14:52:21 +00:00
|
|
|
instance.currentLook[0] *= -1
|
2024-10-12 16:25:54 +00:00
|
|
|
instance.lastMousePos = pos
|
|
|
|
})
|
|
|
|
|
2024-10-15 14:52:21 +00:00
|
|
|
window.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
|
|
|
|
|
2024-10-12 16:25:54 +00:00
|
|
|
return instance
|
|
|
|
}
|
|
|
|
|
2024-10-15 14:52:21 +00:00
|
|
|
func (c *FPSControls) directions() (mgl32.Vec3, mgl32.Vec3, mgl32.Vec3) {
|
|
|
|
rad := mgl32.Vec2{
|
|
|
|
mgl32.DegToRad(c.rotation.X()),
|
|
|
|
mgl32.DegToRad(c.rotation.Y()),
|
|
|
|
}
|
|
|
|
|
|
|
|
sin := mgl32.Vec2{
|
|
|
|
float32(math.Sin(float64(rad.X()))),
|
|
|
|
float32(math.Sin(float64(rad.Y()))),
|
|
|
|
}
|
|
|
|
|
|
|
|
cos := mgl32.Vec2{
|
|
|
|
float32(math.Cos(float64(rad.X()))),
|
|
|
|
float32(math.Cos(float64(rad.Y()))),
|
|
|
|
}
|
|
|
|
|
|
|
|
forward := mgl32.Vec3{
|
|
|
|
cos.X() * sin.Y(),
|
|
|
|
-sin.X(),
|
|
|
|
cos.X() * cos.Y(),
|
|
|
|
}
|
|
|
|
|
|
|
|
right := mgl32.Vec3{
|
|
|
|
cos.Y(),
|
|
|
|
0,
|
|
|
|
-sin.Y(),
|
|
|
|
}
|
2024-10-12 16:25:54 +00:00
|
|
|
|
2024-10-15 14:52:21 +00:00
|
|
|
up := forward.Cross(right)
|
|
|
|
|
|
|
|
return forward, right, up
|
2024-10-12 16:25:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FPSControls) View() mgl32.Mat4 {
|
2024-10-15 14:52:21 +00:00
|
|
|
return c.view
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FPSControls) Position() mgl32.Vec3 {
|
|
|
|
return c.position
|
|
|
|
}
|
2024-10-12 16:25:54 +00:00
|
|
|
|
2024-10-15 14:52:21 +00:00
|
|
|
func (c *FPSControls) Rotation() mgl32.Vec2 {
|
|
|
|
return c.rotation
|
2024-10-12 16:25:54 +00:00
|
|
|
}
|