Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Strange simulation when combining Box2D with SFML


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Fs02   Members   -  Reputation: 413

Like
0Likes
Like

Posted 05 February 2013 - 01:56 AM

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


[my game in #ggj14 : http://www.indiedb.com/games/romance-stalker [my blog : http://surya.volge.net ]


Sponsor:

#2 Milcho   Crossbones+   -  Reputation: 1177

Like
0Likes
Like

Posted 05 February 2013 - 03:04 AM

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.



#3 Fs02   Members   -  Reputation: 413

Like
0Likes
Like

Posted 05 February 2013 - 04:50 AM

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.


[my game in #ggj14 : http://www.indiedb.com/games/romance-stalker [my blog : http://surya.volge.net ]


#4 Milcho   Crossbones+   -  Reputation: 1177

Like
1Likes
Like

Posted 05 February 2013 - 05:28 AM

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?



#5 Fs02   Members   -  Reputation: 413

Like
0Likes
Like

Posted 05 February 2013 - 05:52 AM

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


[my game in #ggj14 : http://www.indiedb.com/games/romance-stalker [my blog : http://surya.volge.net ]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS