I'm working on a 2d top-down shooter game. At the time, I am trying to get the movement and collision right, but I have a few problems with this. First of all, I am making my game frame independent with delta time but that makes it harder for me to implement a collision system.
For example, I am checking collisions like this(java and libgdx):
public boolean collision(Rectangle collision) {
this.setX(this.getX() + this.delta.x);
this.AABB.x = this.getX();
if (this.AABB.overlaps(ext))
{
this.setX(this.getX() - this.delta.x);
this.AABB.x = this.getX();
collided = true;
}
// The same for Y
this.setY(this.getY() + this.delta.y);
AABB.y = this.getY();
if (AABB.overlaps(ext))
{
this.setY(this.getY() - this.delta.y);
AABB.y = this.getY();
collided = true;
}
if (collided)
System.out.println("Collided, setting X to " + this.getX() + " and Y to " + this.getY());
return collided;
}
Basically, I move the character on X, if there is a collision, I move it back to where it was before, and do the same for Y.The problem with this approach is that as the movement is frame independent, every frame delta.x and delta.y will be different, so when moving the player back to where it was before colliding will end up in different results.
this.setX(this.getX() + this.delta.x); // delta.x is a little bit different every frame because of delta time.
this.AABB.x = this.getX();
if (this.AABB.overlaps(ext))
{
this.setX(this.getX() - this.delta.x); // So this will also bring me back to a different location every frame.
this.AABB.x = this.getX();
collided = true;
How can I deal with this(or is this acceptable, as it is almost invisible)?
Any help apreciated, thanks!