-
Hey. Please help me solve the problem!
I'm using VS C++ 10 connect the library SFML and Box2D..
Where the red arrow character can not pass. Map was created in Tiled Map Editor.
Error visible on screen shotah...
-
It looks like you're using an early version of this (http://en.sfml-dev.org/forums/index.php?topic=3023.0) to load your map. You could try the most recent version (https://github.com/fallahn/sfml-tmxloader), which has built in support for box2D.
-
The new version is not working... To Install VS11. We used all library. When running a Benchmark example there are errors.
MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
1>MapLoaderPrivate.obj : error LNK2019: unresolved external symbol _inflate referenced in function "private: bool __thiscall tmx::MapLoader::m_Decompress(char const *,class std::vector<unsigned char,class std::allocator<unsigned char> > &,int,int)" (?m_Decompress@MapLoader@tmx@@AAE_NPBDAAV?$vector@EV?$allocator@E@std@@@std@@HH@Z)
1>MapLoaderPrivate.obj : error LNK2019: unresolved external symbol _inflateEnd referenced in function "private: bool __thiscall tmx::MapLoader::m_Decompress(char const *,class std::vector<unsigned char,class std::allocator<unsigned char> > &,int,int)" (?m_Decompress@MapLoader@tmx@@AAE_NPBDAAV?$vector@EV?$allocator@E@std@@@std@@HH@Z)
1>MapLoaderPrivate.obj : error LNK2019: unresolved external symbol _inflateInit2_ referenced in function "private: bool __thiscall tmx::MapLoader::m_Decompress(char const *,class std::vector<unsigned char,class std::allocator<unsigned char> > &,int,int)" (?m_Decompress@MapLoader@tmx@@AAE_NPBDAAV?$vector@EV?$allocator@E@std@@@std@@HH@Z)
1>D:\Game\SFML Game\DarkRed\Test\Debug\Test.exe : fatal error LNK1120: 3 unresolved externals
-
You need to link the zlib library.
-
Please post a minimal and complete example code (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368) if you want more help with your code for all the reasons mentioned in the link.
-
Thank you. Examples are to work. But I don't understand how using the new version to create to get to work with a simple map. Need to fell cube (Name - Bl). I attached map. Help please.
-
I created object. But drawn with distortion. What's wrong?
int main()
{
sf::View view;
view.reset(sf::FloatRect(0.0f, 0.0f, 1280u, 640u));
view.setViewport(sf::FloatRect(0.0f, 0.0f, 2.0f, 2.0f));
renderWindow.setView(view);
tmx::MapLoader ml("maps/");
ml.Load("platformer.tmx");
//create a box2D world
b2World world(tmx::SfToBoxVec(sf::Vector2f(0.f, 200.f)));
sf::Sprite dynamicCube;
sf::Texture tileSet;
tileSet.loadFromFile("maps/box.png");
dynamicCube.setTexture(tileSet);
sf::FloatRect rect1;
rect1 = sf::FloatRect(0,0,32,32);
dynamicCube.setTextureRect(sf::IntRect(rect1.left, rect1.top,rect1.height,rect1.width));
dynamicCube.setOrigin(rect1.height/2,rect1.width/2);
b2Body* b;
const std::vector<tmx::MapLayer>& layers = ml.GetLayers();
for (const auto& l : layers)
{
if (l.name == "Stat")
{
for (const auto& o : l.objects)
{
b2Body* b1 = tmx::BodyCreator::Add(o, world);
}
}
else if (l.name == "Din")
{
for (const auto& o : l.objects)
{
b = tmx::BodyCreator::Add(o, world, b2BodyType::b2_dynamicBody);
}
}
}
//-----------------------------------//
while(renderWindow.isOpen())
{
//poll input
sf::Event event;
while(renderWindow.pollEvent(event))
{
if (event.type == sf::Event::Closed)
renderWindow.close();
}
//update
world.Step(1.0f / 60.0f, 6, 3);
sf::Vector2f pos = tmx::BoxToSfVec(b->GetPosition());
dynamicCube.setPosition(pos.x,pos.y);
dynamicCube.setRotation(tmx::BoxToSfAngle(b->GetAngle()));
renderWindow.clear();
renderWindow.draw(ml);
renderWindow.draw(dynamicCube);
renderWindow.display();
}
return 0;
}
(http://anrobot.ucoz.ru/zastavka/133.png)
-
It looks like it has rotated slightly, try commenting out
dynamicCube.setRotation(tmx::BoxToSfAngle(b->GetAngle()));
or outputting the angle to the console, to check if it is non-zero
-
If comment
dynamicCube.setRotation(tmx::BoxToSfAngle(b->GetAngle()));
then everything works fine. But right turn Cuba!
That showed that the console when a block fell on a flat surface!
std::cout << "dynamicCube.setRotation = " << dynamicCube.getRotation() << " : b->GetAngle() =" << tmx::BoxToSfAngle(b->GetAngle()) << std::endl;
dynamicCube.setRotation = 271.065 : b->GetAngle() = 271.065
If you change the height of the fall of the cube, the changing angles in the console.
-
It looks like rounding error introduced by box2D and the tmx conversion functions. If this is just a normal physics object in the world, you'll probably have to put up with it (because that's just how box2D works) unless you want to add some sort of correction to a defined tolerance:
float rotation = tmx:BoxToSfAngle(b->GetAngle())
if(b->isSleeping()) //only test the body when resting
{
const float tolerance = 2.f
if(rotation > 270.f - tolerance && rotation < 270.f + tolerance)
rotation = 270.f;
//repeat for 0, 90,180
}
dynamicCube.setRotation(rotation);
Alternatively if the box is to represent a player then you can disable rotation on box2D bodies altogether:
b2BodyDef bd;
bd.FixedRotation = true;
-
Thank you! I did so!
dynamicCube.setRotation(tmx::BoxToSfAngle(b->GetAngle() , b->IsSleepingAllowed()));
float tmx::BoxToSfAngle(float rads , bool Sleep)
{
float rotation = -rads * 57.295779513082320876f;
if(Sleep) //only test the body when resting
{
const float tolerance = 2.f;
if(rotation > 270.f - tolerance && rotation < 270.f + tolerance)
rotation = 270.f;
else if(rotation > 180.f - tolerance && rotation < 180.f + tolerance)
rotation = 180.f;
else if(rotation > 90.f - tolerance && rotation < 90.f + tolerance)
rotation = 90.f;
else if(rotation > 0.f - tolerance && rotation < 0.f + tolerance)
rotation = 0.f;
}
return rotation;
}