Android bouncing ball - java

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!

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!

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.

Youtube video
Source code available at github.
Your x coordinate should not be affected by gravity.
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.

Related

How to make an object move from one point to another via coordinates?

I currently have a 2d game in which I am trying to move a .png image following certain coordinates. If I where to place this object at (0,0) and then try to move to (500,500), I could do the following:
public void move() {
if (x < 500) {
x += velocityX;
}
if (y < 500) {
y += velocityY;
}
}
This would work, but only because its a perfectly diagonal line. With that system, I can only move the object horizontally, vertically, or diagonally, but never in between. If I were to try something like:
public void move() {
if (x < 500) {
x += velocityX;
}
if (y < 400) {
y += velocityY;
}
}
The object would just move in a perfect diagonal angle until it reached 400 on the y axis, it would move horizontally for the last bit to reach 500 on the x axis. How would I make it so that I could move the object to any coordinates while following a straight path?
To move perfect diagonally in your window, you must make sure that the ratio of velocityX and velocityY` should be like this,
velocityX width
----------- = -----------
velocityY height
in Your case if width is 500 , height is 400 and if velocityX is 5, then
velocityY should be 4

Java bounce physics : never ending bounce

I have recently started programming games in java. I come from a C# / XNA background and i already have experience in game development.
However I have a problem in my java game. I have a bouncing script which makes a in-game "particle" bounce upon collision with a solid surface, such as the ground.
Here is a snippet of the code that manages the bouncing.
private final void respondY(Rectangle r)
{
if(!r.intersects(getBounds())) // If not colliding, return
return;
if(Yvel > 0) // If falling...
{
if(r.y < Y + Height) //Another collision test
{
Y = r.y - Height; // Set Y to top of object
Yvel *= -bounce; // Bounce (Here is the problem)
onFloor = true;
}
}
else if(Yvel < 0) // If hit ceiling
{
if(Y < r.y + r.height) // Collision test
{
Y = r.y + r.height; // Set Y to bottom of object
Yvel = 0; // No bouncing here
}
}
}
The problem is that the object bounces upon the floor as it should, but after a while the object bounces constantly at the same height where I want it to stop bouncing when it got to that constant height.
NOTE:
Yvel is a int that is the vertical velocity of the object
Bounce is a float that controls how "bouncy" a object is. e.g. 0.5 means that it bounces half as high as it fell
Thanks in advance! Please note that this is my first post so if I make mistakes please point them out in a constructive manner.
The float (and double) types are imprecise. This especially causes problems when dealing with very small numbers - in fact there's a smallest representable number and a finite quantity of possible numbers that can be represented (and bit like quantum physics). The number stored by a float is actually the closest number possible to represent to the calculation result.
What's happening is the velocity calculation never reaches zero, because the result of multiplying the smallest number float can represent by a value >= .5 is itself.
I would force it to zero by putting a low threshold on the calculation result:
Yvel = Yvel * -bounce;
if (Yvel < .000001F)
Yvel = 0;

Sync shape's rotation to it's movement direction

While practicing wanted to create basic game-kind-of(?) - there is just a basic shape (rectangle) and you move forward with one button, move backwards with another, and rotate it with 2 additional buttons, that shape as well should move to other direction accordingly to it's rotation. What i have done so far is:
Input handling:
if (input.isKeyDown(Input.KEY_UP)) {
rectX += Math.cos(Math.toRadians(rotation)) * (0.2*delta);
rectY += Math.sin(Math.toRadians(rotation)) * (0.2*delta);
}
if (input.isKeyDown(Input.KEY_DOWN)) {
rectX -= Math.cos(Math.toRadians(rotation)) * (0.2*delta);
rectY -= Math.sin(Math.toRadians(rotation)) * (0.2*delta);
}
if(input.isKeyDown(Input.KEY_LEFT)){
rotation-=(0.1*delta);
rectangle = rectangle.transform(Transform.createRotateTransform(rotation, rectX, rectY));
if(rotation <= 0f) {
rotation = 360f;
}
}
if(input.isKeyDown(Input.KEY_RIGHT)){
rotation+=(0.1*delta);
rectangle = rectangle.transform(Transform.createRotateTransform(rotation, rectX, rectY));
if(rotation >= 360f) {
rotation = 0f;
}
}
rectangle.setCenterX(rectX);
rectangle.setCenterY(rectY);
}
rectX, rectY are obviously center coordinates of rectangle (floats)
and rotation is... well rotation..
And I think that's it, other code isn't worth mentioning.
In my opinion this should work, but the thing is while movement direction is changin at normal speed, my rectangle is spinning wildly, probably about 100 times or so faster than its movement direction.
Delta is the number of milliseconds since the last call to update().

Moving Object to certain location

I have a method here that will either add or subtract the location to get to the set location that a person needs to get to if the box needs to move to the left it works great but if it needs to move to the right it doesn't seem to work I know there is a problem with it I just can't figure it out
public void doMovements(float delta) {
if (!movements.isEmpty()) {
Vector2 vec = movements.get(0);
if (pos.x > vec.x)
pos.x -= VELOCITY * delta;
else if (pos.x < vec.x)
pos.x += VELOCITY * delta;
System.out.println(pos.x);
if (pos.x - vec.x < CLOSE_VEC) {
movements.remove(movements.get(0));
System.out.println(movements.size());
}
}
rect.y = pos.y;
rect.x = pos.x;
}
I need to get from point a to point b if the box has to move left it works perfectly but if it has to move right it breaks thanks for helping!
If you calculated delta by the difference of two values, it is probably negative sometimes. They way you have it written, the code assumes that delta is always positive. Have you made sure that the delta is an absolute value.
If delta is positive and negative, then you don't need two separate statements. You do with just one:
pos.x += VELOCITY * delta;
Just make sure that delta is negative when it should be, and positive when it should be
I figured out the problem I was checking if it was at the correct lane incorrectly
I needed to change
if (pos.x - vec.x < CLOSE_VEC) {
To
if (!(pos.x - vec.x < 0) && pos.x - vec.x < CLOSE_VEC) {
I need to make sure that the value isn't negative so the rectangle can move right.

Thrust movement on single axis, very strange behaviour

I've noticed some very strange behaviour with a simple piece of code, I've stripped it down to remove any variables, time-step, acceleration, etc.
I have a shape on the screen, pressing the left side of the screen thrusts the shape right, pressing the right side thrusts left. For some reason the shape favours moving left (speed < 0). It seems to be that it decelerates properly when moving right but when moving left it finishes decelerating then continues for maybe 0.3 seconds before stopping.
float speed;
int x;
Update method for the shape:
speed *= 0.9f;
if (Math.abs(speed) < 0.1f)
speed = 0;
if (Gdx.input.isTouched()) {
if (Gdx.input.getX() < Gdx.graphics.getWidth() / 2) {
speed = 10;
} else {
speed = -10;
}
}
x += speed;
x should be defined as float. I used int because the viewport is set to the screen size but finally realised it needs to have the precision.

Categories