Box2D-SFML问题

时间:2016-05-31 11:18:12

标签: c++ box2d sfml

我目前正在使用SFML中的第一个平台游戏并试图实现Box2D。我已经创建了一个TileMap-Loader(Tiled)来将我的地图加载到sfml中。在那里,我为Box2D对象创建了对象层(一层用于动态,一层用于静态,一层用于运动(如果必要))。每个对象都从其Tiled-object-properties中获取其坐标和大小。

所以,我正在检查我的TileMap-Loader,如果特定对象有一个Box2d物理状态(动态等)。如果是这样,我在我的Tile-Class中实例化一个Box2D-Object,用#define将我的像素转换为米。

我使用box2d debug-draw来显示我的形状所在的位置,雷达的某处。

以下是我得出的结果:

Here is what i get drawn as result 我有点乱,但我的地图显示正确......在实施我的box2d-try之前。必须有一些我看不到的东西,尝试几个小时来解决问题,但没有管理。所以希望你们能给我一些提示。

这是我的Tile-和我的Box2D-Class

瓷砖

#include "Tile.hpp"
#include "Globals.hpp"
#include <iostream>

// INSTRUCTIONS!!!!

// ---------------------------------------------
// --> create tile with pixel boundaries
// --> Init box2d, if m_Collision == "true"
// -> update box2d in game-loop
// -> use render() to draw the sprite on window
// ---------------------------------------------

#define BOX2D_CONVERSION 128.f

// calling BoxObject constructor with arguments
// all floats should be passed in meters
Tile::Tile(float positionX, float positionY, float width, float height)
{
    m_b2Position.x = positionX;
    m_b2Position.y = positionY;
    m_b2Dimension.x = width;
    m_b2Dimension.y = height;
    m_Collision = false;
}

Tile::Tile()
{
    m_b2Position.x = 800.f;
    m_b2Position.y = 0;
    m_b2Dimension.x = 64.f;
    m_b2Dimension.y = 64.f;
    m_Collision = false;
}


Tile::~Tile()
{
}
//----------------------------------

// load sprite textures
void Tile::loadTexture(std::string id, std::string filePath)
{
    m_filePath = id;
    // loads texture from filePath and puts it into TextureManager with key = id
    g_pTextureManager->AddTexture(id, filePath);
    std::cout << "TileLoader added Texture with id " << id << std::endl;

    // get texture and create tile as sprite for beeing rendered
    const sf::Texture& texture = g_pTextureManager->GetTexture(id);
    m_sprite.setTexture(texture);
}
//----------------------------------

// if m_Collision is true init Box2D!
void Tile::InitBox2d()
{
    float boxPositionX = m_b2Position.x / BOX2D_CONVERSION;
    float boxPositionY = m_b2Position.y / BOX2D_CONVERSION;
    float boxWidth = m_b2Dimension.x / BOX2D_CONVERSION;
    float boxHeight = m_b2Dimension.y / BOX2D_CONVERSION;
    m_Box2d = BoxObject(boxPositionX, boxPositionY, boxWidth, boxHeight, m_Type);
}
//----------------------------------

// set collision-type
void Tile::setType(BODYTYPE type)
{
    m_Type = type;
}
//----------------------------------

// get updated Box2d coordinates and rotation 
// convert them into coordinates to display sprites properly
void Tile::updateBox2d()
{
    m_Box2d.update();
    m_b2Position.x = m_Box2d.getPosition().x * BOX2D_CONVERSION;
    m_b2Position.y = m_Box2d.getPosition().y * BOX2D_CONVERSION;
    m_b2Dimension.x = m_Box2d.getDimension().x * BOX2D_CONVERSION;
    m_b2Dimension.y = m_Box2d.getDimension().y * BOX2D_CONVERSION;
    m_angle = m_Box2d.getAngle();
}
//----------------------------------

// set collision to "true" if object should be handled as box2d-object
void Tile::setCollision(bool collision)
{
    m_Collision = collision;
}

// render sprite 
sf::Sprite Tile::render()
{
    m_sprite.setOrigin(m_b2Dimension.x / 2.f, m_b2Dimension.y / 2.f);
    m_sprite.setPosition(m_b2Position.x, m_b2Position.y);
    std::cout << "RenderPos: " << m_b2Position.x << ", " << m_b2Position.y << std::endl;
    std::cout << "RenderBox2DPos: " << m_Box2d.getPosition().x << " ," << m_Box2d.getPosition().y << std::endl;
    std::cout << "ID: " << m_filePath << std::endl;
    m_sprite.setRotation(m_angle);
    return m_sprite;
}
//----------------------------------

BoxObject

#include "BoxObject.hpp"
#include "World.hpp"

#define DEGTORAD 0.0174532925199432957f
#define RADTODEG 57.295779513082320876f

// constructor without parameters
BoxObject::BoxObject()
{
    // set position and dimension
    b2Vec2 m_b2Position(5, 5);
    b2Vec2 m_b2Dimension(1, 1);

    // init box2d object
    Init(BODYTYPE::staticBody);
}
// ----------------------------

// constructor with parameters
BoxObject::BoxObject(float positionX, float positionY, float width, float height, BODYTYPE type)
{
    // set position
    m_b2Position.x = positionX;
    m_b2Position.y = positionY;

    // set dimension
    m_b2Dimension.x = width;
    m_b2Dimension.y = height;

    // init box2d with type
    Init(type);
}
// ----------------------------

// return box2D-position
b2Vec2 BoxObject::getPosition()
{
    return m_b2Position;
}
// ----------------------------

// set box2d position
void BoxObject::setPosition(b2Vec2 position)
{
    m_b2Position = position;
}
// ----------------------------

// getbox2d dimension
b2Vec2 BoxObject::getDimension()
{
    return m_b2Dimension;
}
// ----------------------------

// get angle
float BoxObject::getAngle()
{
    return m_angle;
}
// ----------------------------

// update physics
void BoxObject::update()
{
    m_b2Position = m_body->GetPosition();
    m_angle = m_body->GetAngle();
}
// ----------------------------

// destructor
BoxObject::~BoxObject()
{
}
// ----------------------------

// initialization of box2d
void BoxObject::Init(BODYTYPE type)
{
    // set body definition
    b2BodyDef bodyDef;

    // switch bodyDef.type on type
    switch (type)
    {
    case BODYTYPE::dynamicBody:
        bodyDef.type = b2_dynamicBody;
        break;
    case BODYTYPE::kinematicBody:
        bodyDef.type = b2_kinematicBody;
        break;
    default:
        bodyDef.type = b2_staticBody;
        break;
    }

    // save body in world
    m_body = g_pWorld->getWorld()->CreateBody(&bodyDef);
    m_body->SetTransform(m_b2Position, 0 * DEGTORAD);

    // create shape with dimensions
    b2PolygonShape bodyBox;
    bodyBox.SetAsBox(m_b2Dimension.x / 2.f, m_b2Dimension.y / 2.f);

    // create fixture
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &bodyBox;
    fixtureDef.density = 1.f;
    fixtureDef.friction = 0.3f;

    // save fixture in body
    m_body->CreateFixture(&fixtureDef);
}
// ----------------------------

如果您需要任何其他文件,我会添加它们。我只想到这些界限之间的某个地方是我看不见的错误。

编辑:自己修复了“问题”,昨天累得意识到,调试工具只是我场景的“地图”。现在box2d工作正常,只需要修复我的对象定位。

0 个答案:

没有答案