I am trying to develop my very first android game and it is game named "brick breaker". I believe everyone knows it.
Everything was fine until i came to the ball bouncing part.
To handle collisions I use this found code :
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]);
}
}
}
}
}
});
And this is how Ball.java class looks like:
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());
}
}
It works and it is pretty clear.
But the problem is that the ball always bounces in the same angle from all surfaces. I think that angle is 90 degrees.
I already found some information, but i can not adjust it to this project.
I hope my problem is clear.
P.S. game is based on AndEngine aaand the most important thing - sorry for my bad English :D
Have a nice day/night!
This is more of a physics questions than a programming question, but here we go anyway.
Your Current Rules
Here are the current rules as you described them in your program.
As it stands, your velocities will always be (100,100), (100,-100), (-100,100), or (-100,-100). All of these vectors are at 90 degree angles to one another, which explains the phenomena you are describing.
Improved Rules
Let's say you wanted some variance in your velocity. Try something like this:
You could break this down even further. The closer the ball gets to the left side of a block, the larger the change in X velocity should be. This should be more true to the game we all remember.
The most important change that will be required is knowing when you hit the left, middle, or right side of a block. It might be wise to store your bricks in "pieces", such that every brick is composed of three or more pieces. That way, when you collide with a piece, you'll know exactly which piece you hit.