package game.util;
import java.util.HashMap;
import math.Vector;
import game.baseTypes.PhysicalObject;
import game.baseTypes.RenderableObject;
public class CollisionCheckEvent extends ConstrictedEvent {
private RenderableObject source;
private HashMap<Object, Object> solvedCollisions;
public CollisionCheckEvent(RenderableObject source, HashMap<Object, Object> solvedCollisions) {
super(source.getPosition(), source.getBoundingRadius());
this.source = source;
this.solvedCollisions = solvedCollisions;
}
@Override
public void applyTo(PhysicalObject target) {
if (source == target || !(target instanceof RenderableObject)) {
return;
}
// line is the vector between the centers, from source to target
Vector line = position.clone().invert().add(target.getPosition());
double radsum = source.getBoundingRadius()
+ ((RenderableObject) target).getBoundingRadius();
if ((line.dot(line) < radsum * radsum)) {
if (source.equals(solvedCollisions.get(target))) {
return;
}
Vector impulseArm = line.clone().scale(source.getBoundingRadius() / radsum);
source.collideWith(target, source.getPosition().clone().add(impulseArm),
impulseArm.clone().normalize(), radsum - line.getLength());
solvedCollisions.put(source, target);
}
}
}