Box2dWeb旋转关节“拉伸” [英] Box2dWeb revolute joint "stretching"

查看:199
本文介绍了Box2dWeb旋转关节“拉伸”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 box2dweb 在2D中创建类似汽车的东西。我有一个车身和两个圆圈的盒子,通过旋转接头连接到它,作为车轮。一切都很好,但是轮子的中心和它们所附的盒子上的点之间的间隙逐渐增大。为求解器设置更多迭代没有帮助,轮子迟早会完全离开盒子。它看起来像某种积累的错误。此外,车轮与地面上的一些碰撞碰撞似乎是导致这个问题的最主要因素。最初它就像在左边的图片上,但是在我将它在地面上的大球上驾驶几次之后,就像在右边的图片上一样,并保持这种状态:

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:

之前我使用过较旧的box2djs库,似乎没有这个问题。我将代码移植到box2dweb后出现问题。在这两个库中创建事物的方式有很多不同,所以我一定错过了什么,但不知道是什么。

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.

代码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;
}

对于关节:

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

因此我讨厌询问我的代码有什么问题-kinda问题,我是什么在这里做错了吗?

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

推荐答案

问题似乎是零位置迭代引起的:

It appears the problem was caused by zero position iterations:

world.Step(dt, iterations);

似乎在旧版本的Box2Djs中,函数的原型就是这样的。但是,在较新的版本中,它已更改为

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)

所有参数默认为0.以旧方式调用,如 world.Step(dt,iterations) 将等于 world.Step(dt,iterations,0),仍然有点有效,但相对位置连接的身体没有得到妥善解决。我试着把它称为 world.Step(dt,iterations,iterations),突然一切都得到修复,即使迭代次数很少,比如3。

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.

这篇关于Box2dWeb旋转关节“拉伸”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆