Search code examples
c++box2dgame-enginesfml

Assertion failed in Shape.SetAsBox()?


So, I'm trying to make a Human entity that is associated with a SFML sprite, and a Box2D body and fixture. I get this error, however:

Assertion failed: area > 1.19209289550781250000e-7F, file ...b2PolygonShape.cpp, line 352

So I did some quick googling and found that my problem lies within my shape making incorrect vertices. But I don't understand how that is possible if I'm using the Shape.SetAsBox() function...

Here is my code:

#include "../../include/entity/Human.hpp"

Human::Human(unsigned int id, b2World& world, sf::Vector2f pos, sf::Texture* texture)
    : AnimatedEntity(id, world, pos, sf::Vector2i(32, 32), texture)
{
    b2PolygonShape shape;
    shape.SetAsBox((32/2)/SCALE, (32/2)/SCALE);     // Set the size; Box2D takes the half-width/height as params, and then scale.

    b2FixtureDef fixtureDef;
    fixtureDef.density = .8f;
    fixtureDef.friction = .4f;
    fixtureDef.restitution = .2f;
    fixtureDef.shape = &shape;

    e_body->CreateFixture(&fixtureDef);     // Assuming: shape and density are set
}

FTR, SCALE is a static const int with a value of 30 (30px/1m). What's wrong?


Solution

  • I think what billz means is:

    (32/2) = 16
    16 / 30 = 0
    

    You just need to use a float for the SCALE instead of an int...