Circular Cursor Lock

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Ethan-Taylor
Prole
Posts: 34
Joined: Mon Nov 24, 2014 9:15 pm

Circular Cursor Lock

Post by Ethan-Taylor »

Hey Guys,
I was wondering how I would make the cursor locked to a circle around my player?
So If you move your cursor around the cursor stays locked to a circle path around the player.

I should've listened to all those maths lessons XD

Anyway,
Thanks!
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Circular Cursor Lock

Post by bobbyjones »

I would just do two circle collision checks but I'm sure that is the hacky way of doing things. Another option would just be to use atan2 and the coord of the mouse and the coord of the player to get an angle. And then just use the radius, angle,sin and cos to get the new coords at the distance needed. I said this quite briefly as I haven't programmed in awhile and not at a pc to test. But I hope this is enough to help you figure it out.
Ethan-Taylor
Prole
Posts: 34
Joined: Mon Nov 24, 2014 9:15 pm

Re: Circular Cursor Lock

Post by Ethan-Taylor »

bobbyjones wrote:I would just do two circle collision checks but I'm sure that is the hacky way of doing things. Another option would just be to use atan2 and the coord of the mouse and the coord of the player to get an angle. And then just use the radius, angle,sin and cos to get the new coords at the distance needed. I said this quite briefly as I haven't programmed in awhile and not at a pc to test. But I hope this is enough to help you figure it out.
Can you write a set of instructions?
Example :

xcursorlock = atan(blah blah blah blah) ...

Thanks :D
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Circular Cursor Lock

Post by s-ol »

You can try setting the mouse back into the circle every frame, but that can be laggy/glitchy/buggy. Alternatively enable relative mode and draw a virtual cursor yourself.

The math is rather easy, get the vector between center of the screen and mousepos, then clamp that to the radius.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Ethan-Taylor
Prole
Posts: 34
Joined: Mon Nov 24, 2014 9:15 pm

Re: Circular Cursor Lock

Post by Ethan-Taylor »

S0lll0s wrote:You can try setting the mouse back into the circle every frame, but that can be laggy/glitchy/buggy. Alternatively enable relative mode and draw a virtual cursor yourself.

The math is rather easy, get the vector between center of the screen and mousepos, then clamp that to the radius.
Image

Here's a diagram. The orange circle is the cursor and the black line is the path it's locked to.
The purple square is the player. When you move the cursor around it stays locked to the path.

Any code will help.

Thanks.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Circular Cursor Lock

Post by micha »

Lets say the player is at coordinates (px,py) and the mouse at (mx,my) and the desired radius is r then you can calculate the coordinates you are looking for like this:

Code: Select all

function getPositionOnCircle(px,py,mx,my,r)
  local dx,dy = mx-px, my-py
  local dist = math.sqrt(dx^2+dy^2)
  if  dist > r then
    return px + dx/dist*r, py + dy/dist*r
  else
    return px,py
  end
end
You don't need trigonometry, only Pythagoras Theorem. The code basically calculates the distance between mouse and player and if it larger than the given radius, divides the distance vector by the distance and multiplies by the radius.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Circular Cursor Lock

Post by bobbyjones »

micha what about less than?
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Circular Cursor Lock

Post by micha »

bobbyjones wrote:micha what about less than?
In that case, the function returns the actual mouse coordinates. That means that the mouse can freely move inside the circle, but if it is outside, the mouse position is corrected.

I noticed an error I made in the previous code snippet. This is the corrected version.

Code: Select all

function getPositionOnCircle(px,py,mx,my,r)
  local dx,dy = mx-px, my-py
  local dist = math.sqrt(dx^2+dy^2)
  if  dist > r then
    return px + dx/dist*r, py + dy/dist*r
  else
    return mx,my
  end
end
Ethan-Taylor
Prole
Posts: 34
Joined: Mon Nov 24, 2014 9:15 pm

Re: Circular Cursor Lock

Post by Ethan-Taylor »

micha wrote:
bobbyjones wrote:micha what about less than?
In that case, the function returns the actual mouse coordinates. That means that the mouse can freely move inside the circle, but if it is outside, the mouse position is corrected.

I noticed an error I made in the previous code snippet. This is the corrected version.

Code: Select all

function getPositionOnCircle(px,py,mx,my,r)
  local dx,dy = mx-px, my-py
  local dist = math.sqrt(dx^2+dy^2)
  if  dist > r then
    return px + dx/dist*r, py + dy/dist*r
  else
    return mx,my
  end
end
Thanks, This is what I was looking for :D
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest