简体   繁体   中英

Collision function in c++(SDL2). How do I pass two structs stored in separate vectors?

I'm making a duplicate of breakout. The ball is a struct stored in a vector just like the Enemy struct. The tutorial( http://headerphile.com/sdl2/sdl2-part-5-collision-detection/ ) I'm following only gives the example of checking it with a player rectangle. I'm wondering if this can be modified to my requirements by checking the collision with the ball struct.

Any help is appreciated.

Cheers.

bool CheckEnemyCollisions()
{
    for (const auto &p : enemies)
    {
        if (CheckCollision(p.pos, playerPos))
            return true;
    }

    return false;
}

Problem solved. Adding two rectangles from a struct into a function:

bool CheckEnemyCollisions()
{
    for (const auto &p : enemies)
    {
        for (const auto &c : ball)

        if (CheckCollision(p.pos, c.pos ))
            return true;
    }

   return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM