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

Author Topic: [Solved]body and Sprite lag problem  (Read 2501 times)

0 Members and 1 Guest are viewing this topic.

geekx

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
[Solved]body and Sprite lag problem
« on: September 10, 2013, 09:38:48 pm »
Hello everyone im new to c++, box2d, and SFML and haven't done anything more than small amount of visual basic in high school. I took a week to learn the basics of c++ and SFML and finally got a grasp on box2d until now.

Currently i have a small world with a few static bodies and one player body that moves via impulses around the screen. I have now started to attach a sprite to my player body and it works fine when my body is at full speed. During acceleration my sprite doesn't match up with my body until it hits full acceleration.



                                b2Vec2 pvel = pBody->GetLinearVelocity();
                                b2Vec2 desiredVel (2,2);
                       
                                float velChangeXR = desiredVel.x - pvel.x;
                                float velChangeYD = desiredVel.y - pvel.y;
                                float velChangeXL = desiredVel.x + pvel.x;
                                float velChangeYU = desiredVel.y + pvel.y;

                                impulseXR = velChangeXR;
                                impulseYD = velChangeYD;
                                impulseXL = velChangeXL;
                                impulseYU = velChangeYU;
                       

                       
                        b2Vec2 movement(0.f, 0.f);
                        if (pMovingUp)
                        {
                                movement.y -= impulseYU;
                                pBody->ApplyLinearImpulse(b2Vec2(0,movement.y), pBody->GetWorldCenter() );
                                source.y = Direction::Up;      
                                std::cout << pvel.y << std::endl;
                        }
                        if (pMovingDown)
                        {
                                movement.y = impulseYD;
                                pBody->ApplyLinearImpulse(b2Vec2(0,movement.y), pBody->GetWorldCenter() );
                                source.y = Direction::Down;
                                std::cout << pvel.y << std::endl;
                        }
                        if (pMovingLeft)
                        {
                                movement.x -= impulseXL ;
                                pBody->ApplyLinearImpulse(b2Vec2(movement.x,0), pBody->GetWorldCenter() );
                                source.y = Direction::Left;
                                std::cout << pvel.x << std::endl;
                        }
                        if (pMovingRight)
                        {
                                movement.x = impulseXR;
                                pBody->ApplyLinearImpulse(b2Vec2(movement.x,0), pBody->GetWorldCenter() );
                                source.y = Direction::Right;
                                std::cout << pvel.x << std::endl;
                        }
                        if (keyR == true)
                        {
                                pBody->SetLinearVelocity(b2Vec2(0,0));
                                pBody->SetAngularVelocity(0);
                        }
                       
//sprite to pbody glue//////////////////////////////////////////////////////////////////
               
        {
        pbos = pBody->GetPosition();
        pSprite.setPosition((pbos.x * sfdd::SCALE) - 16,(pbos.y * sfdd::SCALE) - 16);

        //animation rect
        pSprite.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
        }
        }
//render//
        worldA.Step(timeStep,velocityIterations,positionIterations);
        worldA.DrawDebugData();
        //mWindow.draw(ground);
        mWindow.draw(pSprite);
        mWindow.display();
                       
               

 

Here is the code feel free to tell me im an idiot but please be helpful for others new to this  :).
« Last Edit: September 11, 2013, 07:02:34 am by geekx »

_Fiction_

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Box2d body and Sprite lag problem
« Reply #1 on: September 11, 2013, 03:19:24 am »
Try setting the sprite to the body position after the world step.

geekx

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Box2d body and Sprite lag problem
« Reply #2 on: September 11, 2013, 07:02:06 am »
That worked thank you, that seems like an obvious thing i should have tried.