2014-12-04 38 views
0

我正在开发一款越野游戏,我是libgdx的新手。libgdx绑定多个部分纹理和东西

我由汽车,只有3个部分:机箱,后&前轮。我有一台“Zoomable”摄像机与底盘连接,底盘与车轮连接。

底盘工作正常,但不是车轮。

这里的问题:

  1. 我无法弄清楚如何计算车轮的√(正确)vector2把它在[Sprite.setPosition]方法
  2. 如何使我的车更快导致它从未> 94

Focus.java

public class Focus extends InputAdapter { 
    private Body chassis, rearWheel, frontWheel; 
    private WheelJoint leftAxis, rightAxis; 
    private float speed = 90f; 
    private Sprite spriteChassis, spriteRearWheel, spriteFrontWheel; 
    private float xOffSet = 5f; 
    private float yOffSet = -2f; 
    private float rwOffSet = 0; 
    private float fwOffSet = 0; 

    private float rwOffSetY = 0; 
    private float fwOffSetY = 0; 


    public float getOffSetX(){ return xOffSet; } 
    public float getOffSetY(){ return yOffSet; } 

    public Focus(World world, FixtureDef chassisFixtureDef, FixtureDef wheelFixtureDef, float x, float y) { 
     BodyDef bodyDef = new BodyDef(); 
     bodyDef.type = BodyDef.BodyType.DynamicBody; 
     bodyDef.position.set(x,y); 
     bodyDef.gravityScale = 1; 
     float width = 5.333f; 
     float height = 1.933f; 


     BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("data/focus.json")); 
     chassis = world.createBody(bodyDef); 
     loader.attachFixture(chassis, "focus", chassisFixtureDef, width); 
     String imgpath = loader.getImagePath("focus"); 
     chassis.createFixture(chassisFixtureDef); 
     chassis.setGravityScale(1.2f); 

     spriteChassis = new Sprite(new Texture(imgpath)); 
     spriteRearWheel = new Sprite(new Texture("tires.png")); 
     spriteFrontWheel = new Sprite(new Texture("tires.png")); 

     // Wheels 
     CircleShape wheelShape = new CircleShape(); 
     wheelShape.setRadius(height/6f); 

     wheelFixtureDef.shape = wheelShape; 

     rearWheel = world.createBody(bodyDef); 
     rearWheel.createFixture(wheelFixtureDef); 

     frontWheel = world.createBody(bodyDef); 
     frontWheel.createFixture(wheelFixtureDef); 


     // Axels 
     WheelJointDef axisDef = new WheelJointDef(); 
     axisDef.bodyA = chassis; 
     axisDef.bodyB = rearWheel; 
     rwOffSet = wheelShape.getRadius()*3.3f; 
     axisDef.localAnchorA.x = rwOffSet; 
     rwOffSetY = height/10.5f; 
     axisDef.localAnchorA.y = rwOffSetY; 
     axisDef.frequencyHz = chassisFixtureDef.density; 
     axisDef.localAxisA.set(Vector2.Y); 
     axisDef.maxMotorTorque = chassisFixtureDef.density * 24.5f; 

     leftAxis = (WheelJoint) world.createJoint(axisDef); 

//  right 
//  Axels 
     WheelJointDef axisDef2 = new WheelJointDef(); 
     axisDef2.bodyA = chassis; 
     axisDef2.bodyB = frontWheel; 
     axisDef2.localAnchorA.set(width, 0); 
     axisDef2.frequencyHz = chassisFixtureDef.density; 
     axisDef2.localAxisA.set(Vector2.Y); 
     fwOffSet = width-wheelShape.getRadius()*3.f; 
     axisDef2.localAnchorA.x = fwOffSet; 
     fwOffSetY = height/9f; 
     axisDef.localAnchorA.y = fwOffSetY; 
     axisDef2.maxMotorTorque = chassisFixtureDef.density * 24.5f; 
     rightAxis = (WheelJoint) world.createJoint(axisDef2); 

    } 



    @Override 
    public boolean keyDown(int keycode) { 

     switch(keycode) { 
      case Input.Keys.W: 
       leftAxis.enableMotor(false); 
       rightAxis.enableMotor(true); 
       rightAxis.setMotorSpeed(-speed); 
       break; 
      case Input.Keys.S: 
       leftAxis.enableMotor(false); 

       rightAxis.enableMotor(true); 
       rightAxis.setMotorSpeed(speed); 
       break; 
      case Input.Keys.SPACE: 
       leftAxis.enableMotor(true); 
       leftAxis.setMotorSpeed(0); 

       rightAxis.enableMotor(true); 
       rightAxis.setMotorSpeed(0); 
       break; 
     } 
     return true; 
    } 

    @Override 
    public boolean keyUp(int keycode) { 
     switch(keycode) { 

      case Input.Keys.SPACE: 
      case Input.Keys.W: 
      case Input.Keys.S: 
       leftAxis.enableMotor(false); 
       rightAxis.enableMotor(false); 
       break; 
     } 
     return true; 
    } 

    public Sprite getSpriteChassis(){ return spriteChassis; } 

    public Sprite getSpriteRearWheel() { return spriteRearWheel; } 

    public Sprite getSpriteFrontWheel() { return spriteFrontWheel; } 

    public Body getChassis() { return chassis; } 

    public Body getFrontWheel() { return frontWheel; } 

    public Body getRearWheel() { return rearWheel; } 
} 

SmallHill.java(屏幕)

public class SmallHill implements Screen { 


    private final float PIXELS_PER_METER = 15f;  // how many pixels to a meter 
    private final float TIME_STEP = 1/60f;  // 60 fps 
    private final float SPEED = 1/60f;   // speed constant 
    private final float MIN_ZOOM = .25f;   // How far in should we be able to zoom 
    private final float ANGULAR_MOMENTUM = .5f; 
    private final int VELOCITY_ITERATIONS = 8;  // copied from box2d example 
    private final int POSITION_ITERATIONS = 3;  // copied from box2d example 

    private World world; 
    private Box2DDebugRenderer debugRenderer; 
    private OrthographicCamera camera; 
    private Body ball; 
    private Focus focus; 


    private BitmapFont font; 

    private SpriteBatch batch;  
    private Texture texture; 


    @Override 
    public void show() { 
     batch = new SpriteBatch(); 
     font = new BitmapFont(); 
     font.setColor(Color.RED); 


     world = new World(new Vector2(0, -9.81f), true); 
     debugRenderer = new Box2DDebugRenderer(); 

     camera = new OrthographicCamera(); 
     camera.zoom = 1f; 

     BodyDef bodyDef = new BodyDef(); 

     // Shape 
     ChainShape groundShape = new ChainShape(); 
     groundShape.createChain(new Vector2[] {new Vector2(-1,24),new Vector2(0,14),new Vector2(25,14),new Vector2(50,10),new Vector2(100,5),new Vector2(150,12),new Vector2(155,10), new Vector2(200,22),new Vector2(225,22),new Vector2(226,22.15f),new Vector2(227,22),new Vector2(229,22.25f),new Vector2(350,22),new Vector2(385,24),new Vector2(389,25),new Vector2(390,24),new Vector2(395,25),new Vector2(398,24),new Vector2(400,25),new Vector2(401,48) }); 
     CircleShape ballShape = new CircleShape(); 
     ballShape.setRadius(1f); 
     ballShape.setPosition(new Vector2(-10, 16)); 

     // Fixture 

     FixtureDef fixtureDef = new FixtureDef(); 
     fixtureDef.shape = groundShape; 
     fixtureDef.friction = .8f; 
     fixtureDef.restitution = 0; 
     world.createBody(bodyDef).createFixture(fixtureDef); 

     fixtureDef.shape = ballShape; 
     fixtureDef.friction = 0.9f; 
     fixtureDef.restitution = .3f; 
     fixtureDef.density = 3; 
     bodyDef.type = BodyType.DynamicBody; 

     fixtureDef.density = 5; 
     fixtureDef.friction = .4f; 
     fixtureDef.restitution = .1f; 

     FixtureDef wheelFixtureDef = new FixtureDef(); 
     wheelFixtureDef.density = fixtureDef.density ; 
     wheelFixtureDef.friction = 2; 
     wheelFixtureDef.restitution = .7f; 

     focus = new Focus(world, fixtureDef, wheelFixtureDef, 50, 14); 

     wheelFixtureDef.shape.dispose(); 
     fixtureDef.shape.dispose(); 


     Gdx.input.setInputProcessor(new InputMultiplexer(new InputController() { 
      @Override 
      public boolean keyDown(int keycode) { 
       switch(keycode) { 
        case Input.Keys.ESCAPE: 
         dispose(); 
         break; 
        case Input.Keys.R: 
         camera.zoom = 1; 
         break; 
        case Input.Keys.PLUS: 
         camera.zoom = 10; 
         break; 
        case Input.Keys.MINUS: 
         camera.zoom = 1; 
         break; 
       } 
       return false; 
      } 



      @Override 
      public boolean scrolled(int amount) { 
       if(amount == -1 && camera.zoom <= MIN_ZOOM) { 
        camera.zoom = MIN_ZOOM; 
       } else { 
        camera.zoom += amount/PIXELS_PER_METER; 
       } 
       return false; 
      } 
     },focus)); 
    } 

    @Override 
    public void render(float delta) { 
     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     world.step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS); 
     camera.position.set(focus.getChassis().getWorldCenter().x,focus.getChassis().getWorldCenter().y,0); 

     camera.update(); 

     String x; 
     WheelJoint wj = (WheelJoint) focus.getChassis().getJointList().get(0).joint; 
     x = (int)Math.abs(wj.getJointSpeed())+""; 

     batch.begin(); 
     font.draw(batch, x, 20, 20); 

     focus.getSpriteChassis().setPosition((Gdx.graphics.getWidth()/2 - focus.getSpriteChassis().getWidth()/2) + focus.getOffSetX(), (Gdx.graphics.getHeight()/2 - focus.getSpriteChassis().getHeight()/2) + focus.getOffSetY()); 


     focus.getSpriteChassis().setRotation(focus.getChassis().getAngle() * MathUtils.radiansToDegrees); 
     focus.getSpriteChassis().setScale(1/camera.zoom); 
     focus.getSpriteChassis().draw(batch); 

     focus.getSpriteRearWheel().setPosition((Gdx.graphics.getWidth()/2 - focus.getSpriteChassis().getWidth()/2) , (Gdx.graphics.getHeight()/2 - focus.getSpriteChassis().getHeight()/2)); 
     focus.getSpriteFrontWheel().setPosition((Gdx.graphics.getWidth()/2 - focus.getSpriteChassis().getWidth()/2) , (Gdx.graphics.getHeight()/2 - focus.getSpriteChassis().getHeight()/2)); 
     //focus.getSpriteRearWheel().setPosition((Gdx.graphics.getWidth()/2 - focus.getSpriteChassis().getWidth()/2) + focus.getRwOffSet()*PIXELS_PER_METER*(1/camera.zoom), (Gdx.graphics.getHeight()/2 - focus.getSpriteChassis().getHeight()/2) + focus.getRwOffSetY()*PIXELS_PER_METER*(1/camera.zoom)); 
     //focus.getSpriteFrontWheel().setPosition((Gdx.graphics.getWidth()/2 - focus.getSpriteChassis().getWidth()/2)+ focus.getFwOffSet()*PIXELS_PER_METER*(1/camera.zoom) , (Gdx.graphics.getHeight()/2 - focus.getSpriteChassis().getHeight()/2) + focus.getFwOffSetY()*PIXELS_PER_METER*(1/camera.zoom)); 

     focus.getSpriteRearWheel().setRotation(focus.getRearWheel().getAngle() * MathUtils.radiansToDegrees); 
     focus.getSpriteFrontWheel().setRotation(focus.getFrontWheel().getAngle() * MathUtils.radiansToDegrees); 
     focus.getSpriteRearWheel().setScale(1/camera.zoom); 
     focus.getSpriteRearWheel().draw(batch); 
     focus.getSpriteFrontWheel().setScale(1/camera.zoom); 
     focus.getSpriteFrontWheel().draw(batch); 

     batch.end(); 

     debugRenderer.render(world, camera.combined); 

    } 

    @Override 
    public void resize(int width, int height) { 
     camera.viewportWidth = width/PIXELS_PER_METER; 
     camera.viewportHeight = height/PIXELS_PER_METER; 
    } 

    @Override 
    public void hide() { dispose(); } 

    @Override 
    public void pause() { } 

    @Override 
    public void resume() { } 

    @Override 
    public void dispose() { 
     world.dispose(); 
     debugRenderer.dispose(); 
     batch.dispose(); 
     font.dispose(); 
    } 
} 

谁能帮我理解吗?非常感谢 !!!

+0

你有3个身体,他们都有一个'getPosition()',你为什么不把它用于'Sprite'的位置?您可能需要一些小的偏移量,因为'身体''位置'是它的中心,就我所知,而'雪碧'的'位置'是它的较低的左边角落。 – Springrbua 2014-12-04 07:28:01

+0

是的,你是对的!我只是觉得有可能有​​一个更快的方式来处理它..谢谢, – ryanfunyeah 2014-12-04 07:36:14

+0

你这样做的方式似乎并不重要,因为你认为,汽车总是在相机的中心。这可能会导致逻辑和视图之间的一些差异。所以你应该将'Sprite'绑定到他们的“逻辑部分”,在这种情况下,Box2D' Body' – Springrbua 2014-12-04 07:42:51

回答

0

Box2D具有限速,约为2.0单位/次步长。
所以如果你每秒钟60次,你可以移动2 * 60 = 120单位/秒。
要提高最大速度,可以减少时间步长,即增加步数/秒。
例如,如果将其增加到30,则可以移动到2 * 120 = 240单位/秒。

另外请确保您使用仪表作为您的单位。 Box2D是以千克,米和秒为单位创建的,所以你也应该使用这些单位。