Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class BaseObject {
  2.     constructor(x, y, speed, angle, width, height) {
  3.         this.x = x;
  4.         this.y = y;
  5.         this.speed = speed;
  6.         this.angle = angle;
  7.         this.width = width;
  8.         this.height = height;
  9.         this.color = "blue";
  10.     }
  11.     render(canvas) {
  12.         canvas.save();
  13.         let hw = this.width / 2;
  14.         let hh = this.height / 2;
  15.         canvas.translate(this.x + hw, this.y + hh);
  16.         canvas.fillStyle = this.color;
  17.         let rad = this.degToRad(this.angle);
  18.         canvas.rotate(rad);
  19.         canvas.fillRect(-hw, -hh, this.width, this.height);
  20.         canvas.restore();
  21.         let p1 = {x: this.x + hw, y: this.y + hh}
  22.         this.renderPoint(p1.x, p1.y, '#FFFF00');
  23.         let result = this.isPointInObject(p1);
  24.         console.log('Точка внутри? ' + result);
  25.     }
  26.  
  27.     renderPoint(x, y, color) {
  28.         canvas.fillStyle = color;
  29.         canvas.beginPath();
  30.         canvas.arc(x, y, 5, 0, Math.PI * 2, true);
  31.         canvas.fill();
  32.     }
  33.  
  34.     forward() {
  35.         let d = this.theoremSin(this.angle + 90, this.speed);
  36.         this.x += d.deltaX;
  37.         this.y += d.deltaY;
  38.     }
  39.  
  40.     theoremSin(angle, distance) {
  41.         let alpha = angle;
  42.         let beta = 90 - alpha;
  43.         let gamma = Math.PI / 2;
  44.         let deltaX = Math.sin(this.degToRad(beta)) * distance
  45.                      / Math.sin(gamma);
  46.         let deltaY = Math.sin(this.degToRad(alpha)) * distance
  47.                      / Math.sin(gamma);
  48.         return { deltaX, deltaY }
  49.     }
  50.    
  51.     degToRad(deg) {
  52.         return deg * Math.PI / 180;
  53.     }
  54.     collision() {
  55.  
  56.     }
  57.  
  58.     // возвращает объект с полями x и y
  59.     getLeftTopPoint() {
  60.         let hw = this.width / 2;
  61.         let hh = this.height / 2;
  62.         let point = this.rotatePoint(-hw, -hh, this.angle);
  63.         return {x: point.x + this.x + hw, y: point.y + this.y + hh}
  64.     }
  65.  
  66.     // возвращает объект с полями x и y
  67.     getRightBottomPoint() {
  68.         let hw = this.width / 2;
  69.         let hh = this.height / 2;
  70.         let point = this.rotatePoint(hw, hh, this.angle);
  71.         return {x: point.x + this.x + hw, y: point.y + this.y + hh}
  72.     }
  73.  
  74.     isPointInObject(point) {
  75.         let hw = this.width / 2;
  76.         let hh = this.height / 2;
  77.         let centerX = this.x + hw;
  78.         let centerY = this.y + hh;
  79.         let p1X = point.x - centerX;
  80.         let p1Y = point.y - centerY;
  81.         let p1 = this.rotatePoint(p1X, p1Y, -this.angle);
  82.         p1X = p1.x + centerX;
  83.         p1Y = p1.y + centerY;
  84.         if (p1X >= this.x && p1X <= this.x + this.width
  85.             && p1Y >= this.y && p1Y <= this.y + this.height) {
  86.             return true;
  87.         } else {
  88.             return false;
  89.         }
  90.     }
  91.  
  92.     rotatePoint(x, y, angle) {
  93.         let a = this.degToRad(angle);
  94.         let x1 = x * Math.cos(a) - y * Math.sin(a);
  95.         let y1 = x * Math.sin(a) + y * Math.cos(a);
  96.         return {x: x1, y: y1}
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement