java - Box2D + LibGDX pong,所有主体 "stuck"

标签 java box2d

我可能错过了一些简单的东西,不确定是什么。我只是在练习并重新创建 Pong,看看它会如何发展。

我有这个:

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 类中实现,但为了练习和整洁,我选择了这个。我尝试在此处和球的 GameScreen 中设置 LinearVelocity,我试图模拟如果球击中 Racket 会发生什么、它会如何 react 以及在屏幕外销毁球的方法是否有效。

但由于某种原因,一切都“卡住了”。仿佛一切都是静止的。我什至尝试在对象数组中创建球,这样我就可以执行 balls[0].setxxxx 但这一切都很有趣。

我需要为我的球和其他类创建一个构造函数吗?我的 Racket 、墙壁和球是否只是不断渲染并被迫保持在同一位置?

关于其中包含 print 方法的随机 if 语句,只是测试是否达到了该方法,因此强制它通过使用该方法来运行。

我在这里有什么失败的地方吗?

提前致谢。

最佳答案

开枪打死我,结果发现整数除法太糟糕了。

我必须改变:

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

至:

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

哇,这很烦人,但又这么简单,哈哈。

关于java - Box2D + LibGDX pong,所有主体 "stuck",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18729797/

相关文章:

android - 使用 ANDROID NDK 编译时出错

java - php 和 java 之间具有零填充加密结果的不同 AES128

java - 动态改变jasper的textFieldExpression的class属性

C++ 含义 |= 和 &=

c++ - Box2D ContactListener 未检测到碰撞

iphone - box2d 凹体

java - 我的 Java 应用程序中的离线奖励系统

java - 在 Java 中创建受密码保护的 zip

java - 如何将 XML 格式的字符串存储在压缩字符串中并存储到内存中?

java - 圆-圆碰撞预测(后续)