bad effect when draw object moving fast

Hi, i have a shooter game where a gun can shoot a bullet.

This bullet run fast so i move it whit X+=dx (dx is distance in pixel)

When the bullet run too fast i ahve a bad effect, ot smootly.

How i can resolve?

Now my code is frame based i think, i use this:

delay =30;

Timer timer=new Timer(delay,new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

processBulletPosition();

timer.setDelay(delay-delayed);

}

});

publicvoid processBulletPosition(){

bullet.process(new 2DVector(10,0));//2DVector is simple object contain dx and dy increment.

repaint();

}

//im use JPanel to paint, not canvas

protectedvoid paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D g2=(Graphics2D)g;

nanoTime=System.nanoTime();

bullet.draw(g2);

delayed=(System.nanoTime()-nanoTime)*1E-6;//make delayed in millisec

}

bullet proces is like this:

publicvoid process(2DVector vect){

int dx=vect.dx;

int dy=vect.dy;

x+=dx;

y+=dy;

}

Animation is very smooty, but when a bullet or an other object run too fast and dx increment is too many pixel the effect is very bad, and not smooty.

I think i have same fps becouse i decrement delay time of a delayed time...

Can you help me?

Message was edited by:

blow

[2212 byte] By [blowa] at [2007-10-3 3:02:19]
# 1

One problem is that I'm not sure how accurate Timer is.

A standard approach in this sort of animation is to measure the time since the last redraw and move each component based on the time difference, rather than trying to keep the redraws an exact time apart and moving the components fixed distances.

The distance/time approach reduces the effect of pauses in your rendering code caused by outside influences such as the garbage collector and other game logic such as spawning controllers etc. It won't eliminate pauses but it makes them much less noticable.

TimRyanNZa at 2007-7-14 20:52:04 > top of Java-index,Other Topics,Java Game Development...
# 2
Thank your for advice!So if i dont use a Timer but only measure the render time my frame rate can varius but my bullet move at same velocity?
blowa at 2007-7-14 20:52:04 > top of Java-index,Other Topics,Java Game Development...
# 3

That's the idea.

You may still want to put in a Thread.sleep(millisecs) so that your game doesn't take 100% CPU if it doesn't need to.

The beauty of using a time-between-frames = distance-moved renderer is that it is very forgiving of different computer speeds - the game runs at the same apparent speed almost regardless of the power of the PC.

TimRyanNZa at 2007-7-14 20:52:04 > top of Java-index,Other Topics,Java Game Development...
# 4

> One problem is that I'm not sure how accurate Timer

> is.

>

> A standard approach in this sort of animation is to

> measure the time since the last redraw and move each

> component based on the time difference, rather than

> trying to keep the redraws an exact time apart and

> moving the components fixed distances.

I'm having the same problem, even while using double-buffering. What classes would allow me to measure time?

Pakkabowla at 2007-7-14 20:52:04 > top of Java-index,Other Topics,Java Game Development...
# 5

Well there is System.currentTimeMillis() of course but unfortunately that has a terrible granularity (sometimes as bad as 16mS or more depending on your platform).

If you are running Java 1.5 then your simplest choice is System.nanoTime(). If not, you'll have to go to a third-party solution - there are several of them including one in LWJGL I think.

TimRyanNZa at 2007-7-14 20:52:04 > top of Java-index,Other Topics,Java Game Development...