Problem with wheel joints (car game)

Hi!

I'm writing game with car on difficult terrain (in C++ and Box2d).
I built a car using rectangle as car body, circles as wheels and springs was wheel joints.
But I've got a problem with springs - when I apply to they the same motor speed, they "distort", and they aren't on the same level.http://youtu.be/qICHHMmcBOk
PS: I use SFML and b2Draw for display
Code:
struct Body
{
    b2Body* b;
    unsigned dm;
    string name;

    enum DisplayMethod
    {
        B2DebugDraw,
        Console,
    };

    enum Type
    {
        Static,
        Dynamic,
    };

    void update(Time elapsed);
    void display(RenderWindow& win);

    Body(string n, Vector2f pos, b2World* parent, const b2Shape& shape, Type t, unsigned dispMet = B2DebugDraw);
};

struct Game
{
    RenderWindow win;
    b2World world;
    vector<Body*> objs;
//...
    Body* addStaticBody(Vector2f centerPos, const b2Shape& shape, string name); // creates static body
    Body* addDynamicBody(Vector2f pos, const b2Shape& shape, string name); // creates dynamic body

    vector<Body*> getBodyList() { return objs; }
    Body* getBody(string name) { for(int i = 0; i < objs.size(); i++) if(objs[i]->name == name) return objs[i]; return NULL; }
    template<typename JointT>
    JointT* getJoint(Body* bA, Body* bB);

    Time update(); // return elapsed time
    void display();
};
void createCar(Game* g)
{
    Vector2f ltPos(320.0f, 600.0f); // position left-top car

    // body
    b2PolygonShape sh;
    sh.SetAsBox(SFtoB2(50.0f), SFtoB2(20.0f), SFtoB2(ltPos + Vector2f(50.0f, 20.0f)), 0.0f);
    Body* car = g->addDynamicBody(Vector2f(), sh, "car");

    // wheels
    float whRad = 10.0f;
    float whFr = 0.5f;
    b2CircleShape wh;
    wh.m_radius = SFtoB2(whRad);

    wh.m_p = SFtoB2(ltPos + Vector2f(20.0f, 50.0f));
    Body* lwheel = g->addDynamicBody(Vector2f(), wh, "left wheel");
    lwheel->b->GetFixtureList()->SetFriction(whFr);

    wh.m_p = SFtoB2(ltPos + Vector2f(80.0f, 50.0f));
    Body* rwheel = g->addDynamicBody(Vector2f(), wh, "right wheel");
    rwheel->b->GetFixtureList()->SetFriction(whFr);

    // springs
    b2WheelJointDef spr;
    spr.collideConnected = false;
    spr.dampingRatio = 2000.0f;
    spr.frequencyHz = 7.0f;
    spr.enableMotor = true;
    spr.motorSpeed = 0.0f;
    spr.maxMotorTorque = 100000.0f;

    spr.Initialize(car->b, lwheel->b, SFtoB2(ltPos + Vector2f(20.0f, 50.0f)), b2Vec2(0.0f, 0.4f));
    g->world.CreateJoint(&spr);

    spr.Initialize(car->b, rwheel->b, SFtoB2(ltPos + Vector2f(80.0f, 50.0f)), b2Vec2(0.0f, 0.4f));
    g->world.CreateJoint(&spr);
}
void updateCar(Game* g, Time elapsed)
{
    float spd = 100000.0f; // [px/s]

    b2WheelJoint* left = g->getJoint<b2WheelJoint>(g->getBody("car"), g->getBody("left wheel"));
    b2WheelJoint* right = g->getJoint<b2WheelJoint>(g->getBody("car"), g->getBody("right wheel"));
    if(Keyboard::isKeyPressed(Keyboard::Up))
    {
        left->SetMotorSpeed(SFtoB2(elapsed.asSeconds() * spd));
        right->SetMotorSpeed(SFtoB2(elapsed.asSeconds() * spd));
    }
    else if(Keyboard::isKeyPressed(Keyboard::Down))
    {
        left->SetMotorSpeed(-SFtoB2(elapsed.asSeconds() * spd));
        right->SetMotorSpeed(-SFtoB2(elapsed.asSeconds() * spd));
    }
    else
    {
        left->SetMotorSpeed(0.0f);
        right->SetMotorSpeed(0.0f);
    }

// setup view
//    View v = g->win.getView();
//    Body* car = g->getBody("car");
//    v.setCenter(B2toSF(car->b->GetWorldPoint(car->b->GetFixtureList()->GetAABB(0).GetCenter())));
//    g->win.setView(v);
}

int main()
{
    Game app(/*...*/);
    createCar(&app);
//...

    while(app.win.isOpen())
    {
        Event e;
        while(app.win.pollEvent(e))
        {
            if(e.type == Event::Closed)
                app.win.close();
        }

//...

        Time elapsed = app.update();
        updateCar(&app, elapsed);
        app.display();
    }
}
It's really hard to guess what's going on without the debug draw display to look at... my first recommendation would be to get that set up so that you can check that fixtures and joint anchors etc are really where you think they are. (That said, it kinda looks like you already have the positions correct.)

Make sure that the scale you are using between pixels and Box2D units is reasonable, eg. the wheels of the car will ideally be around 1 physics unit in diameter.
Make sure that the masses of the wheels and the main body of the car are not too different. When the car body is significantly (eg. factor of 10 or more) heavier than the sum of the mass of the wheels that support it, you may have problems with stability and the wheels may start being pushed into the ground.

Those settings for the wheel joint seem kinda weird.... try these:
spr.dampingRatio = 0.7f; // the default
spr.frequencyHz = 4.0f; // the default
spr.maxMotorTorque = 100.0f;

It also seems really weird to set the speed of the wheels depending on the time elapsed since the last frame. Don't you want the wheels to spin at a constant speed when driving? Just use a fixed value for the speed, eg. 2.5 etc and remember this is radians per second, not degrees.

As a general rule, don't use the time elapsed between frames for anything to do with physics.
irresistible force wrote:
Those settings for the wheel joint seem kinda weird.... try these:
spr.dampingRatio = 0.7f; // the default
spr.frequencyHz = 4.0f; // the default
spr.maxMotorTorque = 100.0f;

Yes! It works normally. But I've got a question: what are max values of those parameters?


irresistible force wrote:
As a general rule, don't use the time elapsed between frames for anything to do with physics.

Why?
There are no max values, only values that are too high for reasonable behavior, as you found out 🙂

Using a variable timestep means that behavior will be very inconsistent. There is plenty of discussion about this on the net and why a fixed timestep is preferrable, try googling terms like: physics simulation fixed vs variable timestep

Original thread: https://box2d.org/forum/viewtopic.php?t=9643