x


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:

more ▼

asked Sep 28 '11 at 11:06 AM

Catlard gravatar image

Catlard
527 48 60 66

I'm sure I've answered something similar to this recently...

Sep 28 '11 at 11:52 AM syclamoth

heh...I wish I knew that anwer!

Sep 28 '11 at 12:08 PM Catlard

Things to remember- don't try to directly change the cursor position, instead 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! My 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.

Sep 28 '11 at 12:23 PM syclamoth
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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!

more ▼

answered Sep 28 '11 at 12:19 PM

syclamoth gravatar image

syclamoth
15k 7 15 80

This is in C#, because it's my mother tongue (so to speak). I can translate it into Javascript if you need me to!

Sep 28 '11 at 12:20 PM syclamoth

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.

Sep 28 '11 at 12:31 PM Catlard
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x60
x2

asked: Sep 28 '11 at 11:06 AM

Seen: 838 times

Last Updated: Sep 28 '11 at 12:31 PM