aspose file tools
The moose likes Game Development and the fly likes Animating an object between two points Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Animating an object between two points" Watch "Animating an object between two points" New topic
Author

Animating an object between two points

Jj Hill
Ranch Hand

Joined: Jul 25, 2011
Posts: 41

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.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3884
    
  18

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


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20046
    
  30

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3884
    
  18

Yes, I agree with Rob. My for loop was a poor example.
Jj Hill
Ranch Hand

Joined: Jul 25, 2011
Posts: 41

I've actually found a way to use Thread.sleep(), but thanks for the advice.
Jj Hill
Ranch Hand

Joined: Jul 25, 2011
Posts: 41

I know how to animate it, my problem is the actual movement.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3884
    
  18

What do you mean by actual movement?
 
wood burning stoves
 
subject: Animating an object between two points
 
It's not a secret anymore!