Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Circular Movment/animation In Java?
Roders1234
post Feb 25 2008, 07:34 PM
Post #1


Newbie
*

Group: Members
Posts: 6
Joined: 28-October 07
Member No.: 52,152



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
Go to the top of the page
 
+Quote Post
galexcd
post Feb 25 2008, 07:58 PM
Post #2


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,069
Joined: 25-September 05
From: L.A.
Member No.: 12,251



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.
Go to the top of the page
 
+Quote Post
Roders1234
post Feb 25 2008, 08:33 PM
Post #3


Newbie
*

Group: Members
Posts: 6
Joined: 28-October 07
Member No.: 52,152



QUOTE(alex7h3pr0gr4m3r @ Feb 25 2008, 07:58 PM) *
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
Go to the top of the page
 
+Quote Post
galexcd
post Feb 26 2008, 12:42 AM
Post #4


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,069
Joined: 25-September 05
From: L.A.
Member No.: 12,251



QUOTE(Roders1234 @ Feb 25 2008, 12:33 PM) *
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.
Go to the top of the page
 
+Quote Post
Roders1234
post Feb 26 2008, 01:57 AM
Post #5


Newbie
*

Group: Members
Posts: 6
Joined: 28-October 07
Member No.: 52,152



QUOTE(alex7h3pr0gr4m3r @ Feb 26 2008, 12:42 AM) *
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
Go to the top of the page
 
+Quote Post
Roders1234
post Feb 27 2008, 03:28 PM
Post #6


Newbie
*

Group: Members
Posts: 6
Joined: 28-October 07
Member No.: 52,152



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.

This post has been edited by Roders1234: Feb 27 2008, 03:35 PM
Go to the top of the page
 
+Quote Post
galexcd
post Mar 8 2008, 05:48 AM
Post #7


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
***********

Group: [HOSTED]
Posts: 1,069
Joined: 25-September 05
From: L.A.
Member No.: 12,251



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.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Java editor(72)
  2. Auto Run Java Program(11)
  3. Java Script Drop Down Menu With Css(2)
  4. [help] Java Script: Window.open(10)
  5. Java Mobile Applications(7)
  6. How To Create Java Button Or Frame(14)
  7. Java Basic Program Guidance(6)
  8. Java Helpdesk(8)
  9. Problem With Java Script Popup Form(1)
  10. Best Java Framework For J2ee?(6)
  11. The Power Of Java(14)
  12. Jsp Or Java Chat Script Like Mig33(5)
  13. Helpful Registry Edit For Java Programmers(3)
  14. Java Iterator Help(2)
  15. Beginning Java(10)
  1. Java In A High School Software Design Course(5)
  2. Java Applet Query(1)
  3. Java Or C++(10)
  4. Java Vs Javascript(11)
  5. Java Object[][] Help(2)
  6. Learn Java Programming Language Online Step By Step(1)
  7. Java Game(2)
  8. Call Pdf995 From Java(0)
  9. How To Implement Single Instance Application On Java(0)
  10. Java Multithreading Issues(2)
  11. Java And Xml: Links You Must Have(1)
  12. Which Is Good Java Or Dot Net(3)
  13. Java Script To Hide The Url In Address Bar(6)


 



- Lo-Fi Version Time is now: 6th October 2008 - 10:35 PM