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!
Circular Cursor Lock
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: Circular Cursor Lock
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.
-
- Prole
- Posts: 34
- Joined: Mon Nov 24, 2014 9:15 pm
Re: Circular Cursor Lock
Can you write a set of instructions?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.
Example :
xcursorlock = atan(blah blah blah blah) ...
Thanks
Re: Circular Cursor Lock
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.
The math is rather easy, get the vector between center of the screen and mousepos, then clamp that to the radius.
-
- Prole
- Posts: 34
- Joined: Mon Nov 24, 2014 9:15 pm
Re: Circular Cursor Lock
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.
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.
Re: Circular Cursor Lock
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:
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.
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
Check out my blog on gamedev
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: Circular Cursor Lock
micha what about less than?
Re: Circular Cursor Lock
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.bobbyjones wrote:micha what about less than?
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
Check out my blog on gamedev
-
- Prole
- Posts: 34
- Joined: Mon Nov 24, 2014 9:15 pm
Re: Circular Cursor Lock
Thanks, This is what I was looking formicha wrote: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.bobbyjones wrote:micha what about less than?
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
Who is online
Users browsing this forum: Google [Bot] and 1 guest