Jump to content
xisto Community
Sign in to follow this  
Roders1234

Circular Movment/animation In Java?

Recommended Posts

Assuming you already know how to make gui in java and draw objects on the screen you can find the x and y of a certain object over a given time using sine and cosine.If the point is at x,y, the following two functions would give you the x and y of the object.X(t)=x+r*cos(t*2*PI)Y(t)=y+r*sin(t*2*PI)where r is the radius that you want it to follow, and t is the amount of times you want it to go around for example if you want it to go around once a second, increment T once every second (but if you want the animation to be smother you will have to increment it smaller in smaller amounts in less amount of time)Also since this is java you might want to change PI to Math.PI, unless you have already defined the constant pi in your program.

Share this post


Link to post
Share on other sites

Assuming you already know how to make gui in java and draw objects on the screen you can find the x and y of a certain object over a given time using sine and cosine.
If the point is at x,y, the following two functions would give you the x and y of the object.

X(t)=x+r*cos(t*2*PI)
Y(t)=y+r*sin(t*2*PI)
where r is the radius that you want it to follow, and t is the amount of times you want it to go around for example if you want it to go around once a second, increment T once every second (but if you want the animation to be smother you will have to increment it smaller in smaller amounts in less amount of time)

Also since this is java you might want to change PI to Math.PI, unless you have already defined the constant pi in your program.


Thanks for this Alex, its really helpful!

Just to clarrify...

x would be the x coord of the fixed point? Also t = 1 is the same as one full journey of the circle?

Thanks again!

Rod

Share this post


Link to post
Share on other sites

Thanks for this Alex, its really helpful!
Just to clarrify...

x would be the x coord of the fixed point? Also t = 1 is the same as one full journey of the circle?

Thanks again!

Rod


Yes, lowercase-x is the x coord of the fixed point, and uppercase-X(t) returns the x coord of the moving object. And yes t=1 is one full journey of the circle, so what I would suggest if you wanted it to go around once a second smoothly, increment t by 1/100 every 10 milliseconds.

Share this post


Link to post
Share on other sites

Yes, lowercase-x is the x coord of the fixed point, and uppercase-X(t) returns the x coord of the moving object. And yes t=1 is one full journey of the circle, so what I would suggest if you wanted it to go around once a second smoothly, increment t by 1/100 every 10 milliseconds.

Thanks a bunch Alex!

This is exavtly what I have been searching for. Hopefully ill be able to post the results at a later stage.

Kind Regards, Rod

Share this post


Link to post
Share on other sites

There seems to be a problem (Well not really a probelm just an unexpected outcome). In the formular X(t) = x + r * cos(t*2*PI), x does not seem to be the central point rather x + r is the radius of the circle from point 0,0. But this is easily overcome by simply adding the value given by the forumla to the X and Y values of the centre point. Just thought i'd clear that up if anyone were to stumble across this.EDIT: perhaps a better way to explain woul be to say X(t) = [r * cos(*t*2*PI)] + x; Where x is the centre point's x coordinate.

Edited by Roders1234 (see edit history)

Share this post


Link to post
Share on other sites

Also another quick note that I forgot to mention: Using sine for y and cosine for x would make the object move counter-clockwise. If you wanted it to move clockwise, switch x and y.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.