簡體   English   中英

自頂向下的汽車在JBox2D libGDX運動問題

[英]Top-Down Car in JBox2D libGDX movement issues

我試圖基於Emanuele Feronato的“兩種制作Box2D汽車的方法”來實現自上而下的汽車游戲的Java版本。 我知道box2d的一些基礎知識,並且在很大程度上我將代碼轉換為Java,幾乎沒有例外。

但是,當我運行程序時,我的車不會移動。

如果我把所有的輪子都變成一個動態的車身(左前方除外)開始前后移動,前后甩車,但最后卻沒有任何地方。 前兩個關節是旋轉關節,每個關節都有馬達,而后背是棱柱形,所以如果我錯了,請糾正我,但前兩個應該是唯一“旋轉”/移動的。 我覺得我正在做一些可怕的錯誤,但到處都是我看起來總是在行動腳本,所以我不是百分之百肯定是錯的。

我檢查過,所有的車輪都在正確的位置,接頭也被放到了正確的車輪上。 我檢查了電機速度,它也在運行。 “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 );          }       }   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM