나는 무엇인가 간단하지 않을 수도 있습니다. 나는 단지 그것이 어떻게 될 것인지보기 위해 Pong을 연습하고 다시 만들고 있습니다.Box2D + LibGDX 탁구, 모든 몸체가 "달라 붙음"
나는이있다 :
내가 3 개 다른 클래스, 공 하나, 벽 하나 노 하나를 사용하고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);
}
. 주로 그들이 함께 일하는 방법을 연습하고, 논증을 전달하는 등. 여기에 쓴 것은 모두 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을 위해, 나는이와 함께 갔다. 저는 LinearVelocity를 설정하려고 시도했습니다. GameScreen에서 볼을 볼 때, 볼이 패들에 닿았을 때 어떻게 될 것인지, 어떻게 반응하는지, 스크린에서 볼을 파괴하는 방법이 작동하는지 시뮬레이션하려고했습니다.
하지만 몇 가지 이유로 모든 것이 막 쌓여 있습니다. 마치 모든 것이 정적입니다. 나는 심지어 할 수있는 객체 배열에 공을 만들기를 시도했다 balls[0].setxxxx
하지만 그냥 모든 재미가 갔다.
공 및 다른 클래스의 생성자를 만들어야합니까? 내 외륜, 벽 및 공이 지속적으로 렌더링되고 동일한 위치에 있어야합니다.
인쇄 방법이 포함 된 임의의 if 문과 관련하여 해당 메서드에 도달했는지 여부를 테스트했기 때문에이를 사용하여 강제로 실행해야합니다.
여기 내 고장이 있습니까?
미리 감사드립니다.
'world.step()'이 실제로 작동하지 않는 것처럼 보입니다. 이유를 알아낼 수 없으며 인수를 변경하고 변경하려고 시도한 것 같습니다. 아무 소용이 없습니다. – Gibbo