private static void r_rebound(PhysicActor actor, Rectangle rect, double dt) { /* * Employ a heuristic * ------------ * | 0 | * | 1 RECT 3 | * | 2 | * ------------ */ actor.set_last_obstacle_collided(rect); Vector actor_pos = actor.get_position().add(actor.get_velocity().multiply(dt)); Rectangle actor_mbr = actor.get().get_mbr(); double h[] = { actor_pos.y + actor_mbr.y1 - rect.y2, actor_pos.y + actor_mbr.y2 - rect.y1, actor_pos.x + actor_mbr.x2 - rect.x1, actor_pos.x + actor_mbr.x1 - rect.x2 }; for (int i = 0; i < 4; i++) h[i] = Math.abs(h[i]); // now the question is - which of those is the smallest? double smallest_val = Double.POSITIVE_INFINITY; int smallest_index = -1; for (int i = 0; i < 4; i++) if (smallest_val > h[i]) { smallest_val = h[i]; smallest_index = i; } assert (smallest_index != -1); // ok, smallest_index is that if (smallest_index == 0) // colliding with floor Rebound.actor_hit_floor(actor, rect.y2, dt); if (smallest_index == 1) // colliding with roof Rebound.actor_hit_roof(actor, rect.y1, dt); if (smallest_index == 2) { Rebound.actor_hit_rightside(actor, rect.x1, dt); if (actor.get_last_obstacle_collided() != rect) { actor.set_v_braked(true); actor.set_last_obstacle_collided(rect); } } if (smallest_index == 3) { Rebound.actor_hit_leftside(actor, rect.x2, dt); if (actor.get_last_obstacle_collided() != rect) { actor.set_v_braked(true); actor.set_last_obstacle_collided(rect); } } }
/** * Rebound an actor against a game world box. Actor must not be wholly contained within rect. * * @param actor the actor * @param rect rectangle describing the box game is taking place within * @param dt time delta */ public static void rebound(PhysicActor actor, Rectangle rect, double dt) { Rectangle actor_mbr = actor .get() .get_mbr() .translate(actor.get_velocity().multiply(dt).add(actor.get_position())); if (actor_mbr.y2 >= rect.y2) { Rebound.actor_hit_roof(actor, rect.y2, dt); } if (actor_mbr.y1 <= rect.y1) { Rebound.actor_hit_floor(actor, rect.y1, dt); } if (actor_mbr.x1 <= rect.x1) { Rebound.actor_hit_leftside(actor, rect.x1, dt); } if (actor_mbr.x2 >= rect.x2) { Rebound.actor_hit_rightside(actor, rect.x2, dt); } }