Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BaseObject {
- constructor(x, y, speed, angle, width, height) {
- this.x = x;
- this.y = y;
- this.speed = speed;
- this.angle = angle;
- this.width = width;
- this.height = height;
- this.color = "blue";
- }
- render(canvas) {
- canvas.save();
- let hw = this.width / 2;
- let hh = this.height / 2;
- canvas.translate(this.x + hw, this.y + hh);
- canvas.fillStyle = this.color;
- let rad = this.degToRad(this.angle);
- canvas.rotate(rad);
- canvas.fillRect(-hw, -hh, this.width, this.height);
- canvas.restore();
- }
- forward() {
- let d = this.theoremSin(this.angle + 90, this.speed);
- this.x += d.deltaX;
- this.y += d.deltaY;
- }
- theoremSin(angle, distance) {
- let alpha = angle;
- let beta = 90 - alpha;
- let gamma = Math.PI / 2;
- let deltaX = Math.sin(this.degToRad(beta)) * distance
- / Math.sin(gamma);
- let deltaY = Math.sin(this.degToRad(alpha)) * distance
- / Math.sin(gamma);
- return { deltaX, deltaY }
- }
- degToRad(deg) {
- return deg * Math.PI / 180;
- }
- collision() {
- }
- isPointInObject(point, object) {
- let angle = object.angle;
- let x0 = object.x;
- let y0 = object.y;
- let x1 = object.x + object.width;
- let y1 = object.y + object.height;
- let leftTopR = this.rotatePoint({x: x0, y: y0}, angle);
- //let rightBottomR = this.rotatePoint({x: x1, y: y1}, angle);
- console.log(`left top - ${JSON.stringify(leftTopR)}`);
- // let pointR = this.rotatePoint(point);
- // let leftTopR = this.rotatePoint({ x: object.x, y: object.y }, angle);
- // let d = this.theoremSin(angle + 90, Math.sqrt(w * w + h * h));
- // let rightBottom = { x: object.x + d.deltaX, y: object.y + d.deltaY }
- // let rightBottomR = this.rotatePoint(rightBottom, angle);
- }
- rotatePoint(point, angle) {
- let a = this.degToRad(angle);
- let x = point.x;
- let y = point.y;
- let x1 = x * Math.cos(a) + y * Math.sin(a);
- let y1 = - x * Math.sin(a) + y * Math.cos(a);
- return {x1, y1}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement