Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problems with Box2D and sfml-tmxloader  (Read 1185 times)

0 Members and 1 Guest are viewing this topic.

Spode87

  • Newbie
  • *
  • Posts: 2
    • View Profile
Problems with Box2D and sfml-tmxloader
« 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
« Last Edit: August 31, 2016, 10:21:50 am by Spode87 »

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Problems with Box2D and sfml-tmxloader
« Reply #1 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 has support for tmx maps with box2D physics.

Spode87

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problems with Box2D and sfml-tmxloader
« Reply #2 on: August 31, 2016, 10:51:42 am »
Damn, that was a stupid problem x)

Thank you a lot!