0

我目前正在处理(java)中的类似游戏的爬山赛车,并且我正在使用 box2d 处理库。我已经定义了我的汽车、车轮和两个 WheelJoint,但是当我调用函数时,Accelerator() 汽车根本不动。我已经测试了打印 wjArriere 的 motorSpeed 并且它是我想要的实际值,但汽车保持静止。

我希望您能够找到导致问题的原因,如果有不清楚的地方,请随时提出任何问题!

这是我的代码:

汽车构造器:

class Car {
  // Our object is one chassis and two wheels
  Box chassis;
  Wheel wheelGauche;
  Wheel wheelDroite;
  WheelJoint wjArriere;
  WheelJoint wjAvant;
  int w = 90, h = 30;



  Car(float x, float y) {
    // Initialize position of the chassis
    chassis = new Box(x, y, w, h, false);

    // Initialize position of two wheels
    wheelGauche = new Wheel(x, y, h/3);
    wheelDroite = new Wheel(x, y, h/3);

    // Define joints (Pour permettre aux roues de tourner)
    WheelJointDef axisDef = new WheelJointDef();
    axisDef.bodyA = chassis.body;
    axisDef.bodyB = wheelGauche.body;
    axisDef.localAnchorA.set(new Vec2(-2.5, -2.1));
    axisDef.frequencyHz = 5;
    axisDef.dampingRatio = 0.4;
    axisDef.maxMotorTorque = chassis.fd.density * 100;
    axisDef.localAxisA.set(new Vec2(0, 1));
    wjArriere = (WheelJoint) box2d.world.createJoint(axisDef);

    axisDef.bodyB = wheelDroite.body;
    axisDef.localAnchorA.x *= -1;
    wjAvant = (WheelJoint) box2d.world.createJoint(axisDef);
  }

盒子构造器:

class Box {

  // We need to keep track of a Body and a width and height
  Body body;
  float w;
  float h;
  PolygonShape ps;
  FixtureDef fd = new FixtureDef();
  // Constructor
  Box(float x, float y, float w_, float h_, boolean lock) {
    w = w_;
    h = h_;

    // Define and create the body
    BodyDef bd = new BodyDef();
    bd.position.set(box2d.coordPixelsToWorld(new Vec2(x, y)));
    if (lock) bd.type = BodyType.STATIC;
    else bd.type = BodyType.DYNAMIC;

    body = box2d.createBody(bd);

    // Define the shape -- a  (this is what we use for a rectangle)
    PolygonShape sd = new PolygonShape();
    //float box2dW = box2d.scalarPixelsToWorld(w/2);
    //float box2dH = box2d.scalarPixelsToWorld(h/2);
    //sd.setAsBox(box2dW, box2dH);
    // Chassis
    Vec2[] vertices = new Vec2[4];  // An array of the car vectors
    vertices[0] = box2d.vectorPixelsToWorld(new Vec2(w/2, h/2));
    vertices[1] = box2d.vectorPixelsToWorld(new Vec2(w/2, -h/2));
    vertices[2] = box2d.vectorPixelsToWorld(new Vec2(-w/2, -h/2));
    vertices[3] = box2d.vectorPixelsToWorld(new Vec2(-w/2, h/2));

    sd.set(vertices, 4);

    // Define a fixture
    FixtureDef fd = new FixtureDef();
    fd.shape = sd;
    // Parameters that affect physics
    fd.density = 5;
    fd.friction = 0.5;
    fd.restitution = 0.1;

    body.createFixture(fd);
    
    body.setGravityScale(3);
    body.setLinearVelocity(new Vec2(0, 0));
    body.setAngularVelocity(0);
    body.setUserData(this);
  }

车轮构造器:

class Wheel {

  // We need to keep track of a Body and a radius
  Body body;
  float r;
  boolean isOnGround = false;

  Wheel(float x, float y, float r_) {
    r = r_;

    // Define a body
    BodyDef bd = new BodyDef();
    // Set its position
    bd.position = box2d.coordPixelsToWorld(x, y);
    bd.type = BodyType.DYNAMIC;
    body = box2d.world.createBody(bd);

    // Make the body's shape a circle
    CircleShape cs = new CircleShape();
    cs.m_radius = box2d.scalarPixelsToWorld(r);

    FixtureDef fd = new FixtureDef();
    fd.shape = cs;
    // Parameters that affect physics
    fd.density = 8;
    fd.friction = 5;
    fd.restitution = 0.1;

    // Attach fixture to body
    body.createFixture(fd);

    body.setUserData(this);
  }

应该使汽车移动的功能:

void accelerate() {
    // On est au sol, on accélère !
    wjArriere.enableMotor(true);
    wjArriere.setMotorSpeed(-maxCarSpeed);
4

0 回答 0