For my game the character can land on top of platforms but can’t collide left and right. My if statement for touching
the left of a rectangle doesn’t do anything. Here’s code:
foreach (player player in Players){
foreach (platform platform in Platforms){
if (player.hitbox.isOnLeft(platform.hitbox)){
player.cantmoveright = true;
}
}
the boolcantmoveright works for sure. i even checked in the draw method by drawing string
What do you mean exactly? That it is always false?
You have a very small margin there, if the player moves more than 2 pixels per frame he could easily shoot through the obstacles. Another note, this checks if r1 is on the right of r2, rather than on the left.
You should use a speed variable and use time to move, or you player will be as fast as the game can go and collisions will fail sometimes. And maybe if the player can go left use a negative speed.
And you should replace your prediction statement with this value to be sure to be ahead of the next move.
As jjagg said, your code is a little confuse.
And also the problem isn’t that the collision is failing. It’s that it’s missing it. For example, when I run right into the left of a platform, no collision detection. But, if I jump into it a certain way so that the bottom of the player hitbox is between the top and bottom of the platform, then bam, collision detection.
Will give you the time that passed in seconds, so of you have a velocity and every frame do
Position += dt*Velocity;
The unit of that velocity will essentially be pixels/second. If you don’t divide by the time between updates like Mattlekim said, you can think in terms of time rather than number of updates.
You should store floats (or vector2 in this case) and round only when you need to. If you round after every recalculation movement may look a bit jittery at low speeds.