Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Refactor code formatting and optimize robot movement
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyAdam committed Jan 4, 2024
1 parent dca43b1 commit 2db7ba0
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 79 deletions.
28 changes: 14 additions & 14 deletions mini-mecha-code/example/1.mini
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 9 additions & 9 deletions mini-mecha-code/example/2.mini
Original file line number Diff line number Diff line change
@@ -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()
}
}
12 changes: 6 additions & 6 deletions mini-mecha-code/example/3.mini
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions mini-mecha-code/example/4.mini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let void entry () {
setSpeed(10)
loop getDistance() > 100
{
Forward 100
}
return
}
16 changes: 16 additions & 0 deletions mini-mecha-code/example/5.mini
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions mini-mecha-code/src/interpretor/interpretor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
29 changes: 24 additions & 5 deletions mini-mecha-code/src/simulator/entities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Vector, Ray } from "./utils.js";
import { Scene } from "./scene.js";


export interface Entities {
type: string;
pos: Vector;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
91 changes: 56 additions & 35 deletions mini-mecha-code/src/simulator/scene.ts
Original file line number Diff line number Diff line change
@@ -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<Entities.Timestamp>;
export interface Scene {
size: Vector;
entities: Entities.Entities[];
robot: Entities.Robot;
time: number;
timestamps: Array<Entities.Timestamp>;

reset():void;
reset(): void;
}

export class BaseScene{
size:Vector;
entities:Entities.Entities[] = [];
robot: Entities.Robot;
time:number = 0;
timestamps:Array<Entities.Timestamp> = [];
export class BaseScene {
size: Vector;
entities: Entities.Entities[] = [];
robot: Entities.Robot;
time: number = 0;
timestamps: Array<Entities.Timestamp> = [];

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
6 changes: 6 additions & 0 deletions mini-mecha-code/src/simulator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 4 additions & 10 deletions mini-mecha-code/static/simulator/robot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 2db7ba0

Please sign in to comment.