- Home /
Unit Circle Equation Question!
Hey there!
So I'm working with a GUI script that needs to allow someone to drag the cursor by holding down the mouse button, but only to a certain distance away from where they started holding down that mouse button. After that point, the cursor should stay in the circle--but at an angle that's appropriate to where the cursor is outside of the circle. It's sort of the way that Angry Birds allows you to drag the bird a certain distance away from the slingshot, but then keeps the bird at the correct angle, even if you're dragging it farther than the slingshot will allow. So, I'm calculating the angle that the cursor is from the center of the circle correctly (i.e. 360 and 0 are when you're directly "above" it in the GUI, and 180 is below, and so on. However, when the cursor attempts to calculate its position, it's moving way too quickly around the circle. I think it has to do with these two lines of code. Do you guys know what I'm doing wrong, calculating the psoitions of the cursor? I realize this question is kind of vague, and perhaps hard to understand. But I've made a video of what's going on, so you can see what I mean...here's the code:
circleCoordinates.x = (dragPlaneRadius * (Mathf.Cos(angleFromOrigin)) + xOffset;
circleCoordinates.y = (dragPlaneRadius * (Mathf.Sin(angleFromOrigin)) + yOffset;
and here's the video:
I'm sure I've answered something similar to this recently...
Things to remember- don't try to directly change the cursor position, ins$$anonymous$$d you should hide the cursor and draw a GUI.DrawTexture or the like where your corrected cursor should be. The windowing system sometimes gets rather upset if you try to override the cursor! $$anonymous$$y answer may or may not help you- it depends on exactly what the problem is. From the sounds of things, you already have a way of correcting your cursor position, but it's buggy (hence the question). If my answer doesn't help you, can you post the full code for your cursor correction in an edit to your original question? I can help you debug it.
Answer by syclamoth · Sep 28, 2011 at 12:19 PM
Ok then. Assuming that you know the true position of the mouse, and the point at which you want the cursor to rotate, first you get the original offset:
Vector2 originalOffset = mousePosition - rotateAroundThis;
Then you use this handy function called Atan2-
float angle = Atan2(originalOffset);
Then use that and your desired radius to reconstruct the correct location.
float correctRadius = Mathf.Clamp(0, desiredRadius, originalOffset.magnitude);
Vector2 correctOffset = new Vector2(correctRadius * Mathf.Cos(angle), correctRadius * Mathf.Sin(angle));
Then use the correctOffset to draw your cursor!
This is in C#, because it's my mother tongue (so to speak). I can translate it into Javascript if you need me to!
Ah! this works perfectly, I've just thrown it in...but I just realized my first problem problem--I was using degrees to calculate something I needed radians for! GAHHHH...thanks for your help.
Your answer