SFML community forums

Help => General => Topic started by: Spode87 on August 31, 2016, 09:54:29 am

Title: Problems with Box2D and sfml-tmxloader
Post by: Spode87 on August 31, 2016, 09:54:29 am
Hello everyone!  :D
I decided to write a simple platformer to learn SFML, and I found a library which parses a .tmx file format and creates Box2D bodies automatically. I created a player body, but somehow this body teleports out of the map, and if I comment the line which sets view's center on player, it shows my map without the player.
What should I do? And is this the right place to ask?

Source on Bitbucket: https://bitbucket.org/ilyam9/first-game/src (the code is in main.cpp, I hope you won't complain that I posted the whole thing)
sfml-tmxloader: https://github.com/fallahn/sfml-tmxloader/tree/next
Title: Re: Problems with Box2D and sfml-tmxloader
Post by: fallahn on August 31, 2016, 10:47:36 am
I'm not in a position to try the code right now but after a cursory glance the only thing which stands out is that you're not using the unit conversion functions when creating the the player body.

b2PolygonShape playerShape; playerShape.SetAsBox(32, 32);

This is creating a 32 metre by 32 metre box which, in SFML units, is 3200px * 3200px with the default scale, which is pretty big. Box2D is probably trying to force the giant body out when it collides with the rest of the map. If you want your player to be 32px * 32px try

playerShape.SetAsBox(tmx::sfToBoxVec({32.f, 32.f}))

Alternatively if you want a more 'prebaked' solution my game framework xygine (https://github.com/fallahn/xygine/wiki/Tiled-tmx-map-parser) has support for tmx maps with box2D physics.
Title: Re: Problems with Box2D and sfml-tmxloader
Post by: Spode87 on August 31, 2016, 10:51:42 am
Damn, that was a stupid problem x)

Thank you a lot!