xxxxxxxxxx
98
/*
my helper thing for https://www.codingame.com/ide/puzzle/search-race
im age8ugag9a438ugh9i4a3gh93ayhgpahygpa4gh dont understand a thing
reading to do https://math.stackexchange.com/a/2587852
*/
const cp_radius = 50;
const cp_coords = [[50,50],[300,300],[100,300]]
const checkpoints = [];
var ti = 0;
var car;
var speed;
var turnLerp;
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
turnLerp = createSlider(0,100,20);
for (coords of cp_coords) {
[cx, cy] = coords;
checkpoints.push(createVector(cx,cy));
}
car = createVector(200,200);
speed = createVector(1,1);
}
function draw() {
background(220);
// draw checkpoints
stroke(0);
strokeWeight(1);
for (cp of checkpoints){
point(cp.x, cp.y);
fill(0,0,100,30);
circle(cp.x,cp.y,cp_radius*2);
}
// get target checkpoint
var tc = checkpoints[ti];
var tn = checkpoints[ (ti+1) % checkpoints.length]
if (car.dist(tc) < cp_radius){
ti = ((ti+1) % checkpoints.length);
tc = checkpoints[ti];
}
var target_diff = createVector(tc.x - car.x, tc.y-car.y);
var angle_to_cp = speed.angleBetween(target_diff);
// how to limit this rotation speed? aka
// how to write this without the lerp i guess
var lerpitylerplerp = map(turnLerp.value(), 0,100,0.5,0.999);
speed.rotate(lerp(angle_to_cp, 0, lerpitylerplerp));
car.add(speed);
strokeWeight(10);
point(car.x, car.y);
strokeWeight(0);
fill(0);
text(`higher value is less turny ${map(turnLerp.value(), 0,100,0.5,0.999).toFixed(2)}`,5,height-5);
stroke(255,255,0,110);
strokeWeight(1);
line(car.x, car.y, tc.x,tc.y);
line(tc.x,tc.y ,tn.x, tn.y);
var next_angle =
// nevermind this. i dont get it .atan2(tn.y - car.y, tn.x - car.x) - atan2(tc.y - car.y, tc.x - car.x);
// law of cosines
// p1 = the current checkpoint
//https://stackoverflow.com/a/1211243/6934388
// i dont get this either but it works
acos(
((tc.dist(car)*tc.dist(car)) +
(tc.dist(tn)*tc.dist(tn)) - (car.dist(tn)*car.dist(tn) ))
/ (2 * tc.dist(car) * tc.dist(tn))
);
text(`${next_angle.toFixed(1)}`, tc.x, tc.y);
push();
translate(car.x,car.y);
stroke('red')
strokeWeight(2);
line(0,0, speed.x*25, speed.y*25);
pop();
}