1
\$\begingroup\$

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!

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

What you want to do is determine how much the two AABBs overlap and then move them apart based on that amount.

You already have an overlaps method. If you tweak that to return the amount that the AABBs overlap (on each axis) then you can just move your player back by that amount. That assumes that what you're colliding with doesn't need to be pushed back either. If that IS the case then you'll have to move both AABBs back by some ratio of the overlapping value. You can probably just get away with moving both AABBs back by half of the overlapping value but a more generic solution would be to move them back based on their speed so a faster object moves back more than a slower object.

\$\endgroup\$
2
  • \$\begingroup\$ Actually, the overlaps method is in the framework. But I can do it in another method. Thanks so much for the response, I will definetly try it out, I'll let you know if it works, thanks! \$\endgroup\$
    – twkmz
    Commented Aug 13, 2015 at 3:50
  • \$\begingroup\$ it works perfectly, even with a lot of tiles colliding at the same time, thanks \$\endgroup\$
    – twkmz
    Commented Aug 13, 2015 at 17:19
1
\$\begingroup\$

I think if it is invisible and if it is not affecting gameplay it is acceptable. But if you want a solution you can set a flag when starting collision detection, when the flag is true don't move your objects and store how amount they should move. If a collision happens extract how much it should go back from stored value and apply the stored value then set your flag false again.

I hope you understand it and I hope you asked that too.

\$\endgroup\$
1
\$\begingroup\$

Use of delta is to smooth your animation or as you said "making it independent of frames".

So may be separating animation with the position calculation can solve your problem.

For example, First calculate your next position in separate method then show your animation, by adding delta to initial position to final position, without loosing the initial position. And if you detect any collision, simple traverse back or swap initial and final position, by adding delta.

It's just a logic, I have to look into your code to find out how to incorporate this in your code.

One of my favorite blog which motivated me towards game development. Here is his views on your problem, hope it will help to resolve your problem.

http://obviam.net/index.php/getting-started-in-android-game-development-with-libgdx-tutorial-part-4-collision-detection/

\$\endgroup\$
2
  • \$\begingroup\$ "making it independent of frames", well, I meant, making it independent of the frame rate. But you got the idea ;) \$\endgroup\$
    – twkmz
    Commented Aug 13, 2015 at 18:00
  • \$\begingroup\$ Read that link, in my answer. He has explained collision detection , keeping "independent of frame rate :D", in mind very thoroughly. If you achieve/resolve this, you can update that code as answer here. May be in future, I stumbled upon it while trying to detect collision in my game :) \$\endgroup\$
    – kingAm
    Commented Aug 14, 2015 at 10:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .