samedi 29 août 2015

Box2D b2body segmentation fault

Some I'm trying to encapsulate an object with Box2D and SFML. It's supposed to just be a simple box. I'll expand on it to be adaptable to other shapes later on once I have the basics worked out. However, I have gotten stuck with a segmentation fault. I tried searching online for answers, but I'm still lost.

Here is where my boxes get called to be created. Nothing wrong appears to happen here.

main.cpp

if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
    int MouseX = sf::Mouse::getPosition(Window).x;
    int MouseY = sf::Mouse::getPosition(Window).y;
    Box box(&World, MouseX, MouseY);
    box.setTexture(BoxTexture);
    boxes.push_back(box);
}

Next this is the Box constructor. Still seems to run through this without an issue.

Box.cpp

Box::Box(b2World* World, int MouseX, int MouseY)
{

    Shape.SetAsBox((32.f/2)/SCALE, (32.f/2)/SCALE);
    BodyDef.position = b2Vec2(MouseX/SCALE, MouseY/SCALE);
    BodyDef.type = b2_dynamicBody;
    b2Body* Body = World->CreateBody(&BodyDef);
    FixtureDef.density = 1.f;
    FixtureDef.friction = 0.7f;
    FixtureDef.shape = &Shape;
    Body->CreateFixture(&FixtureDef);
}

Once a box is created and added to the vector, it's supposed to update the sprite attached.

main.cpp

for(int i = 0; i < boxes.size(); i++)
{
    boxes[i].update();
    Window.draw(boxes[i].getSprite());
}

The code for the update is where I receive the segmentation fault. It appears whenever I call the pointer for Body.

Box.cpp

void Box::update()
{
    sprite.setOrigin(16.f, 16.f);
    sprite.setPosition(SCALE * Body->GetPosition().x, SCALE * Body->GetPosition().y); //Segmentation Fault
    sprite.setRotation(Body->GetAngle() * 180/b2_pi); //Segmentation Fault
}

Here I'll provide the entire files in case you may need something I didn't provide to help me.

main.cpp: http://ift.tt/1KuAI5s

Box.h: http://ift.tt/1Jtu4dD

Box.cpp: http:// pastebin (dot) com/1y1cpwzq

sorry about that last one. Says I can only post 2 links

Aucun commentaire:

Enregistrer un commentaire