2015-08-17 45 views
0

问题是精灵在身体开始移动时会抵消。 enter image description hereBox2d和LibGDX sprite与灯具不同步

我使用RUBE编辑器加载资产。 enter image description here

这是汽车的源代码:

public class Vehicle { 

// Vehicle settings 
public DriveMode driveMode = DriveMode.ALL_WHEEL_DIVE; 
public SuspensionSetting suspensionSetting = SuspensionSetting.LOW; 

private float suspensionFrequency = 5; 
private float torque = 10f; 
private float speedLimit = 6f; 
private float SPIN_FACTOR = 0.8f; 

public enum DriveMode { 
    ALL_WHEEL_DIVE, 
    REAR_WHEEL_DRIVE, 
    FRONT_WHEEL_DRIVE 
} 

public enum SuspensionSetting { 
    HIGH, 
    NORMAL, 
    LOW 
} 

// The body 
public Body body; 
private Sprite bodySprite; 

// Wheels 
public Body frontWheel; 
public Body rearWheel; 
private Sprite frontWheelSprite; 
private Sprite rearWheelSprite; 

// Temporary 
private Vector2 tempPosition = new Vector2(); 

public Vehicle(RubeScene scene) { 
    // Images 
    RubeImage bodyImage = scene.getNamed(RubeImage.class, "body-image").first(); 
    RubeImage fwImage = scene.getNamed(RubeImage.class, "front-wheel-image").first(); 
    RubeImage rwImage = scene.getNamed(RubeImage.class, "rear-wheel-image").first(); 

    // Body 
    Texture tempTexture; 
    tempTexture = new Texture(bodyImage.file); 
    bodySprite = new Sprite(tempTexture, tempTexture.getWidth(), tempTexture.getHeight()); 

    float tempWidth = bodyImage.width * PPM; 
    float tempHeight = bodyImage.height * PPM; 
    bodySprite.setSize(tempWidth, tempHeight); 
    bodySprite.setOrigin(tempWidth/2, tempHeight/2); 

    // Front wheel 
    tempTexture = new Texture(fwImage.file); 
    frontWheelSprite = new Sprite(tempTexture, tempTexture.getWidth(), tempTexture.getHeight()); 

    tempWidth = fwImage.width * PPM; 
    tempHeight = fwImage.height * PPM; 
    frontWheelSprite.setSize(tempWidth, tempHeight); 
    frontWheelSprite.setOrigin(tempWidth/2, tempHeight/2); 

    // Rear wheel 
    tempTexture = new Texture(rwImage.file); 
    rearWheelSprite = new Sprite(tempTexture, tempTexture.getWidth(), tempTexture.getHeight()); 

    tempWidth = rwImage.width * PPM; 
    tempHeight = rwImage.height * PPM; 
    rearWheelSprite.setSize(tempWidth, tempHeight); 
    rearWheelSprite.setOrigin(tempWidth/2, tempHeight/2); 

    body = bodyImage.body; 
    rearWheel = rwImage.body; 
    frontWheel = fwImage.body; 
} 

/** 
* Updates the positions and orientation of the sprites 
* @param delta elapsed time 
*/ 
public void update(float delta) { 
    // Update body 
    tempPosition.set(body.getWorldCenter().scl(PPM).sub(bodySprite.getWidth()/2, bodySprite.getHeight()/2)); 
    bodySprite.setPosition(tempPosition.x, tempPosition.y); 
    bodySprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees); 

    // Update front wheel 
    tempPosition.set(frontWheel.getWorldCenter()).scl(PPM).sub(frontWheelSprite.getWidth()/2, frontWheelSprite.getHeight()/2); 
    frontWheelSprite.setPosition(tempPosition.x, tempPosition.y); 
    frontWheelSprite.setRotation(frontWheel.getAngle() * MathUtils.radiansToDegrees); 

    // Update rear wheel 
    tempPosition.set(rearWheel.getWorldCenter()).scl(PPM).sub(rearWheelSprite.getWidth()/2, rearWheelSprite.getHeight()/2); 
    rearWheelSprite.setPosition(tempPosition.x, tempPosition.y); 
    rearWheelSprite.setRotation(rearWheel.getAngle() * MathUtils.radiansToDegrees); 

    // Limit the speed to 4 meters per second 
    if (body.getLinearVelocity().x > speedLimit) { 
     body.setLinearVelocity(speedLimit, body.getLinearVelocity().y); 
    } 
} 

/** 
* Renders the graphics to the screen 
* @param sb initialized sprite batch 
*/ 
public void render(SpriteBatch sb) { 
    bodySprite.draw(sb); 
    frontWheelSprite.draw(sb); 
    rearWheelSprite.draw(sb); 
} 

public void driveForward(float delta) { 

    // Rotate counter-clockwise 
    body.applyTorque(SPIN_FACTOR, true); 

    switch (driveMode) { 
     case ALL_WHEEL_DIVE: 
      frontWheel.applyTorque(-torque * delta, true); 
      rearWheel.applyTorque(-torque * delta, true); 
      break; 
     case REAR_WHEEL_DRIVE: 
      rearWheel.applyTorque(-torque * delta, true); 
      break; 
     case FRONT_WHEEL_DRIVE: 
      frontWheel.applyTorque(-torque * delta, true); 
      break; 
    } 
} 

public void driveBackward(float delta) { 

    // Rotate clockwise 
    body.applyTorque(-SPIN_FACTOR, true); 

    switch (driveMode) { 
     case ALL_WHEEL_DIVE: 
      frontWheel.applyTorque(torque * delta, true); 
      rearWheel.applyTorque(torque * delta, true); 
      break; 
     case REAR_WHEEL_DRIVE: 
      rearWheel.applyTorque(torque * delta, true); 
      break; 
     case FRONT_WHEEL_DRIVE: 
      frontWheel.applyTorque(torque * delta, true); 
      break; 
    } 
} 

public void toggleDriveMode() { 
    switch (driveMode) { 
     case ALL_WHEEL_DIVE: 
      driveMode = DriveMode.FRONT_WHEEL_DRIVE; 
      break; 
     case FRONT_WHEEL_DRIVE: 
      driveMode = DriveMode.REAR_WHEEL_DRIVE; 
      break; 
     case REAR_WHEEL_DRIVE: 
      driveMode = DriveMode.ALL_WHEEL_DIVE; 
      break; 
    } 
} 

public void toggleSuspensionSettings() { 
    switch (suspensionSetting) { 
     case HIGH: 
      suspensionSetting = SuspensionSetting.LOW; 
      suspensionFrequency = 6; 
      break; 
     case NORMAL: 
      suspensionSetting = SuspensionSetting.HIGH; 
      suspensionFrequency = 10; 
      break; 
     case LOW: 
      suspensionSetting = SuspensionSetting.NORMAL; 
      suspensionFrequency = 8; 
      break; 
    } 
    this.setSuspensionFrequency(); 
} 

private void setSuspensionFrequency() { 
    Array<JointEdge> joints = body.getJointList(); 
    for (int i = 0; i < joints.size; i++) { 
     Joint joint = joints.get(i).joint; 
     if(joint.getType() == JointDef.JointType.WheelJoint) { 
      ((WheelJoint)joint).setSpringFrequencyHz(suspensionFrequency); 
     } 
    } 
} 

}

我不知道为什么会这样。我尝试了不同类型的车辆的相同的代码,并产生相同的结果。夹具在图像之前移动。

回答

0

事实证明,渲染代码与更新代码混合在一起。