I'll try to be brief: I've been working in a small platformer since a month ago. I managed to solve all the problems that had been popping out, but I don't know the solution for this one (Well, it's not a game-breaking problem, it's just an annoyance). I don't know how to explain it properly, so here are two images of the problem:
How it is:
How it should be:
What I'm trying to do is to make the crosshair orbit as pictured in the second image, but the only way I know to make the crosshair keep track of the cursor properly and to position itself between the cursor and the player is this one:
Code: Select all
-- findRotation function taken from https://www.love2d.org/wiki/findRotation
local angle = math.floor(findRotation(p.x, p.y, mousePosX, mousePosY)) -- determines the angle between player & cursor
-- p.x, p.y = player coordinates
if angle < 91 then
crosshairX = p.x - angle * 1.1
crosshairY = p.y + (90 - angle) * 1.1
end
if angle > 90 and angle < 181 then
crosshairX = p.x - (90 - (angle - 90)) * 1.1
crosshairY = p.y - (angle - 90) * 1.1
end
if angle > 180 and angle < 271 then
crosshairX = p.x + (angle - 180) * 1.1
crosshairY = p.y - (90 - (angle - 180)) * 1.1
end
if angle > 270 then
crosshairX = p.x + (90 - (angle - 270)) * 1.1
crosshairY = p.y + (angle - 270) * 1.1
end
Note: The code is very messy and it is uncommented, but I hope that it is understandable enough
Note 2: The snippet posted here is located at main.lua, in love.update(dt)
Note 3: The bgm was removed to keep the .love below 10 MiB, if the music is switched on ingame it will crash
Thank you in advance!