Everything makes alot more sense then it did last night… Like alot more sense. But there’s a problem with my code, anyone wanna help debug? It is calculating new points but I know their wrong because sometimes they are behind the collision line :(.
if(intersectionPoint.X != 0f && intersectionPoint.Y != 0f){//If Lines Intersect
System.out.println("Collision Has Occured!");
//Start point of Wall line
Vector2D collisionV1 = new Vector2D(wall.X - (wall.Width/2), wall.Z - (wall.Depth/2));
//End point of Wall line
Vector2D collisionV2 = new Vector2D(wall.X + (wall.Width/2), wall.Z - (wall.Depth/2));
//Subtract the two wall vectors
Vector2D surfaceVector = collisionV2.sub(collisionV1);
//Normalize the two wall vectors
surfaceVector = surfaceVector.normalize();
//Get the Normal
Vector2D normal = new Vector2D( ( surfaceVector.Y * -1), surfaceVector.X);
//Start point of intersecting line
Vector2D velocityVector1 = ballBehavior.toVector2D();
//End Point of the intersecting line
Vector2D velocityVector2 = ballBehavior.movePointForward(10f).toVector2D();
float vectorX = surfaceVector.dotProduct(surfaceVector, velocityVector1);
float vectorY = surfaceVector.dotProduct(surfaceVector, velocityVector2);
//Make the VectorY negative as to "flip" the collision
vectorY = -vectorY;
Vector2D mathVector = normal.mul(vectorY);//To simplify the final operation
Vector2D bouncedVector = surfaceVector.mul(vectorX).add(mathVector);
//Place on Object on the Point of the reflection
endPoint.X = bouncedVector.X;
endPoint.Z = bouncedVector.Y;
}
and maybe I messed up my Vector2D class…
public class Vector2D {
public float X = 0.0f, Y = 0.0f;
public float velocity = 1.0f;
public Vector2D() {}
public Vector2D( float x, float y ) { X = x; Y = y; }
public Vector2D( Vector2D a ) { X = a.X; Y = a.Y; }
public float length() {
return (float) Math.sqrt( (X * X) + (Y * Y) );
}
public Vector2D normalize(){
float len = length();
if (len > 0f || len < 0f) {
X /= len;
Y /= len;
}
return new Vector2D(X, Y);
}
public float dotProduct(Vector2D A, Vector2D B) {
return (A.X * B.X) + (A.Y * B.Y);
}
public Vector2D add( Vector2D a ) {
X += a.X;
Y += a.Y;
return new Vector2D(X, Y);
}
public Vector2D sub( Vector2D a ) {
X -= a.X;
Y -= a.Y;
return new Vector2D(X, Y);
}
public Vector2D mul( Vector2D vec ) {
X *= vec.X;
Y *= vec.Y;
return new Vector2D(X, Y);
}
public Vector2D mul( float scalar ) {
X *= scalar;
Y *= scalar;
return new Vector2D(X, Y);
}
}