Honesty Rocks! truth rules.

## Circular Movment/animation In Java?

### Roders1234

Hey there guys! How would i go about making an object move about a fixed point for a java applet?Thanks in advance guys!Regards, Rod

### galexcd

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.

### Roders1234

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

### galexcd

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.

### Roders1234

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

### Roders1234

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.

### galexcd

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.