java - box2d 中的旋转关节

标签 java c++ opengl box2d revolute-joints

我一直在努力关注the tutorial here build 一个旋转接头;本质上,一只 ARM 。 我需要将 ARM (一个矩形)固定到正方形上的一个点并围绕它旋转。当施加力时,我希望这两个形状表现得像一个形状,固定在一起,而 ARM 像布娃娃一样绕着盒子飞来飞去。

不幸的是,这根本不起作用。 ARM 一开始是连在一起的,然后当我施加力时,两个形状分开,并且以我无法解释的奇怪方式表现。几乎就像 anchor 处于一些不稳定的位置。

我正在使用 jbox2d 和来自 this tutorial 的大量代码还有。

(我已经将初始 anchor 设置到中心只是为了看看它是否有效) (有一些奇怪的转换,因为我使用的是openGl)

这里是要点:

    public class New_char
    {
    Vec2             torso_pos,    arm_pos;
    Body             torso,        arm;
    PolygonShape     torso_shape,  arm_shape;
    BodyDef          torso_def,    arm_def;
    FixtureDef       torso_fix,    arm_fix;

    RevoluteJointDef torsoArmDef; 
    RevoluteJoint    torsoArmJoint; 

    //float[] torSize = {0.5f, 0.5f}, armSize={0.75f, 0.10f};


    public New_char(World world, float[] pos)
    {
        //this.torso_pos = new Vec2(pos[0], pos[1]) ;   this.arm_pos = new Vec2(pos[0]+10,pos[1]+10);   
        //out.println(this.arm_pos+" thepos "+this.torso_pos);

        this.torso_def = new BodyDef()            ;  this.arm_def = new BodyDef();
        torso_def.type = BodyType.DYNAMIC         ;  arm_def.type = BodyType.DYNAMIC;
        torso_def.position.set(320 / 30 / 2, 240 / 30 / 2)    ;  arm_def.position.set(320 / 30 / 2, 240 / 30 / 2);

        this.torso_shape = new PolygonShape()     ;  this.arm_shape = new PolygonShape();
        this.torso_shape.setAsBox(0.50f, 0.50f)   ;  this.arm_shape.setAsBox(0.75f, 0.10f);

        this.torso_fix = new FixtureDef()         ;  this.arm_fix = new FixtureDef();
        this.torso_fix.density = 0.1f             ;  this.arm_fix.density = 0.5f;
        this.torso_fix.shape = this.torso_shape   ;  this.arm_fix.shape = this.arm_shape;

        this.torso = world.createBody(this.torso_def) ;  this.arm = world.createBody(this.arm_def);
        this.torso.createFixture(this.torso_fix)      ;  this.arm.createFixture(this.arm_fix);

        this.torsoArmDef = new RevoluteJointDef();
        this.torsoArmDef.bodyA = this.torso ; this.torsoArmDef.bodyB = this.arm;
        this.torsoArmDef.collideConnected = false;
        this.torsoArmDef.localAnchorA.set(this.torso.getWorldCenter());
        //Vec2 armpin = new Vec2(1f, 1f);
        this.torsoArmDef.localAnchorB.set(this.arm.getWorldCenter());
        this.torsoArmJoint = (RevoluteJoint)world.createJoint(this.torsoArmDef);

最佳答案

问题是 getWorldCenter(我发现的每个教程都推荐它)

解决方法是getLocalCenter:

    this.torsoArmDef.localAnchorA.set(this.torso.getLocalCenter());
    this.torsoArmDef.localAnchorB.set(this.arm.getLocalCenter());

它现在神奇地旋转了!

关于java - box2d 中的旋转关节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25800705/

相关文章:

java - 列表<?> 与列表<?扩展对象>

c++ - 作为具有默认值的参数的函数

python - 带有 Eigen3::Vector3d 和 std::vector<Vector3d> 的 SWIG

c++ - OpenGL 纹理不

java - 如何在 micronaut 中指定标题参数是必需的还是可选的

java - PropertyEditor 未根据 AJAX (JSON) 请求调用

java - onSharedPreferenceChanged 不更新首选项

c++ - 未找到重载函数

java - 在LWJGL 3中渲染透视投影矩阵

c++ - OpenGL:在着色器中执行 TexCoord 计算,不好的做法?