Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Game Mechanics / Re: Collision detection and Thread.sleep() on: 2013-07-26 21:17:32
Ok, I understand. Thank you very much for all your help and explanations! I have learned a lot in these two days Smiley I will have to look into fixed time step next then. Thanks again!!!
2  Game Development / Game Mechanics / Re: Collision detection and Thread.sleep() on: 2013-07-26 18:33:42
Yea, you are right again. if I change this:

1  
Thread.sleep( Math.abs(lastLoopTime-System.nanoTime() + OPTIMAL_TIME)/1000000 );


To for example:

1  
Thread.sleep( 10 );


then there is no warping, at least as far as I can see. Is it then better to use just a constant value instead of the first example of code?
3  Game Development / Game Mechanics / Re: Collision detection and Thread.sleep() on: 2013-07-26 18:17:27
I am actually casting them as int, because the function that I draw with doesn't take anything else as parameters:
1  
graphics.drawImage(img, (int)x, (int)y, null);
4  Game Development / Game Mechanics / Re: Collision detection and Thread.sleep() on: 2013-07-26 17:35:19
Yea, you're exactly right, I just needed to lower the yVelocity that was set in the constructor. Now it works, except for the minor warping glitch I mentioned. Thanks a lot for your help, and also the Bounce tip!!!
5  Game Development / Game Mechanics / Re: Collision detection and Thread.sleep() on: 2013-07-26 17:06:57
When I lower my gravity, the height of the jump increases. with a gravity less than 1 the character jumps out of the screen Sad
6  Game Development / Game Mechanics / Re: Collision detection and Thread.sleep() on: 2013-07-26 16:40:11
gravity = -9.81

If I make the changes that you proposed and decrease/increase gravity, the character still jumps faster than I need and most of the time through the platforms.
7  Game Development / Game Mechanics / Re: Collision detection and Thread.sleep() on: 2013-07-26 15:52:00
Well, I figured that I need the time of the update/frame, so I used optimal time for each frame. If I remove that piece "time/100000000", and try your solution, the character starts jumping super super fast and also jumps through platforms. So I don't really know how to fix that if I remove "time/100000000".

Line 3 means that when the character comes down from a jump, he jumps back up with same velocity as he came down, eg. character is constantly jumping. Velocity only gradually slows down and gets near zero towards the peak of the jump.
8  Game Development / Game Mechanics / Re: Collision detection and Thread.sleep() on: 2013-07-26 14:33:20
So, I've updated my code and it seems to be working, eg. character is now always hitting the platforms when on his way down (except for a minor glitch). Could someone please comment whether this is any better/good?

The minor glitch is that I see my character warp a few pixels up/down, depending on which way he is jumping, every few seconds.

Game class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
public void run(){
        long lastLoopTime = System.nanoTime();
        final int TARGET_FPS = 60;
        final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;      
       
        while (gameRunning)
        {
            long now = System.nanoTime();
            long updateLength = now - lastLoopTime;
            lastLoopTime = now;
            double delta = updateLength / ((double)OPTIMAL_TIME);
            doGameUpdates(delta, OPTIMAL_TIME);
            repaint();

            try{
                Thread.sleep( Math.abs(lastLoopTime-System.nanoTime() + OPTIMAL_TIME)/1000000 );
              } catch (InterruptedException e) {
                      e.printStackTrace();
              }
        }
}


private void doGameUpdates(double delta, long OPTIMAL_TIME){
       
        for(Platform p : plats){
            if(player.collidesWithPlatform(p)== true && !player.getIfJumping()){
                player.setFloor(p.getY()-player.getHeight());
                break;
            }
            else {
                player.setFloor(500);
            }
        }
       
        player.Move(delta, OPTIMAL_TIME);
}  
   


Excerpt of Player class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
public void Move(double delta, long time) {
...

if(y >= floor){
            jumping = true;
            yVelocity = yVelocity * (-1);
            y -= yVelocity * delta * time/1000000000;
        } else{
            yVelocity += gravity;
            y -= yVelocity * delta * time/1000000000;
           
            if(yVelocity <= 0){
                jumping = false;
            }
        }
...
}

public boolean collidesWithPlatform(Platform platform){
       
        if( ((int)this.getY() + this.getHeight()) <= (platform.getY() + 10)
                && (int)this.getX() + this.getWidth() >= platform.getX()
                && (int)this.getX() <= (platform.getX() + platform.getWidth()) ){
            return true;
        }
        return false;
}
9  Game Development / Game Mechanics / Re: Collision detection and Thread.sleep() on: 2013-07-25 18:28:37
I did look at that and tried to implement the variable step, but simplified it, since I thought that it would've been good enough for my simple game. I guess I will try again. Thanks for the tip!
10  Game Development / Game Mechanics / Collision detection and Thread.sleep() on: 2013-07-25 17:40:27
I\m trying to create a simple platformer and trying to do collision detection with platforms. Also using the following gameloop "run()". Unfortunately when I try to match player (x, y) coordinates with platforms (x, y) coordinates, because of Thread.sleep(10) (I think, but not sure.), the character skips it like 9/10 times and no collision happens.

If I remove Thread.sleep(), collision happens accurately, but when I move my character, he starts moving with immense speed. Could anyone please advise how to account for this?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
public void run(){
        long previous = 0, start = 0;
        double delta = 1;
       
         while(true){
            start = System.nanoTime();
           
            if(previous != 0){
                delta = start - previous;
            }  
           
            doGameUpdates(delta);
           
           
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           
           
            previous = start;
            repaint();
        }        
    }

private void doGameUpdates(double delta){
       
       
        for(Platform p : plats){
            if(player.collidesWithPlatform(p)==true){
               //Do something
            }
        }
        player.Move(delta);
       
    }
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

xFryIx (55 views)
2014-11-13 12:34:49

digdugdiggy (34 views)
2014-11-12 21:11:50

digdugdiggy (29 views)
2014-11-12 21:10:15

digdugdiggy (23 views)
2014-11-12 21:09:33

kovacsa (46 views)
2014-11-07 19:57:14

TehJavaDev (50 views)
2014-11-03 22:04:50

BurntPizza (49 views)
2014-11-03 18:54:52

moogie (65 views)
2014-11-03 06:22:04

CopyableCougar4 (63 views)
2014-11-01 23:36:41

DarkCart (148 views)
2014-11-01 14:51:03
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!