Search code examples
rustsfml

Convex Shapes vertices are not accurate SFML rust


So I have been using the Convex Shape struct to create triangles using the rust bindings for sfml.

My code is as follows:

let mut shape2 = ConvexShape::new(3);
shape2.set_point(0, Vector2f::new(0.0, 0.0));
shape2.set_point(1, Vector2f::new(0.0, 100.0));
shape2.set_point(2, Vector2f::new(100.0, 0.0));

for point in shape2.points() {
    println!("x:{} y:{}", point.x, point.y);
}

However, when looping through the points for the triangles sometimes I would get output like this:

x:0 y:100
x:100 y:0
x:434583.44 y:-0.000000000000000000000000000000000000000047353

I am not sure what is causing this problem; however, I assume it has to do with the f32 overflowing. Are there any fixes for this problem or am I doing something wrong here?


Solution

  • This appears to be a bug in the iterator returned by .points(). You can resort to fetching directly by index instead (accessed via the Shape trait):

    use sfml::graphics::{ConvexShape, Shape};
    use sfml::system::Vector2f;
    
    ...
    
    let mut shape2 = ConvexShape::new(3);
    shape2.set_point(0, Vector2f::new(0.0, 0.0));
    shape2.set_point(1, Vector2f::new(0.0, 100.0));
    shape2.set_point(2, Vector2f::new(100.0, 0.0));
    
    for i in 0..shape2.point_count() {
        let point = shape2.point(i);
        println!("x:{} y:{}", point.x, point.y);
    }