SFML community forums

Help => Graphics => Topic started by: amited on September 10, 2018, 11:13:34 pm

Title: Making SAT for rotation shape
Post by: amited on September 10, 2018, 11:13:34 pm
Yo. Im writting a game in which player is shotting with computer bots. I have done movement player and collisions with "right" set wall rotation(0,90,180,270,360). But if change rotation of wall then algorithm for SAT doesnt work good like this on ss:
Work good:
(https://i.imgur.com/8iQnWsY.png)
And dont work good:
(https://i.imgur.com/KMCfz2p.png)

So if you saw, if i changed rotation then sat doesnt works good ;/ This algorithm isnt main. I get this form this film: https://www.youtube.com/watch?v=7WM09bWwqOU
and code looks like this:
bool Player::Sat(const Sprite &sp1, const Sprite &sp2, Vector2f *out_mtv)
{               const FloatRect &rectSp1 = sp1.getGlobalBounds();
                const FloatRect &rectSp2 = sp2.getGlobalBounds();
                float proj_x, proj_y, overlap_x, overlap_y;

                // test overlap in x axis
                proj_x = max(rectSp1.left + rectSp1.width, rectSp2.left + rectSp2.width) - min(rectSp1.left, rectSp2.left);
                if (proj_x < rectSp1.width + rectSp2.width) {
                        if (out_mtv) {
                                // calculate mtv in x
                                overlap_x = rectSp1.width + rectSp2.width - proj_x;
                        }
                        // test overlap in y axis
                        proj_y = max(rectSp1.top + rectSp1.height, rectSp2.top + rectSp2.height) - min(rectSp1.top, rectSp2.top);
                        if (proj_y < rectSp1.height + rectSp2.height)
                        {
                                if (out_mtv)
                                {
                                        // calculate mtv in y
                                        overlap_y = rectSp1.height + rectSp2.height - proj_y;
                                        out_mtv->x = out_mtv->y = 0;

                                        // choose minimun overlap
                                        if (overlap_x < overlap_y) {
                                                out_mtv->x = overlap_x * (rectSp1.left < rectSp2.left ? -1 : 1);
                                        }
                                        else {
                                                out_mtv->y = overlap_y * (rectSp1.top < rectSp2.top ? -1 : 1);
                                        }
                                }
                                return true;
                        }
                }
                return false;
}
 
Im writing in sfml like 5 days and sat algorithm is too difficult ;/ Can you give me hint how i have change this code to get the perfect sat algorithm? I know i have to use .getTransform but its a bit difficult for me ;d
Title: Re: Making SAT for rotation shape
Post by: Hapax on September 12, 2018, 08:01:11 pm
Maybe you'll find use of this:
https://github.com/SFML/SFML/wiki/Source%3A-Rectangular-Boundary-Collision

If you don't want to use it directly, you could adapt the code for what you need but the SAT is all there. Note, though, that this SAT implementation is customised to only work with rectangular shapes.
Title: Re: Making SAT for rotation shape
Post by: amited on September 12, 2018, 10:05:40 pm
Why i dont saw your code ;// Thanks you!