So I just changed my game update function to update alle movements depending on the difference between the actual FPS and the fixed game FPS.
For example I set my game to a fixed 60 FPS.
If the actual FPS is 20 I calculate the difference like that:
((20 * 100) / 60) / 100 which would result in 0.333
So the difference of 1 game loop would be 1 - 0.333 = 0.677
Thus all game movements wouldn’t be updated based on a normal game loop:
x += speedX * 1 but width an increased value: x += speedX * 1.677
So now my game runs at the same speed on every system, just with less pictures, but thats not the problem.
I’m not entirely sure if that causes the collisoin detection to fail, but I guess as the game speed gets increased to match the current fps, the collision detection isn’t able to track all events anymore because positions get skipped. It will only check for collision around the player.
How do I manage this problem ?
Edit:
Here is an example of the collision detection:
var xi = this._game.level.getField(this.x);
var yi = this._game.level.getField(this.y);
var left = this._game.level.levelData["col"][(xi-1) + "," + yi] ? (xi-1) * 32 : null;
var right = this._game.level.levelData["col"][(xi+1) + "," + yi] ? (xi+1) * 32 : null;
var top = this._game.level.levelData["col"][xi + "," + (yi-1)] ? (yi-1) * 32 : null;
var bottom = this._game.level.levelData["col"][xi + "," + (yi+1)] ? (yi+1) * 32 : null;
// Will hit ground ?
if (this.y + this.height >= bottom && bottom != null) {
this.ground = true;
if (!this.jumping) this.y = bottom - this.height;
}
// Left
if (this.x <= left + 32 && left != null) {
this.x = left + 32;
}
// Right
if (this.x + this.width >= right && right != null) {
this.x = right - this.width;
}
this is the player.
and left/right are the collision tile positions on the grid.
Each tile is 32×32 and the player is 25×25.
Here is the full game: [removed]
Edit: The problem is actually somewhere else since bottom is not null.
When I put the player at bottom - (this.height + 1) instead of bottom - this.height (1 pixel higher), he doesn’t allways fall through.
Im on my phone right now so I can’t check out your sample. So if I am reading your collision detection stuff right, then you update the players position and then check for a collision. This, if the frame rate is low enough or the object is moving fast enough, means that an object can “phase” right through a barrier. Being entirely on one side of it before the frame update and being entirely on the opposite side of it after the frame update.
There are a couple ways to address this problem, the most direct is to not only test where the object is now, but where it’s been as well while it moved. Implement a collision detection algorithm that detects all the zones that a player or object has to pass through to get from a to b and base your collision on that.
Also checkout gamedev.stackexchange.com. It’s usually a good place to ask game specific questions.