Android的弹跳球 [英] Android bouncing ball

查看:146
本文介绍了Android的弹跳球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我只是试图让从墙上屏幕周围的球反弹,应是由于减缓重力和反映(反弹),像一个正常的球会。 有人可以给一些基础知识和非常简单的实现了这一点? 其他的例子似乎有点过头,似乎超出了我想做的事情。 我试过这样的:

So i'm just trying to make a ball bounce around the screen which should slow down due to gravity and reflect (bounce) from the wall like a normal ball would. Can someone give some basics and VERY simple implementation of this? Other examples seem a bit "overdone" and seem to go beyond what I want to do. I've tried this:

public void updateLogic() {

    if (x < -1) {

        xPos += (-x * gravity);
    } else if (x > 1) {

        xPos -= (x * gravity);
    }
    if (y > 1) {

        yPos += (y * gravity);
    } else if (y < -1) {

        yPos -= (-y * gravity);
    }
}

这是我通过自己最接近的。 顺便x和y的值是从加速度计。 任何帮助将pciated多少AP $ P $,谢谢!

This is the closest I got by myself. By the way the x and y values are from the accelerometer. Any help would be much appreciated, thanks!

推荐答案

我认为你需要三样东西对于这一点,力量(x和y,你有),速度(称他们xVel和yVel)及位置( XPOS和yPos,你也有)。球的位置被更新(在最简单的方式)的条件:

I think you'll need 3 things for this, forces (x and y, which you have), velocities (call them xVel and yVel) and positions (xPos and yPos which you also have). The position of the ball is updated (in the simplest way) by:

xPos += dt*xVel; 
yPos += dt*yVel;

xVel += dt*x;
yVel += dt*y;

变量DT是的的时间步长的,它控制的速度有多快球会移动。如果设置过大,不过,该计划将是不稳定的!尝试DT = 0.001左右就开始并逐渐将其设置更高。

The variable 'dt' is the timestep, which controls how fast the ball will move. If this is set too large, though, the program will be unstable! Try dt = 0.001 or so to start and gradually set it higher.

然后,让球从墙壁反映,只是翻转速度,如果它击中墙壁:

Then, to get the ball to reflect from the walls, just flip the velocity if it hits a wall:

if (xPos > xMax) {
    xPos = xMax;
    xVel *= -1.0;
} else if (xPos < 0.0) {
    xPos = 0.0;
    xVel *= -1.0;
}

和相同的年。该'XPOS = ...'只是阻止球去关闭屏幕的边缘。如果您想球每次撞墙时间反弹少一点,改变-1.0到-0.9什么的(这是的恢复系数)。

and the same for y. The 'xPos = ...' is just to stop the ball going off the edge of the screen. If you'd like the ball to bounce a little less every time it hits a wall, change the '-1.0' to '-0.9' or something (this is the coefficient of restitution).

希望这应该是所有。祝你好运!

Hopefully that should be all. Good luck!

这篇关于Android的弹跳球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆