Can someone tell me whats wrong with my collision? After I added in the enemy parts it just went haywhire.
Here is the entire ball class code, please help me :’(
import java.awt.Rectangle;
import org.newdawn.slick.geom.Vector2f;
public class Ball {
//int diameter = 30;
int radius = 15;
float speed = 1f;
float maxSpeed = 3;
float angle;
Vector2f position;
Vector2f velo;
Vector2f accel;
public Ball(){
velo = new Vector2f(-1,1);
velo.scale(speed);
position = new Vector2f(640/2 - radius, 300);
accel = new Vector2f(0,0);
//accel.scale(speed);
}
public void move(Wall[] wall, Paddle paddle, Enemy enemy){
position.add(velo);
//for(int i = 0; i < wall.length; i++){
if(velo.y < 0){
float t = ((position.y - radius) - (wall[2].y + wall[2].height))/ velo.y;
float ballHitX = position.x + velo.x * t;
if(t >= 1.0){
if(ballHitX >= wall[2].x && ballHitX <= wall[2].x + wall[2].width){
velo.y = -velo.y;
accel = velo.scale(speed);
velo.add(accel);
if(velo.x > maxSpeed)
velo.x = maxSpeed;
if(velo.y > maxSpeed)
velo.y = maxSpeed;
System.out.println("hit top");
}
}
}
//}
if(velo.y > 0){
float t = ((position.y + radius) - paddle.position.y)/ velo.y;
float ballHitX = position.x + velo.x * t;
if(t >= 1.0){
if(ballHitX >= paddle.position.x && ballHitX <= paddle.position.x + paddle.width){
velo.y = -velo.y;
System.out.println("hit paddle");
}
}
}
//for(int i = 0; i < wall.length; i++){
if(velo.x < 0){
float t = ((position.x - radius) - (wall[0].x + wall[0].width))/ velo.x;
float ballHitY = position.y + velo.y * t;
if(t >= 1.0){
if(ballHitY >= wall[0].y && ballHitY <= wall[0].y + wall[0].height){
velo.x = -velo.x;
accel = velo.scale(speed);
velo.add(accel);
if(velo.x > maxSpeed)
velo.x = maxSpeed;
if(velo.y > maxSpeed)
velo.y = maxSpeed;
System.out.println("hit LWall");
}
}
}
if(velo.x > 0){
float t = ((position.x + radius) - wall[1].x)/ velo.x;
float ballHitY = position.y + velo.y * t;
if(t >= 1.0){
if(ballHitY >= wall[1].y && ballHitY <= wall[1].y + wall[1].height){
velo.x = -velo.x;
accel = velo.scale(speed);
velo.add(accel);
if(velo.x > maxSpeed)
velo.x = maxSpeed;
if(velo.y > maxSpeed)
velo.y = maxSpeed;
System.out.println("hit RWall");
}
}
}
//enemy col start
if(velo.y < 0){
float t = ((position.y - radius) - (enemy.y + enemy.height))/ velo.y;
float ballHitX = position.x + velo.x * t;
if(t >= 1.0){
if(ballHitX >= enemy.x && ballHitX <= enemy.x + enemy.width){
velo.y = -velo.y;
accel = velo.scale(speed);
velo.add(accel);
if(velo.x > maxSpeed)
velo.x = maxSpeed;
if(velo.y > maxSpeed)
velo.y = maxSpeed;
System.out.println("hit top");
}
}
}
if(velo.x < 0){
float t = ((position.x - radius) - (enemy.x + enemy.width))/ velo.x;
float ballHitY = position.y + velo.y * t;
if(t >= 1.0){
if(ballHitY >= wall[0].y && ballHitY <= enemy.y + enemy.height){
velo.x = -velo.x;
accel = velo.scale(speed);
velo.add(accel);
if(velo.x > maxSpeed)
velo.x = maxSpeed;
if(velo.y > maxSpeed)
velo.y = maxSpeed;
System.out.println("hit LWall");
}
}
}
if(velo.x > 0){
float t = ((position.x + radius) - enemy.x)/ velo.x;
float ballHitY = position.y + velo.y * t;
if(t >= 1.0){
if(ballHitY >= wall[1].y && ballHitY <= enemy.y + enemy.height){
velo.x = -velo.x;
accel = velo.scale(speed);
velo.add(accel);
if(velo.x > maxSpeed)
velo.x = maxSpeed;
if(velo.y > maxSpeed)
velo.y = maxSpeed;
System.out.println("hit RWall");
}
}
}
if(velo.y > 0){
float t = ((position.y + radius) - enemy.y)/ velo.y;
float ballHitX = position.x + velo.x * t;
if(t >= 1.0){
if(ballHitX >= paddle.position.x && ballHitX <= enemy.x + enemy.width){
velo.y = -velo.y;
accel = velo.scale(speed);
velo.add(accel);
if(velo.x > maxSpeed)
velo.x = maxSpeed;
if(velo.y > maxSpeed)
velo.y = maxSpeed;
System.out.println("hit paddle");
}
}
}
//enemy col end
}