Another collision question

Back to The Future Reflection!

I asked a question on stackoverflow, and I got this for a answer: http://stackoverflow.com/questions/5476384/how-do-i-calculate-a-normal/5476665#5476665

Now, I am just lost, so the first thing I am going to ask is what all do I need to keep track of for the
Wall, and in what format, in order reflect things off it?

After talking to MatthiasM on IRC I got to this point, but it doesn’t seem to work :frowning: The ball goes right through the paddle.


if(velo.y > 0){
			float t = position.distance(paddle.position)/ velo.y;
			t -= size/2;
			
			if(t <= 1.0){
				if(velo.x * t >= paddle.position.x && velo.x * t <= paddle.position.x + paddle.width){
					velo.y = -velo.y;
				}
			}
		}

Correct code:


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

ARG! Why isn’t my top collision working right? (the ball goes up when under the paddle, and down when not)



if(velo.y < 0){
				float t = ((position.y - radius) - (wall[2].y + wall[2].height))/ velo.y;
				
				if(t <= 1.0){
				velo.y = -velo.y;
				}
			}


Fixed it!


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 >= paddle.position.x && ballHitX <= paddle.position.x + paddle.width){
						velo.y = -velo.y;
					//}
				}
			}
		//}
		
		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");
				}
			}
		}

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
	}

Fixed it! had to switch


if(Math.abs(t) >= 1.0){

to


if(Math.abs(t) <= 1.0){

+rep for solving your own problem :wink: