xxxxxxxxxx
185
//I got a lot of the rockets/particle code from Daniel Schiffman, you can see his code here:https://editor.p5js.org/natureofcode/sketches/S1PaKpQOe
//You can find Schiffman's website here http://natureofcode.com
let lifetime; // How long should each generation live
let population; // Population
let population2; // Population
let lifecycle; // Timer for cycle of generation
let recordtime; // Fastest time to target
let target; // Target position
//let diam = 24; // Size of target
let obstacles2 = []; //an array list to keep track of all the obstacles!
let obstacles = []; //an array list to keep track of all the obstacles!
////images
function preload() {
person1 = loadImage('stickFigure.png');
person2 = loadImage('stickFigure2.png');
background = loadImage('background.png');
}
function setup() {
//Create Camera
createCanvas(320 * 1.8, 240 * 1.8);
capture = createCapture(VIDEO);
capture.size(320 * 1.8, 240 * 1.8);
capture.hide();
//create camera tracker
// load clmtrackr functions:
tracker = new clm.tracker(); // create a new clmtrackr object
tracker.init(); // initialize the object
tracker.start(capture.elt); // start tracking the video element capture.eltcapture.elt
// The number of cycles we will allow a generation to live
lifetime = 400;
// Initialize variables
lifecycle = 0;
recordtime = lifetime;
target = new Obstacle2(0, 0, 40, 40);
// Create a population with a mutation rate, and population max
let mutationRate = 0.01;
population2 = new Population2(mutationRate, 1 * 10);
population = new Population(mutationRate, 181 * 10);
// Create the obstacle course
obstacles2 = [];
//obstacles2.push(new Obstacle2(width / 2 - 100, height / 2, 200, 10));
obstacles = [];
push()
//rect(x,y, w,h)
//00, length, 10
//obstacles.push(new Obstacle(0, 300, 700, 10));
//obstacles.push(new Obstacle(433, 300, 267, 10));
pop()
}
function draw() {
image(background, 0, 0)
//call camera
//image(capture, 0, 0, 320*1.8, 240*1.8);
//where is the target?
//the target is.. the face?
//update tracking in draw
let positions = tracker.getCurrentPosition();
//background("pink")
//
if (positions.length > 0) {
noStroke();
fill(0, 0, 0);
ellipse(positions[62][0], positions[62][1], 200, 300);
//create target position linked to face pos
push()
target.position.x = positions[62][0]
target.position.y = positions[62][1]
fill(255);
//create face
ellipse(positions[27][0], positions[27][1], 40, 15)
ellipse(positions[32][0], positions[32][1], 40, 15)
pop()
ellipse(positions[27][0], positions[27][1], 10, 10)
ellipse(positions[32][0], positions[32][1], 10, 10)
let targetX = positions[62][0]
let targetY = positions[62][1]
//console.log(targetX, targetY) //log points of face
target.x = targetX;
target.y = targetY;
}
//TARGETS
recordtime = lifetime;
//display target
target.display();
//image(img, 0, 0, 700, 700);
//target.display();
push();
noStroke();
//blendMode(OVERLAY)
fill(188, 194, 248, 170)
rect(5, 5, 220, 110)
pop();
// Draw the start and target positions
// If the generation hasn't ended yet
if (lifecycle < lifetime) {
population2.live(obstacles2);
if ((population2.targetReached()) && (lifecycle < recordtime)) {
recordtime = lifecycle;
}
lifecycle++;
// Otherwise a new generation
} else {
lifecycle = 0;
population2.calcFitness();
population2.selection();
population2.reproduction();
}
//ROCKET
if (lifecycle < lifetime) {
population.live(obstacles);
if ((population.targetReached()) && (lifecycle < recordtime)) {
recordtime = lifecycle;
}
lifecycle++;
// Otherwise a new generation
} else {
lifecycle = 0;
population.calcFitness();
population.selection();
population.reproduction();
}
// Draw the obstacles
for (let i = 0; i < obstacles2.length; i++) {
obstacles2[i].display();
}
for (let i = 0; i < obstacles.length; i++) {
obstacles[i].display();
}
// Display some info
fill(0);
noStroke();
text("Generation #: " + population.getGenerations(), 10, 18);
text("Cycles left: " + (lifetime - lifecycle), 10, 36);
text("Record cycles: " + recordtime, 10, 54);
text("target reached Population 1 " + population.targetReached(), 10, 74);
text("target reached Population 2 " + population2.targetReached(), 10, 88);
//text("target reached Population 2 " + Rocket2.display(), 10, 96);
}
// Move the target if the mouse is pressed
// System will adapt to new target
function mousePressed() {
let positions = tracker.getCurrentPosition();
beginShape();
for (let i = 0; i < positions.length; i++) {
vertex(positions[i][0], positions[i][1]);
target.position.x = positions[62][0]
target.position.y = positions[62][1]
}
recordtime = lifetime;
}
// function mousePressed(){
// }
//if targetReached=true, restart pop