/* Copyright 2010 Christian Matt
*
* This file is part of PonkOut.
*
* PonkOut is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PonkOut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PonkOut. If not, see <http://www.gnu.org/licenses/>.
*/
package ponkOut.logic;
import org.lwjgl.util.vector.Vector2f;
/**
* stores information about a collision between a Ball and another Entity and
* methods for collision handling
*/
public abstract class Collision {
protected float time;
protected Ball ball;
public Collision(float time, Ball ball) {
this.time = time;
this.ball = ball;
}
/** update velocities of entities taking part in collision */
public abstract void apply();
/**
* Reflect ball at line perpendicular to connection line between the balls
*
* @param ball
* ball to reflect. ball.velocity will be modified.
* @param fixedBallPosition
* position of the ball to reflect the ball on
*/
protected void reflectAtBall(Ball ball, Vector2f fixedBallPosition) {
// let n be the vector connecting the balls (n is orthogonal to
// reflection line)
Vector2f n = new Vector2f();
Vector2f.sub(fixedBallPosition, ball.getPosition(), n);
n.normalise();
// project velocity onto n and scale by 2
n.scale(2.0f * Vector2f.dot(ball.getVelocity(), n));
Vector2f newVel = new Vector2f();
// add -projected velocity to velocity
Vector2f.sub(ball.getVelocity(), n, newVel);
ball.setVelocity(newVel);
}
}