Better understanding of Sine/Cosine?

Non-platform specific questions.

Better understanding of Sine/Cosine?

Postby Hblade » Tue Oct 07, 2014 1:29 am

Hi! I'd like to rotate small objects around the center of the screen, based on how far off they are... For example, if it's about 5 pixels from the center, and I "rotate", I want that object to go around the center within that 5 pixel radius, 25 pixels, I'd like it to go around the center on a 25 pixel radius. :) If anybody knows how I can do this, please tell me lol, I've tried using sine/cosine, but since you can only do a max of sin(1), which prettymuch is "jitter" mode, I don't understand the math needed to calculate it.... :D
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Better understanding of Sine/Cosine?

Postby skydereign » Tue Oct 07, 2014 2:50 am

So what you want is to have a fixed distance from a point, but constantly change the angle. This is the math you are looking for.
Code: Select all
double ang = degtorad(direction(0, 0, x, y) + 1); // assuming the center is 0, 0
// not the above has a +1 to set the angular velocity to be 1 degree per frame
double radius = distance(0, 0, x, y);

x = 0 + cos(ang)*radius; // the 0 is the center x coordinate
y = 0 - sin(ang)*radius; // the reason for minus sin instead of plus is up in gE is negative, unlike normal math coordinates

What I think you meant by max of sin(1), is that the max value returned by the sin function is 1. It's range is from -1 to 1. sin/cos are used to calculate the points along a circle, based off the angle you pass it. But since you want want to deal with circles with radii greater than 1, all you need to do is multiply the sin/cos values you get by the radius you want.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Better understanding of Sine/Cosine?

Postby Hblade » Tue Oct 07, 2014 2:53 am

Thanks a bunch sky. Your method is a bit different than the method I figured out, but I'm going to look into yours for learning purposes. Yours is more advanced and accurate than mine haha. Thanks a bunch, using this code should teach me a lot of new tricks..

EDIT: Thanks, I've actually learned a lot from what you sent me, however, using it for what I need is quite difficult lol. Here's an image explaining what I mean

Red represents an object 25 pixels away from the center, blue represents 50 pixels, and yellow represents 100 pixels, depending on how far the object is from the view, I want it to rotate around the middle of the view.

EDIT: I actually do have my own way, but still interested in seeing any other forms :D My version is quite long lol
Attachments
example.png
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron