Guest User

Untitled

a guest
May 26th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. circleRect(circlePos, circleSize, rectPos, rectSize) {
  2. //its rectRect collision but it doesnt matter because reflection surface is always horizontal or vertical
  3. let r1 = {
  4. left: circlePos.x - circleSize.x/2,
  5. right: circlePos.x + circleSize.x/2,
  6. top: circlePos.y - circleSize.y/2,
  7. bottom: circlePos.y + circleSize.y/2
  8. };
  9. let r2 = {
  10. left: rectPos.x,
  11. right: rectPos.x + rectSize.x,
  12. top: rectPos.y,
  13. bottom: rectPos.y + rectSize.y
  14. };
  15. return !(r2.left > r1.right ||
  16. r2.right < r1.left ||
  17. r2.top > r1.bottom ||
  18. r2.bottom < r1.top);
  19. }
  20.  
  21. isOnOpenTile(pos: Vector, size: Vector) {
  22. let openTiles = this.getTiles('open');
  23. let result = false;
  24. openTiles.forEach(element => {
  25. if( this.circleRect(pos,size,element.pos,element.size) ){
  26. result = element;
  27. return;
  28. }
  29. });
  30. return result;
  31. }
  32.  
  33. circleRectGetCollisionNormal(c, r) {
  34. if(c.pos.y <= r.pos.y - (r.size.y/2)) return new Vector(0,-1);
  35. //Hit was from below the brick
  36. if(c.pos.y >= r.pos.y + (r.size.y/2)) return new Vector(0,1);
  37. //Hit was from above the brick
  38. if(c.pos.x < r.pos.x) return new Vector(1,0);
  39. //Hit was on left
  40. if(c.pos.x > r.pos.x) return new Vector(-1,0);
  41. //Hit was on right
  42. return false;
  43. }
  44.  
  45. getNewMoveVector(moveVector, normalVector) {
  46. normalVector = this.normalize(normalVector);
  47. let dot = (moveVector.x * moveVector.y) + (normalVector.x * normalVector.y);
  48. let dotProduct = new Vector(dot, dot);
  49. return moveVector.sub(dotProduct.mult(normalVector).mult(new Vector(2,2)));
  50. }
  51.  
  52. normalize(v) {
  53. let length = Math.sqrt((v.x*v.x) + (v.y*v.y));
  54. return new Vector(v.x/length,v.y/length);
  55. }
  56.  
  57. getMoveVectorOnCollision(circle) {
  58. let coll = this.isOnOpenTile( circle.pos, circle.size );
  59. if( coll != false) {
  60. let vector = this.circleRectGetCollisionNormal(circle, coll);
  61. return this.getNewMoveVector(circle.moveVector, vector);
  62. } else return false;
  63. }
  64.  
  65. sub(vector: Vector) {
  66. return new Vector(this.x - vector.x, this.y - vector.y);
  67. }
Add Comment
Please, Sign In to add comment