SFML: Assertion failed while setting points of a Convex shape

Prerequisite Checklist

Describe your issue here

What I see is this:

sfml-src/include/SFML/System/Vector2.inl:185: constexpr sf::Vector2<T> sf::operator/(Vector2<T>, T) [with T = float]: Assertion `right != 0 && “Vector2::operator/ cannot divide by 0”’ failed.

I Just tried to describe a convex shape that I wanted SFML to draw.

Your Environment

  • OS / distro / window manager: Linux 6.14.2
  • SFML version: 3.0.0
  • Compiler / toolchain: g++ (GCC) 14.2.1 20250207
  • Special compiler / CMake flags: N/A

Steps to reproduce

#include <SFML/Graphics.hpp>

int main(int argc, char *argv[])
{
    sf::RenderWindow window(sf::VideoMode({32, 23}, 32), "ConvexShape bug");
    sf::ConvexShape shape;
    shape.setOutlineThickness(1.f);
    shape.setPointCount(3);
    shape.setPoint(0, {2.f, 30.f});
    shape.setPoint(1, {29.f, 30.f});
    shape.setPoint(2, {29.f, 30.f});

    return 0;
}

Expected behavior

Expected behaviour is that the shape is drawn.

Actual behavior

Assertion at runtime. I elieve the shape I specified is perfectly valid - so I don’t see why I get an error.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20 (9 by maintainers)

Most upvoted comments

This is an issue with outlining a zero angle corner. It is related to #2727 and will be fixed with #2741.

Ok. I just tested with commit bd70ebc00597a060054bfc209a628fc8f4988600 from kimci86’s fork and that resolves my current issue. Builds, runs and doesn’t trigger the assert for me.

Here is an example that shows the division by zero on a standard use case.

sf::ConvexShape shape;
shape.setPointCount(4);
shape.setOutlineThickness(1);
shape.setPoint(0, {4, 0});  // Aborts due to division by zero

It fails while setting up a shape with area. This is because the points default-initialized to {0, 0} (coincident upon initialization). It also could fail (depending on floating point error) if a future setPoint() happens to make a shape where the normals between neighboring points are opposite.

I tested Kimci’s branch, and it provably avoids division by zero in all cases 👍. It might be a good idea to add a regression test.

@eXpl0it3r 😃 Good to see you are also still around. I know that dividing by 0 is bad - obviously. And I also know that my shape has zero area (or, in my real code; not quite zero area, but close enough). The thing is; I want to draw my shape regardless of its area - if it is zero, then nothing is drawn and that is fine. If I have to check for zero area and then not draw the shape, then I’m just duplicating the work done by SFML (for all my shapes) which seems wasteful. I wish that I could just pass a zero area shape to SFML which would pass it onto the GPU and then draw nothing. I see that there are two commits and I only tested with one. I’ll re-do my test with both applied and report back.

@jjuhl That commit is in my fork of SFML. You need to add my fork (https://github.com/kimci86/SFML.git) as a remote, fetch the remote, then you can checkout the commit.