Hello, I’m new to p5.js and I’m trying to convert what I learned in a tutorial from the Coding Train into a NextJS app with React and Typescript.
I have this Vehicle class but when I try to use any Vector methods I get that Vector is not found:
import * as p5 from "p5";
export default class Vehicle {
pos: p5.Vector;
target: p5.Vector;
vel: p5.Vector;
acc: p5.Vector;
r: number;
maxspeed: number;
maxforce: number;
constructor(p5: p5, x: number, y: number) {
this.pos = p5.createVector(p5.random(p5.width), p5.random(p5.height));
this.target = p5.Vector.random2D();
this.vel = p5.createVector(10,20);
this.acc = p5.createVector();
this.r = 15; //radius
this.maxspeed = 5;
this.maxforce = 0.1;
}
behaviors(p5: p5) {
let arrive = this.arrive(p5, this.target);
this.applyForce(arrive);
}
applyForce(force) {
this.acc.add(force);
}
seek(p5:p5, target) {
let desired = p5.Vector.sub(target, this.pos);
desired.setMag(this.maxspeed);
let steer = p5.Vector.sub(desired, this.vel);
steer.limit(this.maxforce);
return steer;
}
arrive(p5: p5, target: p5.Vector) {
let desired = p5.Vector.sub(target, this.pos);
let distance = desired.mag();
let speed = this.maxspeed;
if (distance < 100) {
speed = p5.map(distance, 0, 100, 0, this.maxspeed);
}
desired.setMag(speed);
let steer = p5.Vector.sub(desired, this.vel);
steer.limit(this.maxforce);
return steer;
}
update() {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
}
show(p5: p5) {
p5.stroke(255);
p5.strokeWeight(5);
p5.point(this.pos.x, this.pos.y);
}
}
according to my package.json I’m using : “@types/p5”: “^1.4.2”, and “react-p5”: “^1.3.30”, as a wrapper. any idea of how to make this work?