/Hammy/src/collision/TopEdge.as
ActionScript | 45 lines | 36 code | 5 blank | 4 comment | 4 complexity | d6acb227944d71d1d36e7543c381a3f3 MD5 | raw file
- package collision
- {
- import math.Vec2;
- import dynamics.State;
-
- /**
- * ...
- * @author Peter Mackay
- */
- public class TopEdge extends Edge
- {
- private var x1:int;
- private var x2:int;
- private var y:int;
-
- public function TopEdge(x1:int, x2:int, y:int)
- {
- this.x1 = x1;
- this.x2 = x2;
- this.y = y;
- }
-
- override public function collide(from:State, to:State, radius:Vec2):CollisionResult
- {
- var top:Number = y - radius.y;
- if ((from.position.y <= top) && (to.position.y > top))
- {
- var t:Number = (top - from.position.y) / (to.position.y - from.position.y);
- var x:Number = (to.position.x - from.position.x) * t + from.position.x;
- var left:Number = x1 - radius.x;
- var right:Number = x2 + radius.x;
- if ((x >= left) && (x <= right))
- {
- var position:Vec2 = new Vec2(to.position.x, top);
- var velocity:Vec2 = new Vec2(to.velocity.x, 0);
- var state:State = new State(position, velocity);
- return new CollisionResult(state, t, Vec2.negativeY);
- }
- }
-
- return null;
- }
- }
-
- }