Sign in to follow this  

Draw an object moving fast

This topic is 4135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all im new in this forum! I have a little problem, i have to draw a fast object. Now to move an object i use this tecnique: x+=dx where dx is pixel covered distance in a process. When dx is little the aniamtion is smootly, but when the dx is too big the animation is bad and not smootly, how can i resolve it?

Share this post


Link to post
Share on other sites
You could use motion blur.

If you use DirectX:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/PixelMotionBlur_Sample.asp

You can do something similar for the API of your choice.

I'd also like to add that you may want to do the animation time_based in stead of frame based, if you are not already doing so. (Calculate the time passed between consecutive frames and multiply that by a dx, so the movement is greater if the frame took longer).

Greetings.

Share this post


Link to post
Share on other sites
Quote:
Original post by bytecoder
If you update your game more times per second you can make it smaller without decreasing it's speed.


Yes but the update cicle occupies a lot of cpu if im reduce dt time.

Share this post


Link to post
Share on other sites
Quote:
Original post by blow
Yes but the update cicle occupies a lot of cpu if im reduce dt time.


Games typically aim for using 100% of the CPU. So go ahead and use it, unless it's some windowed application that's supposed to play nice with other applications.

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by blow
Thank you!
Im using java and i cant use directx...
Can yout link me some useful article of time based and frame based animation?


How are you doing graphics with Java? Regular Java2D, JOGL, etc?

You can find the current time in java using System.currentTimeMillis(). Then between updates you can determine the elapsed time by storing the time at your last frame and checking what time it is now (and then making that time the new "previous time").

Then use the elapsed time in your calculations, so instead of x += dx, you have x += (dx*elapsed_time), which links the moving speed to real time (windows only seems to be accurate within like 16-20 milliseconds) rather than frame rate, which can smooth things out if the frame rate lags or something (still jitters, but not as much).

...or you can take a bunch of average elapsed times, so when the frame rate slows down significantly, it won't jitter as much.




//...
//make sure to initialize to something
long last = System.currentTimeMillis();
long current;

// finds the time since the last time this was called!
public long findElapsedTime() {
// stores elapsed time
long elapsed;

// gets the new current time
current = System.currentTimeMillis();

// find the time since our last calculation
elapsed = current - last;

// make our current time now the one to use next time
last = current;

// spit out our elapsed time
return elapsed;
}

Share this post


Link to post
Share on other sites
Ok now i want use 100% cpu.
When a game is frame-based end when is time-based? tick-based is sinonimus of frame-based?
Im programming 2D game, what mode you suggest me?

Share this post


Link to post
Share on other sites
Quote:
You can find the current time in java using System.currentTimeMillis().
If you're using Java 1.5, you should use System.nanoTime() instead, it has a much higher resolution.

Share this post


Link to post
Share on other sites
Quote:
Original post by mako_5
Quote:
Original post by blow
Thank you!
Im using java and i cant use directx...
Can yout link me some useful article of time based and frame based animation?


How are you doing graphics with Java? Regular Java2D, JOGL, etc?

You can find the current time in java using System.currentTimeMillis(). Then between updates you can determine the elapsed time by storing the time at your last frame and checking what time it is now (and then making that time the new "previous time").

Then use the elapsed time in your calculations, so instead of x += dx, you have x += (dx*elapsed_time), which links the moving speed to real time (windows only seems to be accurate within like 16-20 milliseconds) rather than frame rate, which can smooth things out if the frame rate lags or something (still jitters, but not as much).

...or you can take a bunch of average elapsed times, so when the frame rate slows down significantly, it won't jitter as much.


*** Source Snippet Removed ***

Thank you! in this case the gameLoop is a thread whit no pause?
like this:
Thread gameLoop=new Thread("GameLoop"){
public void run(){
while(gamePlay){
processPosition(elapsedTime);
}
}
};


In processPosition method i have x+=dx+elapsedTime;
Is good code?

Share this post


Link to post
Share on other sites
An other thing, my game is a network game, so it is multi-thread game, if i use 100% cpu in the thread gameLoop, the other thread "dead", or not?

Share this post


Link to post
Share on other sites
Quote:
Original post by blow
Quote:
Original post by mako_5
Quote:
Original post by blow
Thank you!
Im using java and i cant use directx...
Can yout link me some useful article of time based and frame based animation?


How are you doing graphics with Java? Regular Java2D, JOGL, etc?

You can find the current time in java using System.currentTimeMillis(). Then between updates you can determine the elapsed time by storing the time at your last frame and checking what time it is now (and then making that time the new "previous time").

Then use the elapsed time in your calculations, so instead of x += dx, you have x += (dx*elapsed_time), which links the moving speed to real time (windows only seems to be accurate within like 16-20 milliseconds) rather than frame rate, which can smooth things out if the frame rate lags or something (still jitters, but not as much).

...or you can take a bunch of average elapsed times, so when the frame rate slows down significantly, it won't jitter as much.


*** Source Snippet Removed ***

Thank you! in this case the gameLoop is a thread whit no pause?
like this:
Thread gameLoop=new Thread("GameLoop"){
public void run(){
while(gamePlay){
processPosition(elapsedTime);
}
}
};


In processPosition method i have x+=dx+elapsedTime;
Is good code?


No, it should be x += dx * elapsedTime;

if you use ms:
Because if you want to move 60 pixels per second (dx would be 60/1000), if you do 30 fps then elapsed time = 33.3 ms. dx * 33.3 *30 = (60/1000)*33.3*30 =(60/1000)*1000 = 60;
and if you run at 60 fps: elapsed time = 16.7 -> dx * 16.7*60 = 60 also.

This also works if the framerate varies within the second.

EDIT: where are you calculating elapsedTime? It should be right before processPosition. Calculate the difference between 2 consecutive calls to a time function. (lets say a function GetMilliSec exists which gives you a long)

have a long prev_ms and a long curr_ms declared and set to -1 for instance


curr_ms = GetMilliSec();
if (prev_ms == -1) prev_ms = curr_ms;
elapsedTime = curr_ms - prev_ms;
prev_ms = curr_ms;

processPosition(elapsedTime);

Share this post


Link to post
Share on other sites
Quote:
Original post by blow
An other thing, my game is a network game, so it is multi-thread game, if i use 100% cpu in the thread gameLoop, the other thread "dead", or not?


Yeah quite probably it will, but the network itself could have buffers, so if you just make your network thread so that there is a synchronous function that will read the buffers and sends out its own buffers you could call that every frame. Which would probably be better than having a pause for some time where you may or may not be sending info over the network.
Because you are using the time based processPosition, it doesn't matter anymore that sometimes the flushing of the buffers in the network thread takes more time in some of the cases and doesn't take anytime in other cases. Your movement will still appear smooth.

Greetings.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi thank you for all advices!!
So, my game is a network game, is better a frame-based engine instead a time-based engine whit this kind of game? So all client have always the some fps and is easy synconizig all client or not?

I can use timeElapsed to keep same frame per secon like this?

final int NANO_FRAME_LENGTH = 1000000000/FPS;
long startTime = System.nanoTime();
frameCount=0;
while(gameloop){
frameCount++;
//some processing code
//some rendering code
while((System.nanoTime()-startTime)/NANO_FRAME_LENGTH

Can work right?

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Hi thank you for all advices!!
So, my game is a network game, is better a frame-based engine instead a time-based engine whit this kind of game? So all client have always the some fps and is easy synconizig all client or not?

I can use timeElapsed to keep same frame per secon like this?

final int NANO_FRAME_LENGTH = 1000000000/FPS;
long startTime = System.nanoTime();
frameCount=0;
while(gameloop){
frameCount++;
//some processing code
//some rendering code
while((System.nanoTime()-startTime)/NANO_FRAME_LENGTH

Can work right?


No that wont work if your FPS is lower. Go with the Time-based. Frame-based is considered bad design and won't help you either when your thread locks up becuase of 100% CPU... so build your network code into your game loop. Or add a sleep(1) if you must. You can bound your frames if you want but use it in combination with a timebased update within the game loop.

Greetings.

Share this post


Link to post
Share on other sites
Ok, i understand! So i have to use time based tecnique, x+=dx*elapsedTime.

I cant build netcode in gameloop, my netcode is some like this:

waitDataFromServer(); //wait until any data is send by the server
processingData();


So this code block the thread...

I have write a network service, this service are thread and wiat and send data from/at the server.

Share this post


Link to post
Share on other sites

This topic is 4135 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this