【问题标题】:brick breaker game ball bouncing破砖游戏球弹跳
【发布时间】:2014-09-07 16:22:23
【问题描述】:

我正在尝试开发我的第一个 android 游戏,它是名为“brick断路器”的游戏。 我相信每个人都知道。

一切都很好,直到我来到弹跳部分。

为了处理冲突,我使用这个找到的代码:

    scene.registerUpdateHandler(new IUpdateHandler() {
        public void reset() { }

        public void onUpdate(final float pSecondsElapsed) {
                if(ball.collidesWith(paddle)) {
                    ball.bounceWithRectangle(paddle);
                }
                else if (ball.getY() >= Game.getCAMERA_HEIGHT() - 30) {
                    scene.setBackground(new ColorBackground(255f, 0f, 0f));
                }
                else {
                    for (int i = 0; i < bricks.length; i++) {
                        for (int j = 0; j < bricks[0].length; j++) {
                            scene.setBackground(new ColorBackground(0f, 0f, 0f));
                            if(ball.collidesWith(bricks[i][j])) {
                                bricks[i][j].setPosition(CAMERA_HEIGHT+20, CAMERA_WIDTH+20);
                                scene.getTopLayer().removeEntity(bricks[i][j]);
                                ball.bounceWithRectangle(bricks[i][j]);

                            }
                        }
                    }
                }
        }
    });

这就是 Ball.java 类的样子:

package com.example.zaidimas;


import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import java.lang.Math;

public class Ball extends AnimatedSprite {
    float velocity = 100;
    int i =0;
    private Engine mEngine;

    public Ball(float positionX, float positionY, TiledTextureRegion positionTextureRegion, Engine mEngine) {
        super(positionX, positionY, positionTextureRegion);
        this.mEngine = mEngine;
    }


    protected void onManagedUpdate(final float pSecondsElapsed) {
        if(this.mX < 0) {
            this.setVelocityX(velocity);
        } else if(this.mX + this.getWidth() > Game.getCAMERA_WIDTH()) {
            this.setVelocityX(-velocity);
        }

        if(this.mY < 0) {
            this.setVelocityY(velocity);
        } else if(this.mY + this.getHeight() > Game.getCAMERA_HEIGHT()) {
            this.setVelocityY(-velocity);
        }

        super.onManagedUpdate(pSecondsElapsed);
    }

    public void bounceWithRectangle(Rectangle rectangle){
            this.setVelocityY(-this.getVelocityY());
    }


}

它有效,而且很清楚。

但问题是球总是以相同的角度从所有表面反弹。 我认为这个角度是90度。

  • 我很想知道如何让球以不同的角度反弹,如何 计算这些角度和所有其他有帮助的信息 让游戏更加逼真。

我已经找到了一些信息,但我无法将其调整为这个项目。

  • 也许有人可以更详细地向我解释一下,给一些 示例甚至帮助在此项目中修复它?

我希望我的问题很清楚。

附:游戏基于AndEngine aaand 最重要的是——对不起我的英语不好:D

祝你早日愉快!

【问题讨论】:

  • 我建议你对这里涉及的物理学做一些研究。在这些类型的碰撞中,反弹角始终与入射角相同。

标签: android math game-physics


【解决方案1】:

这更像是一道物理问题,而不是编程问题,但无论如何我们都要开始了。

您当前的规则

这是您在程序中描述的当前规则。

  • 初始 X 和 Y 速度为每秒 100 个单位
  • 当球撞到墙上时,取消 X 速度
  • 当球击中方块或天花板时,取消 Y 速度

就目前而言,您的速度将始终为 (100,100)、(100,-100)、(-100,100) 或 (-100,-100)。所有这些向量彼此成 90 度角,这解释了您所描述的现象。

改进的规则

假设你希望你的速度有一些变化。试试这样的:

  • 初始 X 和 Y 速度为每秒 100 个单位。
  • 当球撞到墙上时,取消 X 速度
  • 当球击中积木的左侧时,取消 Y 速度,并将 X 速度减小一个常数
  • 当球击中方块的右侧时,取消 Y 速度,并以恒定的量增加 X 速度
  • 当球击中方块或天花板的中间时,取消 Y 速度

您可以进一步细分。球越靠近积木的左侧,X 速度​​的变化应该越大。这应该更符合我们都记得的游戏。

需要进行的最重要的更改是知道您何时击中了街区的左侧、中间或右侧。将砖块存储在“块”中可能是明智的,这样每块砖都由三个或更多块组成。这样,当你与一块棋子相撞时,你就会确切地知道你击中的是哪一块。

【讨论】:

  • 这太棒了! “现象”解释得非常完美:D 感谢您的回答 - 现在我有很多事情要做!
猜你喜欢
  • 2020-05-22
  • 2020-05-23
  • 1970-01-01
  • 2022-07-21
  • 1970-01-01
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多