PageRenderTime 22ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Hammy/src/collision/TopEdge.as

https://bitbucket.org/peterm/hammy-as3
ActionScript | 45 lines | 36 code | 5 blank | 4 comment | 4 complexity | d6acb227944d71d1d36e7543c381a3f3 MD5 | raw file
  1. package collision
  2. {
  3. import math.Vec2;
  4. import dynamics.State;
  5. /**
  6. * ...
  7. * @author Peter Mackay
  8. */
  9. public class TopEdge extends Edge
  10. {
  11. private var x1:int;
  12. private var x2:int;
  13. private var y:int;
  14. public function TopEdge(x1:int, x2:int, y:int)
  15. {
  16. this.x1 = x1;
  17. this.x2 = x2;
  18. this.y = y;
  19. }
  20. override public function collide(from:State, to:State, radius:Vec2):CollisionResult
  21. {
  22. var top:Number = y - radius.y;
  23. if ((from.position.y <= top) && (to.position.y > top))
  24. {
  25. var t:Number = (top - from.position.y) / (to.position.y - from.position.y);
  26. var x:Number = (to.position.x - from.position.x) * t + from.position.x;
  27. var left:Number = x1 - radius.x;
  28. var right:Number = x2 + radius.x;
  29. if ((x >= left) && (x <= right))
  30. {
  31. var position:Vec2 = new Vec2(to.position.x, top);
  32. var velocity:Vec2 = new Vec2(to.velocity.x, 0);
  33. var state:State = new State(position, velocity);
  34. return new CollisionResult(state, t, Vec2.negativeY);
  35. }
  36. }
  37. return null;
  38. }
  39. }
  40. }