2013-09-10 54 views
1

我可能錯過了一些簡單的東西,不知道是什麼。我只是在練習和重新創造龐,看看它會如何。Box2D + LibGDX乒乓球,所有的身體「卡住」

我有這樣的:

package com.gibbo.pong; 

enter code here 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Screen; 
import com.badlogic.gdx.graphics.GL10; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.math.Vector2; 
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; 
import com.badlogic.gdx.physics.box2d.CircleShape; 
import com.badlogic.gdx.physics.box2d.World; 

public class GameScreen implements Screen { 

    // Object instances 
    Pong pong; 
    Paddle paddle; 
    Walls wall; 
    Ball ball; 
    OrthographicCamera cam; 
    public static World world; 
    Box2DDebugRenderer debug; 

    // Booleans for paddles 
    private boolean playerOne = false; 
    private boolean playerTwo = false; 

    // Booleans for walls 
    private boolean wallTop = false; 
    private boolean wallBottom = false; 

    // Fields for the balls 
    private boolean ballExists = false; 
    private int ballTotal; 

    //Array to hold ball objects 
    Ball[] ballArray = new Ball[3]; 

    // GameScreen default constructor 
    public GameScreen(Pong pong) { 
     this.pong = pong; 

     // Initiate the camera 
     cam = new OrthographicCamera(20f, 14f); 
     cam.position.set(10f, 7f, 0); 

     // Initiate the world, no gravity 
     world = new World(new Vector2(0, -10), true); 

     // Initiate the renderer 
     debug = new Box2DDebugRenderer(); 

     // Initiate the paddle 
     paddle = new Paddle(); 

     //Initiate the walls 
     wall = new Walls(); 

     //Initiate the ball 
     ball = new Ball(); 



    } 

    public void render(float delta) { 
     // Clear the screen and fill it black 
     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

     // Update the camera 
     cam.update(); 

     // Render the debug lines 
     debug.render(world, cam.combined); 

     // Check if paddles exist, if not create them 
     if (!playerOne) { 
      paddle.createPaddle(1, 7); 
      playerOne = true; 
      System.out.println("Creating player ones paddle"); 
     } 
     if (!playerTwo) { 
      paddle.createPaddle(19, 7); 
      playerTwo = true; 
      System.out.println("Creating player twos paddle"); 
     } 

     //Check if walls exist, if not create them 
     if(!wallBottom){ 
      wall.createWalls(0, 0.1f); 
      wallBottom = true; 
      System.out.println("Creating top wall"); 
     } 
     if(!wallTop){ 
      wall.createWalls(0, 13f); 
      wallTop = true; 
      System.out.println("Creating bottom wall"); 
     } 

     //Checks if ball exists 
     if(!ballExists){ 
      ball.createBall(10f, 7f); 
      ballExists = true; 
      if(ballTotal == 0){ 
      System.out.println("Creating ball number 1"); 
      ballTotal += 1; 
      }else{ 
       System.out.println("Creating ball number "+ballTotal + 1); 
      } 
     } 
     if(ball.ballBody.getPosition().x < 0){ 
      System.out.println("Player twos point"); 
      ball.destroyBall(); 
     } 

     boolean test = true; 
     if(test){ 
     ball.ballBody.setLinearVelocity(-10, 0); 
     System.out.println("Does this work?"); 
     } 



     // Simulate the world with frame rate 
     // Keep at bottom of render when possible 
     world.step(1/60, 6, 2); 
    } 

我使用其他3類,一個是球,一個牆,一個用於撥片。主要是練習如何讓他們一起工作,傳遞參數等等。這裏所寫的一切都是來自LibGDX站點和C++ box2d手冊的教程組合。

這是我的球類:

package com.gibbo.pong; 

import com.badlogic.gdx.physics.box2d.Body; 
import com.badlogic.gdx.physics.box2d.BodyDef; 
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; 
import com.badlogic.gdx.physics.box2d.CircleShape; 
import com.badlogic.gdx.physics.box2d.Fixture; 
import com.badlogic.gdx.physics.box2d.FixtureDef; 
import com.badlogic.gdx.physics.box2d.World; 

public class Ball { 

    //Instance of the world 
    World world; 

    //Body for the ball 
    Body ballBody; 

    //Fixture for the ball 
    Fixture ballFixture; 

    public void createBall(float posX, float posY){ 
     //Define a body for the ball 
     BodyDef ballBodyDef = new BodyDef(); 
     ballBodyDef.type = BodyType.DynamicBody; 
     ballBodyDef.position.set(posX, posY); 

     //Define a shape for the ball 
     CircleShape ballShape = new CircleShape(); 
     ballShape.setRadius(0.50f); 

     //Define a fixture for the ball 
     FixtureDef ballFixtureDef = new FixtureDef(); 
     ballFixtureDef.shape = ballShape; 
     ballFixtureDef.density = 1; 

     //Create a ball 
     ballBody = GameScreen.world.createBody(ballBodyDef); 
     ballFixture = ballBody.createFixture(ballFixtureDef); 

     ballShape.dispose(); 

     ballBody.setLinearVelocity(-10, 0.1f); 


    } 

相當簡單,可以很容易地在GameScreen類中被實施,但實踐和tidyness的緣故,我這個去了。我曾嘗試在這裏和球的GameScreen中設置LinearVelocity,我試圖模擬如果球碰到球拍時會發生什麼,球會如何反應,以及如果摧毀球外屏幕的方法會起作用。

但由於某種原因,一切都只是「卡住」。彷彿一切都是靜止的。我甚至嘗試在對象數組中創建球,所以我可以做balls[0].setxxxx,但這一切都很有趣。

我需要爲我的球和其他類創建構造函數嗎?我的槳,牆和球是否不斷被渲染並被迫保持在同一位置?

關於帶有打印方法的隨機if語句,只是測試以確定該方法是否已經達到,因此強制它通過使用該方法來運行。

任何地方我的失敗在這裏?

在此先感謝。

+0

這似乎是'world.step()'實際上並沒有工作,不知道爲什麼,試圖移動它並改變參數。徒勞無功。 – Gibbo

回答

1

拍攝我,結果是整數除法是可怕的。

我不得不改變:

world.step(1/60, 6, 2) 

到:

world.step(1f/60f, 6, 2) 

哇,這是煩人,卻又如此簡單的笑。