Mouse: How to emulate "MouseLook" from FPS games?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Mouse: How to emulate "MouseLook" from FPS games?
First rule of engineering: sin(x) ≈ x for small x
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
Re: Mouse: How to emulate "MouseLook" from FPS games?
This kinda works for me:
All angles are done in radians, but it prints them out in degrees, as I find them easier to read/understand. And it's easier to see how the angle is changing (radians are too small).
Code: Select all
function love.load()
TURN_SPEED = 0.005
VISION_LENGTH = 1
inverseYAxis = true
horizontalAngle = 0
verticalAngle = 0
love.mouse.setGrab(true)
love.mouse.setVisible(false)
love.mouse.setPosition( love.graphics.getWidth() / 2, love.graphics.getHeight() / 2 )
oldMouse = { love.mouse.getPosition() }
end
function love.update(dt)
if love.keyboard.isDown("q") then
love.event.push("q")
end
local dx = love.mouse.getX() - oldMouse[1]
local dy = love.mouse.getY() - oldMouse[2]
if dx ~= 0 then
local angle = math.atan( (dx * TURN_SPEED) / VISION_LENGTH )
horizontalAngle = (horizontalAngle + angle) % (math.pi * 2)
end
if dy ~= 0 then
local angle = math.atan( (dy * TURN_SPEED) / VISION_LENGTH )
if inverseYAxis then
verticalAngle = math.max(math.min(verticalAngle + angle, math.pi / 2), -math.pi / 2)
else
verticalAngle = math.max(math.min(verticalAngle - angle, math.pi / 2), -math.pi / 2)
end
end
love.mouse.setPosition(love.mouse.getX() - dx, love.mouse.getY() - dy)
end
function love.draw()
love.graphics.print( "horiz: "..(horizontalAngle * 180 / math.pi), 16, 16 )
love.graphics.print( "verti: "..(verticalAngle * 180 / math.pi), 16, 32 )
love.graphics.circle( "line", love.graphics.getWidth() / 2, love.graphics.getHeight() / 2, 5 )
end
Re: Mouse: How to emulate "MouseLook" from FPS games?
Interesting. A hidden cursor works like a charm with updates on every frame.IMP1 wrote:This kinda works for me.
Shallow indentations.
Re: Mouse: How to emulate "MouseLook" from FPS games?
IMP1's solution totally works.
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
Re: Mouse: How to emulate "MouseLook" from FPS games?
It's not perfect in terms of the angle. It should be slightly less, but the maths is a lot more confusing.
should beNot sure about this, but I have this drawn out
Other quadrants might need adjusting for. And if visionLength is large, then visionLength^2 - dx^2 is roughly visionLength^2.
*shrug*
EDIT: So I guess with this maths-ness, it becomes:TURN_SPEED and VISION_LENGTH need to be edited to suit yer wolfen-thingamybob.
Code: Select all
local angle = math.atan( (dx * TURN_SPEED) / VISION_LENGTH )
Code: Select all
local y = math.sqrt(math.abs(VISION_LENGTH ^ 2 - dx ^ 2))
local angle = math.atan( (dx * TURN_SPEED) / y )
Other quadrants might need adjusting for. And if visionLength is large, then visionLength^2 - dx^2 is roughly visionLength^2.
*shrug*
EDIT: So I guess with this maths-ness, it becomes:
Code: Select all
-- main.lua
function love.load()
TURN_SPEED = 1
VISION_LENGTH = 100
inverseYAxis = true
horizontalAngle = 0
verticalAngle = 0
love.mouse.setGrab(true)
love.mouse.setVisible(false)
love.mouse.setPosition( love.graphics.getWidth() / 2, love.graphics.getHeight() / 2 )
oldMouse = { love.mouse.getPosition() }
end
function love.update(dt)
if love.keyboard.isDown("q") then
love.event.push("q")
end
local dx = love.mouse.getX() - oldMouse[1]
local dy = love.mouse.getY() - oldMouse[2]
if dx ~= 0 then
local angle = math.atan( (dx * TURN_SPEED) / math.sqrt(math.abs(VISION_LENGTH^2 - dx^2)) )
horizontalAngle = (horizontalAngle + angle) % (math.pi * 2)
end
if dy ~= 0 then
local angle = math.atan( (dy * TURN_SPEED) / math.sqrt(math.abs(VISION_LENGTH^2 - dy^2)) )
if inverseYAxis then
verticalAngle = math.max(math.min(verticalAngle + angle, math.pi / 2), -math.pi / 2)
else
verticalAngle = math.max(math.min(verticalAngle - angle, math.pi / 2), -math.pi / 2)
end
end
love.mouse.setPosition(love.mouse.getX() - dx, love.mouse.getY() - dy)
end
function love.draw()
love.graphics.print( "horiz: "..(horizontalAngle * 180 / math.pi), 16, 16 )
love.graphics.print( "verti: "..(verticalAngle * 180 / math.pi), 16, 32 )
love.graphics.circle( "line", love.graphics.getWidth() / 2, love.graphics.getHeight() / 2, 5 )
end
Who is online
Users browsing this forum: Google [Bot] and 3 guests