Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java box2d中的旋转关节_Java_C++_Opengl_Box2d_Revolute Joints - Fatal编程技术网

Java box2d中的旋转关节

Java box2d中的旋转关节,java,c++,opengl,box2d,revolute-joints,Java,C++,Opengl,Box2d,Revolute Joints,我一直在尝试建造一个旋转关节;本质上,是一只手臂。 我需要手臂,一个长方形,被固定在一个正方形上的一个点上并围绕它旋转。当施加力时,我希望两个形状表现为一个,固定在一起,同时手臂像布娃娃一样绕着盒子飞行 不幸的是,这根本不起作用。手臂起初是连接在一起的,然后当我用力的时候,两个形状分开了,并且以我无法解释的奇怪方式表现出来。它几乎就像锚在一些摇摇晃晃的位置 我正在使用jbox2d和许多来自的代码 我已经在中心设置了初始锚,只是为了看看它是否能工作 有一些奇怪的转换,因为我使用的是openGl 要

我一直在尝试建造一个旋转关节;本质上,是一只手臂。 我需要手臂,一个长方形,被固定在一个正方形上的一个点上并围绕它旋转。当施加力时,我希望两个形状表现为一个,固定在一起,同时手臂像布娃娃一样绕着盒子飞行

不幸的是,这根本不起作用。手臂起初是连接在一起的,然后当我用力的时候,两个形状分开了,并且以我无法解释的奇怪方式表现出来。它几乎就像锚在一些摇摇晃晃的位置

我正在使用jbox2d和许多来自的代码

我已经在中心设置了初始锚,只是为了看看它是否能工作 有一些奇怪的转换,因为我使用的是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());

它现在神奇地旋转

我知道也有类似的问题,但我觉得这些问题没有得到充分的回答,也不是很具体。如果你施加的力足够大,关节约束可以很容易地分离。你通常对旋转关节及其身体使用什么密度/力值?这并不是很重要,作为密度和所施加的力之间的比率。密度=1和力=1的结果与密度=10和力=10的结果相同。那么,旋转接头的最佳传动比是多少?