1 import {Vec2, Box, World} from "planck";
2 import * as MENU from "./menu.js";
3 import PIDController from "./pid-controller.js";
5 // Get the canvas element
6 const canvas = document.getElementById("area");
7 const ctx = canvas.getContext("2d");
9 const world = World(Vec2(0, 0));
11 canvas.width = window.innerWidth;
12 canvas.height = window.innerHeight;
19 // Define the box properties
23 // Create a dynamic body for the box
24 const box = world.createDynamicBody(Vec2(1, -2.5));
26 shape: Box(boxWidth, boxHeight),
31 const pid = new PIDController();
34 canvas.addEventListener("click", function(event) {
35 targetX = (event.clientX - canvas.getBoundingClientRect().left) / PPM;
36 console.log("Updated targetX:", targetX);
39 // Render the box on the canvas
41 const position = box.getPosition();
42 const angle = box.getAngle();
45 ctx.clearRect(0, 0, canvas.width, canvas.height);
46 ctx.fillStyle = "white";
47 ctx.fillRect(targetX * PPM, (-position.y * PPM), 30, 30);
51 //ctx.translate(position.x * PPM, (-position.y * PPM));
53 ctx.fillStyle = "blue";
54 ctx.fillRect(position.x * PPM, (-position.y * PPM), boxWidth * PPM, boxHeight * PPM);
56 var r = pid.Update(1/60, position.x, targetX);
58 var force = {x: r, y: 0};
59 box.applyForce(force, box.getWorldCenter());
66 requestAnimationFrame(animate);