Sign in to follow this  
Fs02

Strange simulation when combining Box2D with SFML

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this