Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9161083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:53:18+00:00 2026-06-17T13:53:18+00:00

I am trying to create a car-like thing in 2D using box2dweb . I

  • 0

I am trying to create a car-like thing in 2D using box2dweb. I have a box for the car body and two circles, connected to it by revolute joints, as the wheels. Everything works fine for a while, but gradually the gaps increase between the centers of the wheels and the points on the box they were attached to. Setting more iterations for the solver doesn’t help, and sooner or later the wheels go completely away from the box. It looks like an accumulated error of some sort. Also, collision of the wheels with some bumps on the ground seem to be the most contributing factor to this problem. Initially it’s like on the left picture, but after I drive it over the big ball on the ground a couple of times, it gets like on the right picture, and stays that way:

Left: initial state, right: after I drive it over the big circle several times.

I used an older box2djs library before and didn’t seem to have this problem there. The problem appeared after I ported the code to box2dweb. There are quite a few differences in how things are created in those two libraries, so I must’ve missed something, but don’t know what.

The code for the box:

function createBox(world, x, y, width, height)
{
    var fixDef = new b2FixtureDef;
    fixDef.density = 1.0;
    fixDef.friction = 1.0;
    fixDef.restitution = 1.0;

    var bodyDef = new b2BodyDef;
    bodyDef.type = b2Body.b2_dynamicBody;
    bodyDef.position.x = x;
    bodyDef.position.y = y;

    fixDef.shape = new b2PolygonShape;
    fixDef.shape.SetAsBox(width, height);

    var b = world.CreateBody(bodyDef);
    b.CreateFixture(fixDef);
    return b;
}

For the wheels (almost same, except it creates circles):

function createBall(world, x, y, r)
{
    var fixDef = new b2FixtureDef;
    fixDef.density = 1.0;
    fixDef.friction = 1.0;
    fixDef.restitution = 1.0;

    var bodyDef = new b2BodyDef;
    bodyDef.type = b2Body.b2_dynamicBody;
    bodyDef.position.x = x;
    bodyDef.position.y = y;

    fixDef.shape = new b2CircleShape(r);

    var b = world.CreateBody(bodyDef);
    b.CreateFixture(fixDef);
    return b;
}

And for the joints:

var jointDef_rear = new b2RevoluteJointDef();   
jointDef_rear.Initialize(rear_wheel, car_body, rear_wheel.GetPosition());
jointDef_rear.maxMotorTorque = 10.0;
jointDef_rear.enableMotor = true;

rear_joint = world.CreateJoint(jointDef_rear);

var jointDef_front = new b2RevoluteJointDef();  
jointDef_front.Initialize(front_wheel, car_body, front_wheel.GetPosition());
jointDef_front.maxMotorTorque = 10.0;
jointDef_front.enableMotor = true;

front_joint = world.CreateJoint(jointDef_front);

So as much as I hate asking “what’s wrong with my code”-kinda questions, what am I doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:53:19+00:00Added an answer on June 17, 2026 at 1:53 pm

    It appears the problem was caused by zero position iterations:

    world.Step(dt, iterations);
    

    It seems that in older versions of Box2Djs the prototype of the function was exactly like that. In a newer version, however, it was changed to

    function (dt, velocityIterations, positionIterations)
    

    with all parameters defaulting to 0. Calling it the old way, like world.Step(dt, iterations) would then be equal to world.Step(dt, iterations, 0), which still “sort of” works, but the relative positions of the connected bodies aren’t properly resolved. I tried calling it like world.Step(dt, iterations, iterations), and suddenly everything got fixed, even if the number of iterations is low, like 3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a car make/model form using Javascript or AJAX, problem
I am trying to create a car application, each car belongs to a make
I'm trying to a create an update statement along the following lines: TABLE car:
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
I'm trying to create a standalone app using JBoss Microcontainer for IoC and JBoss
I have been working in DrRacket, trying to create a prefix function (#lang racket).
I have the following table definition: CREATE TABLE [Car] ( CarID int NOT NULL
//I am trying to create a basic race car in java. I made a
I'm trying to create a car game in C#. I'm now trying to figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.