Package game.util

Source Code of game.util.CollisionCheckEvent

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

}
TOP

Related Classes of game.util.CollisionCheckEvent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.