Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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



Report this thread to moderator Post Follow-up to this message
Old Post
Mike Crenshaw
09-28-04 09:13 PM


Re: smooth animation
Mike Crenshaw wrote:

> 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?

> 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


Report this thread to moderator Post Follow-up to this message
Old Post
Paul Lutus
09-28-04 09:13 PM


Re: smooth animation
In article <d_f6d.1198$Wv1.248@news.get2net.dk>,
"Mike Crenshaw" <someone@microsoft.com> wrote:

> method 1: the nono

These are all "nonos". You shouldn't be calling paint() directly.

What you are probably referring to is double buffering, where you do
your drawing into an offscreen image, then copy the image onscreen all
at once, the effect being that the user doesn't see the flickering that
results from successive drawing operations over the same canvas.

If you're concerned about keeping a constant frame rate, then just call
System.currentTimeMillis() or whatever it is, and subtract how long you
spend painting from how long of a delay you want between frames, and
then Thread.sleep() for that long.

--
|\/|  /|  |2  |<
mehaase(at)sas(dot)upenn(dot)edu

Report this thread to moderator Post Follow-up to this message
Old Post
Mark Haase
09-29-04 02:28 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:31 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.