-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10cf80d
commit 1a61986
Showing
10 changed files
with
1,057 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
class Car { | ||
constructor() { | ||
this.carImg = carImg; | ||
this.velocity = p5.Vector.random2D(); | ||
this.genotype = new DNA(); | ||
this.r = 15; | ||
this.index = 0; | ||
this.pos = createVector(width/2, height - 325); | ||
this.angle = 0; | ||
this.rotateAmount = 0; | ||
this.alive = true; | ||
this.currentCheckpoint = 0; | ||
} | ||
|
||
die() { | ||
this.alive = false; | ||
} | ||
|
||
draw() { | ||
if (!this.alive) return; | ||
|
||
|
||
push(); | ||
translate(this.pos.x, this.pos.y); | ||
rotate(this.angle); | ||
|
||
if (this.wasBest) { | ||
tint(0, 255, 100); | ||
} | ||
imageMode(CENTER); | ||
image(carImg, 0, 0, this.r + 10, this.r); | ||
pop(); | ||
} | ||
|
||
update() { | ||
if (!this.alive) return; | ||
this.pos.x -= SPEED * cos(this.angle); | ||
this.pos.y -= SPEED * sin(this.angle); | ||
this.angle += this.genotype.genes[this.index]; | ||
this.index++; | ||
} | ||
|
||
calcFitness() { | ||
//this.genotype.calcFitness(this.index); | ||
this.genotype.calcFitness(this.currentCheckpoint); | ||
} | ||
|
||
crossover(partner) { | ||
let childCar = new Car(); | ||
childCar.genotype = this.genotype.crossover(partner.genotype); | ||
return childCar; | ||
} | ||
|
||
fitness() { | ||
return this.genotype.fitness; | ||
} | ||
|
||
mutate(mutationRate) { | ||
this.genotype.mutate(mutationRate); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class Checkpoints { | ||
constructor(points) { | ||
this.checkpoints = []; | ||
if (points) { | ||
this.checkpoints = points; | ||
} | ||
this.r = 20; | ||
} | ||
|
||
create(x, y) { | ||
this.checkpoints.push({x,y}); | ||
} | ||
|
||
delete() { | ||
this.checkpoints.pop(); | ||
} | ||
|
||
draw() { | ||
if (!SHOW_CHECKPOINTS) return; | ||
for (let checkpoint of this.checkpoints) { | ||
circle(checkpoint.x, checkpoint.y, this.r); | ||
} | ||
} | ||
|
||
hit(car) { | ||
if (this.checkpoints.length <= car.currentCheckpoint) return; | ||
let checkpoint = this.checkpoints[car.currentCheckpoint]; | ||
|
||
|
||
if (dist(car.pos.x, car.pos.y, checkpoint.x, checkpoint.y) <= this.r + car.r) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
save() { | ||
saveJSON(this.checkpoints, 'points.json'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class DNA { | ||
|
||
constructor() { | ||
this.genes = this.createGenes(); | ||
} | ||
|
||
|
||
createGenes() { | ||
let s = []; | ||
for (let j = 0; j < 10000; j++) { | ||
s[j] = random(-TURN_MAX, TURN_MAX); | ||
} | ||
return s; | ||
} | ||
|
||
calcFitness(currentCheckpoint) { | ||
// index is just the gene they were currently on | ||
// using this as an easy way to calc fitness | ||
// the higher the index, the further they moved the closer to the finish? | ||
// this.fitness = map(index, 0, 10000, 0, 1); | ||
|
||
this.fitness = map(currentCheckpoint, 0, 20, 0, 1); | ||
this.fitness = pow(this.fitness, 4); | ||
} | ||
|
||
crossover(partner) { | ||
|
||
let child = new DNA(); | ||
|
||
let midpoint = floor(random(this.genes.length)); | ||
|
||
for (let i = 0; i < this.genes.length; i++) { | ||
if (i > midpoint) { | ||
child.genes[i] = this.genes[i]; | ||
} | ||
else { | ||
child.genes[i] = partner.genes[i]; | ||
} | ||
} | ||
return child; | ||
} | ||
|
||
mutate(mutationRate) { | ||
for (let i = myFrameCount-400; i < this.genes.length; i++) { | ||
if (random(1) < mutationRate) { | ||
this.genes[i] = random(-TURN_MAX, TURN_MAX); | ||
} | ||
} | ||
} | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.2.0/p5.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.2.0/addons/p5.sound.min.js"></script> | ||
<link rel="stylesheet" type="text/css" href="style.css"> | ||
<meta charset="utf-8" /> | ||
|
||
</head> | ||
<body> | ||
<script src="Car.js"></script> | ||
<script src="sketch.js"></script> | ||
<script src="DNA.js"></script> | ||
<script src="Checkpoints.js"></script> | ||
</body> | ||
</html> |
Oops, something went wrong.