两个矩形之间的碰撞 - libGDX

时间:2016-06-22 11:41:53

标签: android libgdx rotation collision-detection

我制作的游戏目标是收集最大数量的球(矩形)以获得最高分。球从智能手机的每一侧中间随机产生,你使用屏幕中间的方块(每次触摸屏幕时方块向右旋转90度)得到球。正方形(矩形)有三个黑色边和一个黄色边。

我想在这里完成的是如果一个球击中方块的黄色边,你得到一分,但如果一个球撞到了方形的黑色边,你将输掉比赛。 / p>

这是我到目前为止所拥有的(已经有碰撞,旋转和球产生):

@Override
public void create(){

    batch = new SpriteBatch();

    Ball = new Texture("energyball.png");
    Up = new Texture("up.png");

    upSprite = new Sprite(Up);

    upSprite.setOriginCenter();
    upSprite.setX(615);
    upSprite.setY(340);
    upSprite.setRegionWidth(64);
    upSprite.setRegionHeight(64);

    square = new Rectangle();

    square.set(630, 360, 32, 32);

    cam = new OrthographicCamera();
    cam.setToOrtho(false, 1280, 720);

    upSprite.setPosition(upSprite.getX(), upSprite.getY());

    // calls the functions to spawn balls randomly
    balls1 = new Array<Rectangle>();
    spawnBalls1();

    balls2 = new Array<Rectangle>();
    spawnBalls2();

    balls3 = new Array<Rectangle>();
    spawnBalls3();

    balls4 = new Array<Rectangle>();
    spawnBalls4();

    score();


    //if the screen is touched sprite rotates 90 degrees clockwise
    Gdx.input.setInputProcessor(new InputAdapter() {

        @Override
        public boolean touchDown(int x, int y, int pointer, int button) {

            return true;
        }

        @Override
        public boolean touchUp(int x, int y, int pointer, int button) {

            upSprite.rotate(-90);
            return true;
        }
    });


}


//shows score
private void score() {

    score = 0;
    showScore = "Score: 0";
    scoreFont = new BitmapFont();

}


//creates the balls and sets their position as well as the random timer for each
private void spawnBalls1() {

    ball1.x = MathUtils.random(639, 641);
    ball1.y = 720;
    ball1.width = 32;
    ball1.height = 32;
    balls1.add(ball1);
    lastDropTime = TimeUtils.nanoTime();

}

private void spawnBalls2() {

    ball2.x = 0;
    ball2.y = MathUtils.random(359, 361);
    ball2.width = 32;
    ball2.height = 32;
    balls2.add(ball2);
    lastDropTime = TimeUtils.nanoTime();
}

private void spawnBalls3() {

    ball3.x = MathUtils.random(639, 641);
    ball3.y = 0;
    ball3.width = 32;
    ball3.height = 32;
    balls3.add(ball3);
    lastDropTime = TimeUtils.nanoTime();
}

private void spawnBalls4() {

    ball4.x = 1280;
    ball4.y = MathUtils.random(359, 361);
    ball4.width = 32;
    ball4.height = 32;
    balls4.add(ball4);
    lastDropTime = TimeUtils.nanoTime();
}

@Override
public void render() {

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    cam.update();

    //draws the game itself as well as the balls on the screen
    batch.setProjectionMatrix(cam.combined);
    batch.begin();

    upSprite.draw(batch);

    //draws the balls
    for (Rectangle ball1 : balls1) {

        batch.draw(Ball, ball1.x, ball1.y);
    }

    for (Rectangle ball2 : balls2) {

        batch.draw(Ball, ball2.x, ball2.y);
    }

    for (Rectangle ball3 : balls3) {

        batch.draw(Ball, ball3.x, ball3.y);
    }

    for (Rectangle ball4 : balls4) {

        batch.draw(Ball, ball4.x, ball4.y);
    }

    scoreFont.setColor(1, 1, 1, 1);
    scoreFont.draw(batch, showScore, 25, 100);

    batch.end();

    // if the time minus the time of the last ball spawn is less than x then spawn another ball in a random place

    if (TimeUtils.nanoTime() - lastDropTime > 1000000000) {

        switch (MathUtils.random(4)) {

            case 0:
                spawnBalls1();
                break;

            case 1:
                spawnBalls2();
                break;

            case 2:
                spawnBalls3();
                break;

            case 3:
                spawnBalls4();
                break;
        }
    }

    Iterator<Rectangle> iter1 = balls1.iterator();
    while(iter1.hasNext()) {

        Rectangle balls1 = iter1.next();
        balls1.y -= 350 * Gdx.graphics.getDeltaTime();
        if (balls1.overlaps(square)) {

            score++;
            showScore = "Score: " + score;
            iter1.remove();
        }

    }

    Iterator<Rectangle> iter2 = balls2.iterator();
    while (iter2.hasNext()) {

        Rectangle balls2 = iter2.next();
        balls2.x += 550 * Gdx.graphics.getDeltaTime();
        if (balls2.overlaps(square)) {
            score++;
            showScore = "Score: " + score;
            iter2.remove();
        }

    }


    Iterator<Rectangle> iter3 = balls3.iterator();
    while(iter3.hasNext()) {

        Rectangle balls3 = iter3.next();
        balls3.y += 350 * Gdx.graphics.getDeltaTime();
        if (balls3.overlaps(square)) {

            score++;
            showScore = "Score: " + score;
            iter3.remove();
        }
    }

    Iterator<Rectangle> iter4 = balls4.iterator();
    while(iter4.hasNext()) {

        Rectangle balls4 = iter4.next();
        balls4.x -= 550 * Gdx.graphics.getDeltaTime();
        if (balls4.overlaps(square)) {

            score++;
            showScore = "Score: " + score;
            iter4.remove();
        }

    }

}

如果有人能帮助我,真的很高兴。谢谢!

2 个答案:

答案 0 :(得分:0)

我现在无法正确编写代码,但您可以使用此逻辑,

  1. 在黄色上创建线对象以捕捉碰撞
  2. 每当你转过方,也改变这条线的位置
  3. 我希望,我理解你的需要并且可以提供帮助。

答案 1 :(得分:0)

我无法让你成为一个例子,但我希望你能用这种方法解决问题。

Intersector

public static boolean intersectRaySphere (Ray ray, Vector3 center, float radius, Vector3 intersection) {
    final float len = ray.direction.dot(center.x - ray.origin.x, center.y - ray.origin.y, center.z - ray.origin.z);
    if (len < 0.f) // behind the ray
        return false;
    final float dst2 = center.dst2(ray.origin.x + ray.direction.x * len, ray.origin.y + ray.direction.y * len,
        ray.origin.z + ray.direction.z * len);
    final float r2 = radius * radius;
    if (dst2 > r2)
        return false;
    if (intersection != null)
        intersection.set(ray.direction).scl(len - (float)Math.sqrt(r2 - dst2)).add(ray.origin);
    return true;
}