 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mike Crenshaw Guest
|
Posted: Tue Sep 28, 2004 4:06 pm Post subject: smooth animation |
|
|
Hi
I'm trying to figure out how to make an animation move smoothly.. so i've
been looking around.. i've found 3 other methods besides just using sleep()
at each frame.. i've tried implementing these methods but i cant see much of
a difference.. but which of the following, if any, is the best way to go
about it.. or is there a better way..
method 1: the nono
...
while(true){
x++; //some simple movement
y++;
paint(getGraphics());
try{
Thread.currentThread().sleep(delay);
}
catch(Exception e){}
}
method 2:
....
while(true){
long start = System.currentTimeMillis();
x++; //some simple movement
y++;
paint(getGraphics());
try {
Thread.sleep(Math.max(0, (start + delay) -
System.currentTimeMillis()));
}
catch (InterruptedException e) {};
}
method 3:
....
while(true){
long start = System.currentTimeMillis();
x++; //some simple movement
y++;
paint(getGraphics());
long sleepTime = delay - (System.currentTimeMillis() - start);
if (sleepTime > 0)
try{
Thread.currentThread().sleep(sleepTime);
}
catch(Exception e){}
}
method 4: frame independent
int velocity = 500;
long end_time = 0;
long start_time = System.currentTimeMillis();
float elapsed_time = 0;
float dtime = 0;
while(true){
end_time = System.currentTimeMillis();
elapsed_time = end_time - start_time ;
dtime = elapsed_time / 1000 ;
x += dtime * velocity; //some simple movement
y += dtime * velocity;
start_time = System.currentTimeMillis();
paint(getGraphics());
try{
Thread.currentThread().sleep(delay);
}
catch(Exception e){}
}
thanx
mike
|
|
| Back to top |
|
 |
Paul Lutus Guest
|
Posted: Tue Sep 28, 2004 4:50 pm Post subject: Re: smooth animation |
|
|
Mike Crenshaw wrote:
| Quote: | Hi
I'm trying to figure out how to make an animation move smoothly.. so i've
been looking around..
|
Define "move smoothly". How many frames per second do you require? How large
is the animated frame? What is the color depth? What is the target
environment? What is the expected target platform's clock speed?
Define "looking around". Which painting methods have you tried?
| Quote: | i've found 3 other methods besides just using
sleep() at each frame.. i've tried implementing these methods but i cant
see much of a difference.. but which of the following, if any, is the best
way to go about it.. or is there a better way..
|
The "better way" is the one that delivers what you want, which you don't
specify. You don't specify what is being drawn, how it is being drawn, or
provide a working code example.
--
Paul Lutus
http://www.arachnoid.com
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|