Android bouncing ball

自古美人都是妖i 提交于 2019-12-18 13:36:52

问题


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);
    }
}

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!


回答1:


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;

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;
}

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!




回答2:


Some comments on your actual code:

  • Both of these lines do exactly the same thing:

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

    likewise, both of these lines do the same thing:

    xPos += (-x * gravity);
    xPos -= (x * gravity);
    
  • You are not handling the cases -1 <= x <= 1 or -1 <= y <= 1

Some comments on the concepts:

  • Start with one ball.
  • You want the ball to have two separate velocities (both either positive or negative), one in the x-direction and one in the y-direction. Every frame, add those velocities to the ball's position.
  • Then add collision detection. Check each frame if the center of the ball is outside the screen. If it is, make it bounce (the easiest way to do this is to make the ball's y-velocity positive when it goes off the most-negative y-value displayed on the screen; and do similarly for the other screen-edges).
  • Improve the collision detection: see if you can figure out how to check if any part of the ball it outside the screen (hint: to check if the ball is off the left-side of the screen, you only need to check the left-most coordinate of the ball)
  • Then take gravity into consideration - gravity will be a constant number subtracted from the y-velocity every frame, until the y-velocity hits 0.



回答3:


Youtube video

Source code available at github.

  1. Your x coordinate should not be affected by gravity.
  2. I would advise against using accelerometers until you understand how the physics work for the simple case.

Break it up into stages. First, ensure that you can get the ball falling at a constant velocity.

Once you have that working, worry about the gravity causing the velocity to increase.

Once you have that working, worry about the intersection with walls and velocity changes. A simple algorithm for bouncing off the walls would be to multiply the x velocity by -1 when you hit a vertical wall, and multiply the y velocity by -1 when you hit a horizontal wall.



来源:https://stackoverflow.com/questions/4513902/android-bouncing-ball

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!