2
votes

I am currently trying to replicate frogger and am struggling with the collision detection in relation to both the water and the logs.

I can have it so if the frog jumps on the log it is carried with the log, and I can also have the frog respawn if it collides with the water, but I can't have both. When I try it just takes the water collision over anything else.

This may be because the water is one large rectangle with the logs on top being smaller rectangles but the size of logs and the movement make it so the frog sits flush on the log.

for (size_t i = 0; i < viLogS.size(); i++)
        {
            if (viLogS[i].getGlobalBounds().intersects(player.getGlobalBounds()))
            {
                player.move(-2, 0); //if frog and log intersect move frog along same axis and speed as log 
            }
            else if (waterzone.waterRectangle.getGlobalBounds().intersects(player.getGlobalBounds()))
            {
                player.FrogRespawn();
            }
        }
2
I guess the problem is that it's a loop. Maybe try to split it up into two loops, because any log you don't intersect with will automatically give you the else case -> water collision. So it seems to me at least. - Beko
I second Beko's comment, your problem appears to be that if you have at least two logs, the frog would need to collide with both logs for it not to respawn. You should (e.g.) only check for collisions with the logs in the loop and update a boolean variable which is true iff the frog doesn't collide with any log, then respawn if that boolean variable is true and the frog collide with the water. - Caninonos
Yes, you are right, I can jump on the first log but as soon as the second log spawns it jumps to the water collision statement. - M.Deacon
does the intersect method return a reference to a intersection rectangle (in this case it always returns true, also for an empty rectangle ---you have to check this) or is a predicate, in the sense that it says true or false if it intersects. Have the rectangle class a boolean cast that maps allows you to check for emptyness? - Luis Colorado

2 Answers

1
votes

Your logic is slightly flawed. Sure if the first section of the if statement fires then the second won't have an opportunity to activate. However you run that snippet for every single log. Unless your frog intersects with every single log then it is going to respawn every single time.

what you need is something like:

if (waterzone.waterRectangle.getGlobalBounds().intersects(player.getGlobalBounds()))
{
    bool safe = false;
    for (size_t i = 0; i < viLogS.size(); i++)
    {
        if (viLogS[i].getGlobalBounds().intersects(player.getGlobalBounds()))
        {
            player.move(-2, 0); //if frog and log intersect move frog along same axis and speed as log
            safe = true;
        }
    }

    if (!safe)
    {
        player.FrogRespawn();
    }
}

This checks if you are in the water area (I'm assuming you have land areas with land dangers as well, if not this can be simplified) and if you intersect with just 1 log then you are declared safe. If not, splash.

0
votes

you don't show how you compute the collisions, but the most probable thing is that if the frog is colliding with both (that is an option) you always consider it has collided only with the log. At least that's what you say in the small snippet of code you've posted.