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();
- let p1 = {x: this.x + hw, y: this.y + hh}
- this.renderPoint(p1.x, p1.y, '#FFFF00');
- let result = this.isPointInObject(p1);
- console.log('Точка внутри? ' + result);
- }
- renderPoint(x, y, color) {
- canvas.fillStyle = color;
- canvas.beginPath();
- canvas.arc(x, y, 5, 0, Math.PI * 2, true);
- canvas.fill();
- }
- 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() {
- }
- // возвращает объект с полями x и y
- getLeftTopPoint() {
- let hw = this.width / 2;
- let hh = this.height / 2;
- let point = this.rotatePoint(-hw, -hh, this.angle);
- return {x: point.x + this.x + hw, y: point.y + this.y + hh}
- }
- // возвращает объект с полями x и y
- getRightBottomPoint() {
- let hw = this.width / 2;
- let hh = this.height / 2;
- let point = this.rotatePoint(hw, hh, this.angle);
- return {x: point.x + this.x + hw, y: point.y + this.y + hh}
- }
- isPointInObject(point) {
- let hw = this.width / 2;
- let hh = this.height / 2;
- let centerX = this.x + hw;
- let centerY = this.y + hh;
- let p1X = point.x - centerX;
- let p1Y = point.y - centerY;
- let p1 = this.rotatePoint(p1X, p1Y, -this.angle);
- p1X = p1.x + centerX;
- p1Y = p1.y + centerY;
- if (p1X >= this.x && p1X <= this.x + this.width
- && p1Y >= this.y && p1Y <= this.y + this.height) {
- return true;
- } else {
- return false;
- }
- }
- rotatePoint(x, y, angle) {
- let a = this.degToRad(angle);
- let x1 = x * Math.cos(a) - y * Math.sin(a);
- let y1 = x * Math.sin(a) + y * Math.cos(a);
- return {x: x1, y: y1}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement