Results 1 to 3 of 3
Thread: Java Game Timer Issue! Help
- 03-04-2010, 06:26 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 2
- Rep Power
- 0
Java Game Timer Issue! Help
Hey guys
Got a bit of a problem, I have created this little pong pong style Java app and I thought everything was working fine however when I tried it at the Universitys computers it was a lot slower then normal.
I have a feeling it has to do with the timer which calls the repaint() method every 3 milliseconds:
Any ideas why the speed of the game varies from PC to PC?Java Code:public void actionPerformed(ActionEvent e) { box.move(); ball.move(); checkcollision(); score.setText(""+scoreno); repaint(); }
Cheers
Smithywill
- 03-04-2010, 09:13 PM #2
You cannot guarantee constant intervals of time. You need to take the delta time (the difference in time) to determine how far to move something.
Lets say you want to move 5 pixels every 100ms and you try to update every 20ms.
Time for each Tick
tick 0: 19ms;
tick 1: 25ms;
tick 2: 35ms;
tick 3: 20ms;
Java Code:double standardTimeInterval = 100.0; //100ms double pos = 0.0; //position of your object double velocity = 5.0; long lastTime = 0; ... if(lastTime == 0) lastTime = System.currentTimeMillis(); double dt = (System.currentTimeMillis()-lastTime)/standardTimeInterval; lastTime = System.currentTimeMillis(); pos += velocity * dt;
Distance traveled after each tick
tick-1: 0 pixels
tick 0: 0.95 pixels
tick 1: 2.2 pixels
tick 2: 3.95 pixels
tick 3: 4.95 pixelsLast edited by mrmatt1111; 03-04-2010 at 09:15 PM.
My Hobby Project: LegacyClone
- 03-11-2010, 09:09 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Java Timer
By sasi25 in forum Advanced JavaReplies: 4Last Post: 01-30-2010, 09:19 AM -
Dice game issue- any Java gamblers ableto help?:P
By mambalamba in forum New To JavaReplies: 2Last Post: 12-17-2009, 06:49 PM -
Timer in java
By manhit45 in forum New To JavaReplies: 4Last Post: 12-14-2009, 02:27 PM -
[SOLVED] Swing Timer issue
By Doctor Cactus in forum New To JavaReplies: 6Last Post: 03-03-2009, 12:25 PM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks