JBox2D libGDX运动中的自上而下的汽车问题 - java

我正在尝试基于Emanuele Feronato's "Two ways to make Box2D cars"实现Java版本的自上而下的汽车游戏。我知道box2d的一些基础知识,在大多数情况下,我几乎没有例外地将代码转换为Java。

但是,当我运行该程序时,我的车什么也不会动。

如果我将所有物体都变成动态物体,则所有车轮(左前叉除外)都会开始向前和向后移动,使汽车前后往复,但最终却无处可挡。前两个关节是旋转关节,每个旋转关节都带有马达,而后两个关节则是棱柱形的,所以如果我错了,请纠正我,但前两个关节应该是唯一的“旋转” /运动的关节。我觉得自己做错了什么,但是到处都是动作脚本,所以我不确定100%出了什么问题。

我检查了一下,所有车轮都处于正确的位置,并且接头也都安装在正确的车轮上。我检查了电机速度,它也正在运行。我的“ ldirection”和“ rdirection”的x分量始终为0,因此它会消除横向速度,而y始终是一个值。真的应该向前发展吗?

当车身上下移动时,左前轮始终与车身保持相同的距离。因此,左前方似乎工作正常。我检查了所有代码,以确保右前轮与左轮相同。

起跑车

向前加速时,只有两个右轮和左后轮来回移动。

当我开始转动两个前轮时,后两个轮仍与汽车保持一致,但开始以某种对角线的方式移动。最终,当前轮旋转90度时,它们开始几乎旋转关节的中心。

初始化

  this.world = new World(new Vector2(0, 0), false);
    this.box2Drender = new Box2DDebugRenderer();

    this.LeftPJointDef = new PrismaticJointDef();
    this.RightPJointDef = new PrismaticJointDef();
    this.RightJointDef = new RevoluteJointDef();
    this.LeftJointDef = new RevoluteJointDef();

    this.CarBody = new PolygonShape();
    this.RightRWheelShape = new PolygonShape();
    this.RightFWheelShape = new PolygonShape();
    this.LeftRWheelShape = new PolygonShape();
    this.LeftFWheelShape = new PolygonShape();

    this.LeftRWheelDef = new BodyDef();
    this.RightRWheelDef = new BodyDef();
    this.RightFWheelDef = new BodyDef();
    this.LeftFWheelDef = new BodyDef();

    this.bodyD = new BodyDef();
    this.CarFixDef = new FixtureDef();

    this.x = x;
    this.y = y;
    this.Cpos = new Vector2(x,y);

    this.RRW = new Vector2((this.x + (this.x * XPrc)), (this.y + (this.y * -YPrc)));
    this.RLW = new Vector2((this.x + (this.x * -XPrc)), (this.y + (this.y * -YPrc)));
    this.FRW = new Vector2((this.x + (this.x * XPrc)), (this.y + (this.y * YPrc)));
    this.FLW = new Vector2((this.x + (this.x * -XPrc)), (this.y + (this.y * YPrc)));

    this.WheelSizeX = this.width * 0.25f;
    this.WheelSizeY = this.length * 0.30f;
    //setting bodyDef damping
    bodyD.linearDamping = 0.5f;
    bodyD.angularDamping = 0.5f;

    //Adding bodyDef to the world and setting type as Dynamic
    body = world.createBody(bodyD);
    body.setType(BodyDef.BodyType.DynamicBody);

    //setting the body position in the world using the Vector given.
    body.setTransform(this.Cpos, (float) ((Math.PI) / 2));

    //Adding the calculated Position vecotrs of the wheel's to each wheel def.
    RightFWheelDef.position.add(FRW);
    LeftFWheelDef.position.add(FLW);
    RightRWheelDef.position.add(RRW);
    LeftRWheelDef.position.add(RLW);

    //Adding the wheels to the world using the Wheel Defs.
    RightFWheel = world.createBody(RightFWheelDef);
    LeftFWheel = world.createBody(LeftFWheelDef);
    RightRWheel = world.createBody(RightRWheelDef);
    LeftRWheel = world.createBody(LeftRWheelDef);

    RightFWheel.setType(BodyDef.BodyType.DynamicBody);
    RightRWheel.setType(BodyDef.BodyType.DynamicBody);
    LeftFWheel.setType(BodyDef.BodyType.DynamicBody);
    LeftRWheel.setType(BodyDef.BodyType.DynamicBody);

    //Setting the car(box) and wheel size
    CarBody.setAsBox(this.length, this.width);
    LeftFWheelShape.setAsBox(WheelSizeX, WheelSizeY);
    LeftRWheelShape.setAsBox(WheelSizeX, WheelSizeY);
    RightRWheelShape.setAsBox(WheelSizeX, WheelSizeY);
    RightFWheelShape.setAsBox(WheelSizeX, WheelSizeY);

    CarFixDef.shape = CarBody;

    RightFWheel.createFixture(RightFWheelShape, 1);
    RightRWheel.createFixture(RightRWheelShape, 1);
    LeftFWheel.createFixture(LeftFWheelShape, 1);
    LeftRWheel.createFixture(LeftRWheelShape, 1);

    body.createFixture(CarFixDef);

    LeftJointDef.enableMotor = true;
    RightJointDef.enableMotor = true;

    LeftJointDef.maxMotorTorque = 500;
    RightJointDef.maxMotorTorque = 500;

    //Setting Front Wheel joints in respects to the wheels and body
    LeftJointDef.initialize(body, LeftFWheel, LeftFWheel.getWorldCenter());
    RightJointDef.initialize(body, RightFWheel, RightFWheel.getWorldCenter());



    this.LeftJoint = (RevoluteJoint) world.createJoint(LeftJointDef);
    this.RightJoint = (RevoluteJoint) world.createJoint(RightJointDef);

    LeftPJointDef.enableLimit = true;
    RightPJointDef.enableLimit = true;
    //Translation Limit
    LeftPJointDef.lowerTranslation = 0;
    LeftPJointDef.upperTranslation = 0;
    RightPJointDef.lowerTranslation = 0;
    RightPJointDef.upperTranslation = 0;

    //Setting Rear wheel joints in respects to wheel and body
    LeftPJointDef.initialize(body, LeftRWheel, LeftRWheel.getWorldCenter(), new Vector2(1, 0));
    RightPJointDef.initialize(body, RightRWheel, RightRWheel.getWorldCenter(), new Vector2(1, 0));



    //adding the P Joints to the world.
    this.LeftPJoint = (PrismaticJoint) world.createJoint(LeftPJointDef);
    this.RightPJoint = (PrismaticJoint) world.createJoint(RightPJointDef);

这是我的更新方法。

    KillOrthoVelocity(LeftFWheel);
    KillOrthoVelocity(RightFWheel);
    KillOrthoVelocity(LeftRWheel);
    KillOrthoVelocity(RightRWheel);

    //Driving
    float r1 = LeftFWheel.getTransform().getRotation();
    Vector2 ldirection = new Vector2((float) -Math.sin(r1), (float) Math.cos(r1));
    ldirection.scl(enginespeed);
    float r2 = RightFWheel.getTransform().getRotation();
    Vector2 rdirection = new Vector2((float) -Math.sin(r2), (float) Math.cos(r2));
    rdirection.scl(enginespeed);

    LeftFWheel.applyForce(ldirection, LeftFWheel.getPosition(), true);
    RightFWheel.applyForce(rdirection, RightFWheel.getPosition(), true);


    //Steering
    float movespeed;

    movespeed = steerAng - LeftJoint.getJointAngle();
    LeftJoint.setMotorSpeed(movespeed * AngleSpeed);

    movespeed = steerAng - RightJoint.getJointAngle();
    RightJoint.setMotorSpeed(movespeed * AngleSpeed);

    world.step(dt, 6, 2);

由于KillOrthoVelocity类似于获取“ ldirection”

    Vector2 localP = new Vector2(0, 0);
    Vector2 velocity = body.getLinearVelocityFromLocalPoint(localP);

    float r = body.getTransform().getRotation();
    Vector2 sideways = new Vector2((float) -Math.sin(r), (float) Math.cos(r));
    sideways.scl(velocity.dot(sideways));

    body.setLinearVelocity(sideways);

任何建议将不胜感激!即使只是线索也将非常有用!
谢谢!

参考方案

try this code 


RaceCar::RaceCar( b2World* world )  
{       
b2Vec2 race_car_pos( 13.0f,
        13.0f );

/ Create race car chassis 
{       
    // Physics Body Shape           
b2Vec2 verticies[8];            
verticies[0].Set(  1.5f, 0.0f  );   
        verticies[1].Set(  3.0f, 2.5f  );   
        verticies[2].Set(  2.8f, 5.5f  );   
        verticies[3].Set(  1.0f, 10.0f ); 
            verticies[4].Set( -1.0f,
        10.0f );        
    verticies[5].Set( -2.8f, 5.5f  );   
        verticies[6].Set( -3.0f, 2.5f  );   
        verticies[7].Set( -1.5f, 0.0f  );

b2PolygonShape body_shape;      
    body_shape.Set( verticies, 8 );

// Physics body definition  
        b2BodyDef body_def;     
        body_def.type
        = b2_dynamicBody; 
//          body_def.position =b2Vec2(3,3);     
        // Physics Body Fixture             
b2FixtureDef body_fixture;      
    body_fixture.shape = &body_shape;           
body_fixture.density = 0.1f;

m_chassisPhysicsBody = world->CreateBody( &body_def );  
        m_chassisPhysicsBody->CreateFixture( &body_fixture );           m_chassisPhysicsBody->SetUserData( this );

m_chassisPhysicsBody->SetTransform( race_car_pos, 0.0f ); 
        }

// Tires 
{           b2Vec2 pos = m_chassisPhysicsBody->GetPosition();

// Top left tire    
        {
                        RCTire* tire_top_left = new RCTire( world );
                        tire_top_left->setPosition( race_car_pos );
                        m_tires.push_back( tire_top_left );

                        b2RevoluteJointDef joint_def;
                        joint_def.enableLimit = true;
                        joint_def.lowerAngle = 0.0f;
                        joint_def.upperAngle = 0.0f;
                        joint_def.bodyA = m_chassisPhysicsBody;
                        joint_def.localAnchorB.SetZero();
                        joint_def.bodyB = tire_top_left->getPhysicsBody();
                        joint_def.localAnchorA.Set( -3.0f, 8.5f );
                        b2RevoluteJoint* joint = static_cast<b2RevoluteJoint*>( world->CreateJoint( &joint_def ) );
                        tire_top_left->setJointToChassis( joint );          }

// Top right tire   
{
                        RCTire* tire_top_right = new RCTire( world );
                        tire_top_right->setPosition( race_car_pos );
                        m_tires.push_back( tire_top_right );

                        b2RevoluteJointDef joint_def;
                        joint_def.enableLimit = true;
                        joint_def.lowerAngle = 0.0f;
                        joint_def.upperAngle = 0.0f;
                        joint_def.bodyA = m_chassisPhysicsBody;
                        joint_def.localAnchorB.SetZero();
                        joint_def.bodyB = tire_top_right->getPhysicsBody();
                        joint_def.localAnchorA.Set( 3.0f, 8.5f );
                        b2RevoluteJoint* joint = static_cast<b2RevoluteJoint*>( world->CreateJoint( &joint_def ) );
                        tire_top_right->setJointToChassis( joint );             }

// Bottom left tire 
{
                        RCTire* tire_bottom_left = new RCTire( world );
                        tire_bottom_left->setPosition( race_car_pos );
                        m_tires.push_back( tire_bottom_left );

                        b2RevoluteJointDef joint_def;
                        joint_def.enableLimit = true;
                        joint_def.lowerAngle = 0.0f;
                        joint_def.upperAngle = 0.0f;
                        joint_def.bodyA = m_chassisPhysicsBody;
                        joint_def.localAnchorB.SetZero();
                        joint_def.bodyB = tire_bottom_left->getPhysicsBody();
                        joint_def.localAnchorA.Set( -3.0f, 0.5f );

                        b2RevoluteJoint* joint = static_cast<b2RevoluteJoint*>( world->CreateJoint( &joint_def ) );
                        tire_bottom_left->setJointToChassis( joint );           }

                    // Bottom right tire            {
                        RCTire* tire_bottom_right = new RCTire( world );
                        tire_bottom_right->setPosition( race_car_pos );
                        m_tires.push_back( tire_bottom_right );

                        b2RevoluteJointDef joint_def;
                        joint_def.enableLimit = true;
                        joint_def.lowerAngle = 0.0f;
                        joint_def.upperAngle = 0.0f;
                        joint_def.bodyA = m_chassisPhysicsBody;
                        joint_def.localAnchorB.SetZero();
                        joint_def.bodyB = tire_bottom_right->getPhysicsBody();
                        joint_def.localAnchorA.Set( 3.0f, 0.5f );
                        world->CreateJoint( &joint_def );
                        b2RevoluteJoint* joint = static_cast<b2RevoluteJoint*>( world->CreateJoint( &joint_def ) );
                        tire_bottom_right->setJointToChassis( joint );          }       }   }

Java重试异常处理 - java

Improve this question 在小型Internet应用程序上工作时,我需要处理服务中断的情况。发生异常后重试的好方法是什么? 参考方案 最简单的解决方案(尽管可能不是最好的)是将错误页面返回给用户(状态503:服务不可用),并告诉他应该在几秒钟后重试。

JAVA:字节码和二进制有什么区别? - java

java字节代码(已编译的语言,也称为目标代码)与机器代码(当前计算机的本机代码)之间有什么区别?我读过一些书,他们将字节码称为二进制指令,但我不知道为什么。 参考方案 字节码是独立于平台的,在Windows中运行的编译器编译的字节码仍将在linux / unix / mac中运行。机器代码是特定于平台的,如果在Windows x86中编译,则它将仅在Win…

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Java:BigInteger,如何通过OutputStream编写它 - java

我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…

如何使用正则表达式匹配以相反顺序排列的后两个字符为前两个字符的任何字符串 - java

Closed. This question needs to be more focused。它当前不接受答案。                                                                                                                            …