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?
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);
}