Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  timebased movement problem  (Read 1310 times)
0 Members and 1 Guest are viewing this topic.
Offline zulo

Junior Duke




Java games rock!


« Posted 2005-08-01 13:54:25 »

hi

Im making a breakout clone for j2me midp2 phones and im having some trouble with timebased movement(i think thats the problem)..
Here is my moveBall method that should make the ball move 20 pixels pr second.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
private void moveBall(double elapsedTime) {
       
        float speed = 20; // adjust by experiment
       
        double bxmov = (speed * elapsedTime) / 1000;
        double bymov = (speed * elapsedTime) / 1000;
ut("bxmov double = "+bxmov);
        if (!ball.isDirectionXPositive()) {
            bxmov *= -1;
        }
        if (!ball.isDirectionYPositive()) {
            bymov *= -1;
        }
        //System.out.println("bxmov=" + bxmov);
        ball.setSpeed((int)bxmov, (int)bymov);
        wallCrashCheck();
        brickCrashCheck();
        //ball.setPosition(ball.getX() + bxmov, ball.getY() + bymov );
        ball.setPosition(ball.getX() + ball.speedX(), ball.getY()
                + ball.speedY());

    }


The sleep of the main loop is 20 milliseconds.
This gives different problems. Some times the bxmov is less then 1 or 0.67 etc. that makes the ball move 0 pixles on that frame. This works on the emulator actually, the ball moves quite slowly but ok,and it hits all the bricks ( its a breakout clone) and the collision code works properly.

 When i transfer the game to my mobile phone SE K750i it runs like crazy..the ball moves very fast and it just runs through the bricks. IS there something wrong with my timecode or should I just add to the collision detection so that it calculates a hit when the ball moves too far?

How can I make sure that if the game runs on nokia phone it wont act crazy because of different sleep times etc..even if i sleep 20 milliseconds, will it sleep 21 milliseconds on SE and 25 ms on nokia and 50 ms on the emulator?

Developing for j2me wasnt as easy as i thought. Lots of problems hehe Roll Eyes
If someone wants to look at the full source it is here:
http://www.vestheimonline.com/bricker2mesource.zip
Offline shmoove

Junior Duke




Doh!


« Reply #1 - Posted 2005-08-02 07:25:07 »

How are you calculating elapsedTime?

shmoove
Offline zulo

Junior Duke




Java games rock!


« Reply #2 - Posted 2005-08-02 09:24:15 »

elapsedTime is calculated in the tick() method:
 
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void tick() {

        long currentFrameTime = System.currentTimeMillis();

        //first time set it to 0
        if (lastFrameTime == 0) {
            lastFrameTime = currentFrameTime;
        }

        double elapsed = (currentFrameTime - lastFrameTime);
        //System.out.println("elapsed:" + elapsed);

        lastFrameTime = currentFrameTime;
       
        moveBall(elapsed);
        movePaddle(elapsed);
    }

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline shmoove

Junior Duke




Doh!


« Reply #3 - Posted 2005-08-02 10:25:24 »

My guess:
On the emulator most of the frames are so quick that the ball moves 0 pixels. Since you are not accumulating these small changes, all these frames actually do nothing to the ball, and only on the ocasional frame where the delta is big enough the ball actually moves.
On the phone, the time delta is big enough so that the ball actually moves on every frame, hence you get a lot faster movement.

A solution would be to make the ball's speed and position variables floating point as well, and only clamp them to an integer when you draw them on screen. This way, even on the frames where the movement is less than one, the actual position of the ball will be affected - you just won't see the difference on screen. But on the next frame, when you add another fraction, there will be a difference. This will probably make the phone and the emulator behave more consistently.

EDIT:
Here's an example of what I mean:
Frame 1 - ball is at 1.0. Movement is 0.25.
Frame 2 - ball is at 1.25. Movement is 0.3.
Frame 3 - ball is at 1.55. Movement is 0.35.
Frame 4 - ball is at 1.90. Movement is 0.2.
Frame 5 - ball is at 2.1 - Only now you actually see the "effect" of all the previous frames.
The way you are currently doing things, the movement would have been 0 in all these frames, so the ball would've stayed at 1.0.

You could also round instead of clamp to an integer and then the movement would be seen on Frame 3.

shmoove
Offline zulo

Junior Duke




Java games rock!


« Reply #4 - Posted 2005-08-02 12:51:00 »

Thank you that was a good idea, I have changed the position and speed variables to float. I think i have to create my own sprite class because i cant override j2me's getX() and then I would have to change all the calls in my code. But i think this float solution is going to work, thanks again Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (46 views)
2014-10-17 03:59:02

Norakomi (37 views)
2014-10-16 15:22:06

Norakomi (29 views)
2014-10-16 15:20:20

lcass (32 views)
2014-10-15 16:18:58

TehJavaDev (62 views)
2014-10-14 00:39:48

TehJavaDev (62 views)
2014-10-14 00:35:47

TehJavaDev (52 views)
2014-10-14 00:32:37

BurntPizza (70 views)
2014-10-11 23:24:42

BurntPizza (40 views)
2014-10-11 23:10:45

BurntPizza (82 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!