Strange simulation when combining Box2D with SFML

Started by
3 comments, last by Fs02 11 years, 2 months ago

I have post it before in Api and Tools forum but i guest that's a wrong place, so i decide to repost it here, i hope you can find me a soulution :)

I want to make a game using Box2D v 2.2.1 and SFML 2, but i have a little problem with the physic simulation, its act strangely and different from when tested using default testbed.

I try to simulate the tire from behavior iforce.net tutorial that working well on the default testbed.

my main code:



...
void GAME::GameLoading()
{
    testTire = new Tire(&_mainWorld);
    _gameState = gs_Playing;
}

void GAME::MainLoop()
{
    ...
    while (_mainWindow.isOpen())
    {
        switch (_gameState)
        {

            case gs_Loading:
            {
                GameLoading();
                break;
            }
            case gs_Playing:
            {
                _mainWindow.clear();
		_mainWorld.Step(1/60.f,6,2);
                testTire->updateFriction();
                DebugRender();
                _mainWindow.display();
                break;
            }
        }

when i run this, the tire just move in x(gravity) axis with slow movement because the friction i made, but it wont move in y axis, i've try to rotate it a few degrees to but it doing the same.

please help me fix the code, i have worked on it for five days and still didn't find any solution.

Thaks smile.png

PS : Sorry for my bad english

Advertisement

Sounds like the problem is probably in the code that updates your testTire object. It would be useful if you posted that code since your run loop doesn't really tell us much about how you move the Tire object.

this is the code for updating the testTire object :


...
b2Vec2 Tire::getLateralVelocity()
{
    b2Vec2 currentRightNormal = b2Vec2(1,0);
    return b2Dot(currentRightNormal, m_Body->GetLinearVelocity()) * currentRightNormal;
}
...
void Tire::updateFriction()
{
    b2Vec2 impulse = m_Body->GetMass() * -getLateralVelocity();
    m_Body->ApplyLinearImpulse(impulse, m_Body->GetWorldCenter());
}

I think that's not the problem because i have test it in default test bed and run smoothly. that code just kill the lateral velocity so the object can only move foward or backward.

for addition, i have also try to implement it using car with those tires and its like the tire dont move(stop doing it's behavior) when the foward button is released.

the tire just move in x(gravity) axis with slow movement because the friction i made, but it wont move in y axis

that code just kill the lateral velocity so the object can only move foward or backward.

From what you posted, it looks like you specifically wrote the code to eliminate the y-axis velocity.

In your getLateralVelocity function, you return a vector that will always have a 0 y-value - you declare currentRightNormal as (1, 0), so whatever you multiply it by, the vector will always be (#, 0) - thus no movement in y. If you want movement in y, you need to somehow add a vector that has a y-component too.

Do you have a corresponding getLongitudinalVelocity function that you return some velocity vector that has a y-value and then sum it up with the lateral velocity vector?

In your getLateralVelocity function, you return a vector that will always have a 0 y-value - you declare currentRightNormal as (1, 0), so whatever you multiply it by, the vector will always be (#, 0) - thus no movement in y. If you want movement in y, you need to somehow add a vector that has a y-component too.

Oh yeah, i see it. i need to convert my currentRightNormal vector to be world vector, so i won't get zero value in y-axis.

thanks for your help, it's work fine now :)

This topic is closed to new replies.

Advertisement