Page 1 of 1

[SOLVED] Refining the "orbit" of the crosshair

Posted: Sat Dec 28, 2013 8:38 pm
by trackpoth
Hi! First of all, I've been following these forums for a while as a guest, but I believe that it was time to join already! ^^

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:
Image

How it should be:
Image

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
Sorry if there are any grammatical errors, english is my secondary language :/

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! :ultrahappy:

Re: Refining the "orbit" of the crosshair

Posted: Sun Dec 29, 2013 1:18 am
by ArchAngel075
Im not sure if it would help but :

A constant integer, Magnitude , how far the cursor is/should-be from player at all times.
Then take the position of mouse x,y , this is mx,my
then have player position, px,py

someAngle = math.atan2(py-my,px-py) --angle between player and mouse

then Cursor position is obtained via :

cx,cy is position,
doing Magnitude*math.cos(someAngle)+px gets the cursors X value,
doing Magnitude*math.sin(someAngle)+py gets the cursors Y value,

Thats it!

or in code : (Untested)

Code: Select all

 playerx,playery = 5,5
 Magnitude = 150
 mousex,mousey = love.mouse.getPosition()
 someAngle = math.atan2(playery-mousey,playerx-mousex)
 cursorX,cursorY = Magnitude*math.cos(someAngle)+playerx,Magnitude*math.sin(someAngle)+playery
-Edit-

Infact i went and made the edit myself to your main.lua, so here is the new main.lua and the edit starts at line 145 and ends at line 167
Ive slightly commented the changes and left your original code in, but commented out.
main.lua
Edited main.lua with fixed rotation
(7.81 KiB) Downloaded 84 times

Re: Refining the "orbit" of the crosshair

Posted: Sun Dec 29, 2013 11:52 am
by micha
Note that when you use math.sin/math.cos/math.atan2 then angles are measured in radians, not in degrees. That means that the full circle is 2pi and not 360°.

Re: [SOLVED] Refining the "orbit" of the crosshair

Posted: Sun Dec 29, 2013 1:27 pm
by trackpoth
ArchAngel075 wrote:Im not sure if it would help but: [...]
micha wrote:Note that when you use math.sin/math.cos/math.atan2 then angles are measured in radians, not in degrees. That means that the full circle is 2pi and not 360°.
Thank you a lot, problem solved! :awesome:

Re: [SOLVED] Refining the "orbit" of the crosshair

Posted: Sun Dec 29, 2013 10:43 pm
by ArchAngel075
I know that they are measured in radians, but its never given me issues when im dealing with rotation or components before, im not even sure what difference it makes besides the difference of radians and degrees, i just know that it works :P