Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
135
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.     }
  22.     forward() {
  23.         let d = this.theoremSin(this.angle + 90, this.speed);
  24.         this.x += d.deltaX;
  25.         this.y += d.deltaY;
  26.     }
  27.  
  28.     theoremSin(angle, distance) {
  29.         let alpha = angle;
  30.         let beta = 90 - alpha;
  31.         let gamma = Math.PI / 2;
  32.         let deltaX = Math.sin(this.degToRad(beta)) * distance
  33.                      / Math.sin(gamma);
  34.         let deltaY = Math.sin(this.degToRad(alpha)) * distance
  35.                      / Math.sin(gamma);
  36.         return { deltaX, deltaY }
  37.     }
  38.    
  39.     degToRad(deg) {
  40.         return deg * Math.PI / 180;
  41.     }
  42.     collision() {
  43.  
  44.     }
  45.  
  46.     isPointInObject(point, object) {
  47.         let angle = object.angle;
  48.         let x0 = object.x;
  49.         let y0 = object.y;
  50.         let x1 = object.x + object.width;
  51.         let y1 = object.y + object.height;
  52.         let leftTopR = this.rotatePoint({x: x0, y: y0}, angle);
  53.         //let rightBottomR = this.rotatePoint({x: x1, y: y1}, angle);
  54.         console.log(`left top - ${JSON.stringify(leftTopR)}`);
  55.  
  56.         // let pointR = this.rotatePoint(point);
  57.         // let leftTopR = this.rotatePoint({ x: object.x, y: object.y }, angle);
  58.         // let d = this.theoremSin(angle + 90, Math.sqrt(w * w + h * h));
  59.         // let rightBottom = { x: object.x + d.deltaX, y: object.y + d.deltaY }
  60.         // let rightBottomR = this.rotatePoint(rightBottom, angle);
  61.     }
  62.  
  63.     rotatePoint(point, angle) {
  64.         let a = this.degToRad(angle);
  65.         let x = point.x;
  66.         let y = point.y;
  67.         let x1 = x * Math.cos(a) + y * Math.sin(a);
  68.         let y1 = - x * Math.sin(a) + y * Math.cos(a);
  69.         return {x1, y1}
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement