This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Animating an object between two points

 
Ranch Hand
Posts: 41
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all, I'm making a game where you have to order troops around, meaning click on the destination, click on the soldier, and the soldier moves to the destination. I am having problems with the animation part, I have already figured out the MouseListener and graphics stuff. I know that you are supposed to use y = mx + b, m = y2 - y1/x2 - x1 and y - y1 = m(x - x1), but don't know how to implement this into my application, and I have been working on this for a long time with no success (hey, I'm in middle school, and I taught myself). I have a game class that contains the listeners, and a UserUnit class that contains the actual moveTo() method. Assume that I've got everything else figured out, if I don't I'll ask. Any help is appreciated, and thanks in advance.
 
Saloon Keeper
Posts: 15732
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, an animation is a function of distance over time. At time "t" you want to know distance "d" from the starting point. So you need to think of a function that determines this.

If you have a "begin" point and an "end" point, then the "total" distance to be traveled is end - begin. If you want to travel from begin to end in a period of time "duration", then d = distance / duration * t
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except you need to use a javax.swing.Timer in order for the Event Dispatcher Thread to be able to do the repainting in between. If you use a loop like that, only the last movement will be seen.
 
Stephan van Hulst
Saloon Keeper
Posts: 15732
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I agree with Rob. My for loop was a poor example.
 
Jj Hill
Ranch Hand
Posts: 41
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've actually found a way to use Thread.sleep(), but thanks for the advice.
 
Jj Hill
Ranch Hand
Posts: 41
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know how to animate it, my problem is the actual movement.
 
Stephan van Hulst
Saloon Keeper
Posts: 15732
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by actual movement?
 
There's a city wid manhunt for this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic