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

Author Topic: drawing and collision objects  (Read 3252 times)

0 Members and 1 Guest are viewing this topic.

vovik_0_1

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
drawing and collision objects
« on: March 21, 2014, 03:55:12 pm »
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...
« Last Edit: March 25, 2014, 09:50:52 am by vovik_0_1 »

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: drawing and collision objects
« Reply #1 on: March 21, 2014, 08:34:03 pm »
It looks like you're using an early version of this to load your map. You could try the most recent version, which has built in support for box2D.

vovik_0_1

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: drawing and collision objects
« Reply #2 on: March 23, 2014, 11:00:44 am »
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


fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: drawing and collision objects
« Reply #3 on: March 23, 2014, 01:23:49 pm »
You need to link the zlib library.

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: drawing and collision objects
« Reply #4 on: March 23, 2014, 08:22:24 pm »
Please post a minimal and complete example code if you want more help with your code for all the reasons mentioned in the link.

vovik_0_1

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: drawing and collision objects
« Reply #5 on: March 24, 2014, 04:42:48 pm »
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.
« Last Edit: March 24, 2014, 04:44:33 pm by vovik_0_1 »

vovik_0_1

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: drawing and collision objects
« Reply #6 on: March 25, 2014, 09:47:59 am »
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;
}
« Last Edit: March 25, 2014, 09:49:30 am by vovik_0_1 »

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: drawing and collision objects
« Reply #7 on: March 25, 2014, 10:55:18 am »
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

vovik_0_1

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: drawing and collision objects
« Reply #8 on: March 25, 2014, 11:09:08 am »
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.

« Last Edit: March 25, 2014, 10:04:32 pm by vovik_0_1 »

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: drawing and collision objects
« Reply #9 on: March 26, 2014, 01:08:20 pm »
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;
 

vovik_0_1

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: drawing and collision objects
« Reply #10 on: March 26, 2014, 02:08:36 pm »
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;
}

 

anything