From 2db7ba0bc2d20d8ae8ec1649f2283bd0743db7bc Mon Sep 17 00:00:00 2001 From: Cody Adam Date: Thu, 4 Jan 2024 19:00:59 +0100 Subject: [PATCH] Refactor code formatting and optimize robot movement --- mini-mecha-code/example/1.mini | 28 +++--- mini-mecha-code/example/2.mini | 18 ++-- mini-mecha-code/example/3.mini | 12 +-- mini-mecha-code/example/4.mini | 8 ++ mini-mecha-code/example/5.mini | 16 ++++ .../src/interpretor/interpretor.ts | 1 + mini-mecha-code/src/simulator/entities.ts | 29 +++++- mini-mecha-code/src/simulator/scene.ts | 91 ++++++++++++------- mini-mecha-code/src/simulator/utils.ts | 6 ++ mini-mecha-code/static/simulator/robot.js | 14 +-- 10 files changed, 144 insertions(+), 79 deletions(-) create mode 100644 mini-mecha-code/example/4.mini create mode 100644 mini-mecha-code/example/5.mini diff --git a/mini-mecha-code/example/1.mini b/mini-mecha-code/example/1.mini index ea0639a..ab1fde2 100644 --- a/mini-mecha-code/example/1.mini +++ b/mini-mecha-code/example/1.mini @@ -1,19 +1,19 @@ let void entry () { - var number count = 0 - loop count < 5 - { - count = count + 1 - square() - } + var number count = 0 + loop count < 5 + { + count = count + 1 + square() + } } let void square(){ - Forward 30 cm - Clock 90 - Forward 300 mm - Clock 90 - Forward 30 cm - Clock 90 - Forward 300 mm - Clock 90 + Forward 30 cm + Clock 90 + Forward 300 mm + Clock 90 + Forward 30 cm + Clock 90 + Forward 300 mm + Clock 90 } diff --git a/mini-mecha-code/example/2.mini b/mini-mecha-code/example/2.mini index 3b25cb4..0c8d4f9 100644 --- a/mini-mecha-code/example/2.mini +++ b/mini-mecha-code/example/2.mini @@ -1,11 +1,11 @@ let void entry () { - setSpeed(200 mm) // distance per second (here 200mm/s) - var number time = getTimestamp() - loop time < 60000 - { - var number dist = getDistance() in cm - Forward dist - 25 in cm - Clock 90 - time = getTimestamp() - } + setSpeed(200 mm) // distance per second (here 200mm/s) + var number time = getTimestamp() + loop time < 60000 + { + var number dist = getDistance() in cm + Forward dist - 25 in cm + Clock 90 + time = getTimestamp() + } } \ No newline at end of file diff --git a/mini-mecha-code/example/3.mini b/mini-mecha-code/example/3.mini index 9988281..6c361a9 100644 --- a/mini-mecha-code/example/3.mini +++ b/mini-mecha-code/example/3.mini @@ -3,19 +3,19 @@ let void entry () { setSpeed(10) loop count < 4 { - Forward 3000 - Clock 90 + Forward 3000 + Clock 90 - count = count + 1 + count = count + 1 } Clock 180 Forward 1500 count = 0 loop count < 20 { - Forward 400 - Clock 18 - count = count + 1 + Forward 400 + Clock 18 + count = count + 1 } return } diff --git a/mini-mecha-code/example/4.mini b/mini-mecha-code/example/4.mini new file mode 100644 index 0000000..887481d --- /dev/null +++ b/mini-mecha-code/example/4.mini @@ -0,0 +1,8 @@ +let void entry () { + setSpeed(10) + loop getDistance() > 100 + { + Forward 100 + } + return +} diff --git a/mini-mecha-code/example/5.mini b/mini-mecha-code/example/5.mini new file mode 100644 index 0000000..e4b3820 --- /dev/null +++ b/mini-mecha-code/example/5.mini @@ -0,0 +1,16 @@ +let void entry () { + var number count = 0 + setSpeed(10) + Clock 30 + loop count < 500 + { + loop getDistance() < 500 + { + Clock 5 + } + Forward 500 + + count = count + 1 + } + return +} diff --git a/mini-mecha-code/src/interpretor/interpretor.ts b/mini-mecha-code/src/interpretor/interpretor.ts index db2d9c0..391362f 100644 --- a/mini-mecha-code/src/interpretor/interpretor.ts +++ b/mini-mecha-code/src/interpretor/interpretor.ts @@ -278,6 +278,7 @@ function evaluateExpression( * @return {number} - The distance between the robot and the closest obstacle. */ function handleGetDistance(scene: Scene): number { + console.log(scene.robot.getForwardDist()); return scene.robot.getForwardDist(); } diff --git a/mini-mecha-code/src/simulator/entities.ts b/mini-mecha-code/src/simulator/entities.ts index 7e9fcaa..eaeb2f7 100644 --- a/mini-mecha-code/src/simulator/entities.ts +++ b/mini-mecha-code/src/simulator/entities.ts @@ -1,7 +1,6 @@ import { Vector, Ray } from "./utils.js"; import { Scene } from "./scene.js"; - export interface Entities { type: string; pos: Vector; @@ -32,11 +31,31 @@ export class Robot implements Entities { } getForwardDist(): number { - return 100; + // get all walls + const walls = this.scene.entities.filter( + (x) => x.type === "Wall" + ) as Wall[]; + + const forwardRay = this.getRay(); + + // find the nearest wall that the forward ray intersects + let minDist = Infinity; + for (const wall of walls) { + const intersectionPoints = wall.intersect(forwardRay); + for (const point of intersectionPoints) { + const dist = this.pos.dist(point); + if (dist < minDist) { + minDist = dist; + } + } + } + + return minDist; } intersect(ray: Ray): Vector[] { - return [] as Vector[]; + const poi = ray.getPoiFinder()(this.pos, this.size); + return poi ? ([poi] as Vector[]) : ([] as Vector[]); } turn(angle: number): void { @@ -114,8 +133,8 @@ export class Block implements Entities { export class Wall implements Entities { type: string = "Wall"; - pos: Vector; - size: Vector; + pos: Vector; // top left corner + size: Vector; // bottom right corner constructor(p1: Vector, p2: Vector) { this.pos = p1; diff --git a/mini-mecha-code/src/simulator/scene.ts b/mini-mecha-code/src/simulator/scene.ts index 23ea548..3788029 100644 --- a/mini-mecha-code/src/simulator/scene.ts +++ b/mini-mecha-code/src/simulator/scene.ts @@ -1,44 +1,65 @@ -import * as Entities from "./entities.js" -import { Vector } from "./utils.js" +import * as Entities from "./entities.js"; +import { Vector } from "./utils.js"; -export interface Scene{ - size:Vector; - entities:Entities.Entities[]; - robot: Entities.Robot; - time:number; - timestamps:Array; +export interface Scene { + size: Vector; + entities: Entities.Entities[]; + robot: Entities.Robot; + time: number; + timestamps: Array; - reset():void; + reset(): void; } -export class BaseScene{ - size:Vector; - entities:Entities.Entities[] = []; - robot: Entities.Robot; - time:number = 0; - timestamps:Array = []; +export class BaseScene { + size: Vector; + entities: Entities.Entities[] = []; + robot: Entities.Robot; + time: number = 0; + timestamps: Array = []; - constructor(size:Vector = new Vector(10000,10000)){ - this.size = size; - this.robot = new Entities.Robot(this.size.scale(0.5), new Vector(250,250), 0, 30, this) - this.entities.push(new Entities.Wall(Vector.null(), this.size.projX())); - this.entities.push(new Entities.Wall(Vector.null(), this.size.projY())); - this.entities.push(new Entities.Wall(this.size, this.size.projY())); - this.entities.push(new Entities.Wall(this.size, this.size.projX())); - this.timestamps.push(new Entities.Timestamp(0, this.robot)); - } + constructor(size: Vector = new Vector(10000, 10000)) { + this.size = size; + this.robot = new Entities.Robot( + this.size.scale(0.5), + new Vector(250, 250), + 0, + 30, + this + ); + this.entities.push(new Entities.Wall(Vector.null(), this.size.projX())); + this.entities.push(new Entities.Wall(Vector.null(), this.size.projY())); + this.entities.push(new Entities.Wall(this.size, this.size.projY())); + this.entities.push(new Entities.Wall(this.size, this.size.projX())); + this.timestamps.push(new Entities.Timestamp(0, this.robot)); + } - reset(){ - this.robot = new Entities.Robot(this.size.scale(0.5), new Vector(250,250), 0, 30, this) - this.entities = []; - this.entities.push(new Entities.Wall(Vector.null(), this.size.projX())); - this.entities.push(new Entities.Wall(Vector.null(), this.size.projY())); - this.entities.push(new Entities.Wall(this.size, this.size.projY())); - this.entities.push(new Entities.Wall(this.size, this.size.projX())); - this.timestamps = []; - this.timestamps.push(new Entities.Timestamp(0, this.robot)); - this.time = 0; - } + reset() { + this.robot = new Entities.Robot( + this.size.scale(0.5), + new Vector(250, 250), + 0, + 30, + this + ); + this.entities = []; + const X = this.size.x; + const Y = this.size.y; + this.entities.push(new Entities.Wall(Vector.null(), this.size.projX())); + this.entities.push(new Entities.Wall(Vector.null(), this.size.projY())); + this.entities.push(new Entities.Wall(this.size, this.size.projY())); + this.entities.push(new Entities.Wall(this.size, this.size.projX())); + this.entities.push( + new Entities.Wall(new Vector(0.4 * X, Y), new Vector(0.2 * X, 0.7 * Y)) + ); + // opposite side : + this.entities.push( + new Entities.Wall(new Vector(0.6 * X, 0), new Vector(0.8 * X, 0.3 * Y)) + ); + this.timestamps = []; + this.timestamps.push(new Entities.Timestamp(0, this.robot)); + this.time = 0; + } } // You can add new Scenes here diff --git a/mini-mecha-code/src/simulator/utils.ts b/mini-mecha-code/src/simulator/utils.ts index f771054..a4f1e9c 100644 --- a/mini-mecha-code/src/simulator/utils.ts +++ b/mini-mecha-code/src/simulator/utils.ts @@ -29,6 +29,12 @@ export class Vector { return new Vector(this.x * factor, this.y * factor); } + dist(other: Vector): number { + const dx = other.x - this.x; + const dy = other.y - this.y; + return Math.sqrt(dx * dx + dy * dy); + } + projX(): Vector { return new Vector(this.x, 0); } diff --git a/mini-mecha-code/static/simulator/robot.js b/mini-mecha-code/static/simulator/robot.js index 4f9456e..97cdcd3 100644 --- a/mini-mecha-code/static/simulator/robot.js +++ b/mini-mecha-code/static/simulator/robot.js @@ -35,20 +35,14 @@ class Robot { translate(canvasX, canvasY); rotate(this.angle); stroke(255, 255, 255); + fill(255, 255, 255); const realWidth = this.width * zoom; const realHeight = this.height * zoom; rect(-realHeight / 2, -realWidth / 2, realHeight, realWidth); - stroke(255, 0, 0); - fill(255, 0, 0); + stroke(171, 157, 242); + fill(171, 157, 242); const h = (Math.sqrt(3) / 2) * (realWidth / 3); - triangle( - -0.5 * h, - -(realHeight / 6), - -0.5 * h, - realHeight / 6, - 0.5 * h, - 0 - ); + triangle(-0.5 * h, -(realHeight / 6), -0.5 * h, realHeight / 6, 0.5 * h, 0); pop(); }