xxxxxxxxxx
295
/*
בתחילת המלחמה כשנדחה הסמסטר
.לימדתי קורסי העשרה בתכנות בזום
יום שישי אחד בבוקר רק אני באתי לשיעור
...אז נתתי לעצמי תרגיל מאתגר במיוחד
GPT הקוד שרץ פה לפניכם נכתב כולו על ידי
:לפי הוראות כאלו
א. כאשר אתם פותחים את הפה תצא יונה
ותעוף ותנחת על האצבע המורה שלכם
ב. אחרי שהיונה נוחתת תישמע המילה
שלום" באנגלית, עברית וערבית"
ג. כשפוערים את העיניים יצאו מהן קרני לייזר לכיוון
שאתם מפנים את הראש ואם הלייזר פוגע ביונה אז
היא עולה באש עם ציור של להבה וסאונד אפקט
!היזהרו לא לפגוע ביונה טרם הישמע השלום המיוחל
*/
let handpose;
let facemesh;
let video;
let handPredictions = [];
let facePredictions = [];
let pigeonPosition;
let targetPosition;
let isPigeonFlying = false;
let isMouthOpen = false;
let hasLanded = false;
let isPigeonOnFire = false;
let pigeonTriggered = false;
let thresh = 4;
let laserOn = false;
let willReset = false;
document.addEventListener('keyup', e => {
if (e.key == '=') thresh++
else if (e.key == '-' && thresh >= 0) thresh--
console.log(thresh)
})
let burnSound; // Declare a variable for the sound
function preload() {
burnSound = loadSound('https://cdn.freesound.org/previews/555/555519_10912485-lq.mp3'); // Replace with the path to your sound file
facemesh = ml5.faceMesh({flipHorizontal: true, runtime: 'mediapipe'}, () => {
handpose = ml5.handPose({flipHorizontal: true, runtime: 'mediapipe'});
}); // see: https://github.com/ml5js/ml5-next-gen/issues/69
}
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO, {flipped: true});
video.size(width, height);
facemesh.detectStart(video, results => { facePredictions = results; });
handpose.detectStart(video, results => { handPredictions = results; });
pigeonPosition = createVector(0, 0); // Initialize with a default position
video.hide();
describe(`All code written by GPT4 (except updating ml5 to ml5-next-gen):
1. When you open your mouth a pigeon flies out and lands on your index finger
2. When it lands you hear the word "peace" in English, Hebrew and Arabic. (note: voices need to be installed in your OS)
3. Open your eyes wide to get laser rays shooting out of them
4. If the laser hits the pigeon it will catch fire including a sound effect`)
gridOutput = function(){} // see: https://github.com/processing/p5.js/issues/7259#issuecomment-2518331223
}
function reset() {
handPredictions = [];
facePredictions = [];
isPigeonFlying = false;
isMouthOpen = false;
hasLanded = false;
isPigeonOnFire = false;
pigeonTriggered = false;
laserOn = false;
pigeonPosition = createVector(0, 0)
willReset = false;
}
function draw() {
image(video, 0, 0, width, height);
checkMouthOpen();
if (!facePredictions.length) reset()
laserOn = areEyesOpen(facePredictions);
if (laserOn && pigeonPosition) {
drawLaserRays();
}
if (handPredictions.length > 0) {
const indexFinger = handPredictions[0].index_finger_tip;
targetPosition = createVector(indexFinger.x, indexFinger.y);
}
updateFingerPosition();
if (isMouthOpen && !isPigeonFlying && facePredictions.length > 0) {
const mouth = facePredictions[0].keypoints[13]; // Adjust index for precise mouth location
pigeonPosition = createVector(mouth.x, mouth.y);
pigeonTriggered = true;
isPigeonFlying = true;
hasLanded = false;
isPigeonOnFire = false;
}
if (pigeonTriggered && !hasLanded && targetPosition) {
movePigeonToTarget();
} else if (hasLanded) {
movePigeonWithFinger();
}
drawPigeon(pigeonPosition.x, pigeonPosition.y);
}
function areEyesOpen(predictions) {
if (predictions.length > 0) {
const rightEyeUpper = predictions[0].keypoints[159];
const rightEyeLower = predictions[0].keypoints[145];
const leftEyeUpper = predictions[0].keypoints[386];
const leftEyeLower = predictions[0].keypoints[374];
const leftEyeOpen = dist(leftEyeUpper.x, leftEyeUpper.y, leftEyeLower.x, leftEyeLower.y) > thresh;
const rightEyeOpen = dist(rightEyeUpper.x, rightEyeUpper.y, rightEyeLower.x, rightEyeLower.y) > thresh;
return leftEyeOpen && rightEyeOpen;
}
return false;
}
function updateFingerPosition() {
if (handPredictions.length > 0) {
const indexFinger = handPredictions[0].index_finger_tip;
targetPosition = createVector(indexFinger.x, indexFinger.y);
}
}
function movePigeonToTarget() {
if (pigeonPosition.dist(targetPosition) > 10) {
pigeonPosition.lerp(targetPosition, 0.05);
} else {
hasLanded = true;
sayPeace();
}
}
function movePigeonWithFinger() {
pigeonPosition.set(targetPosition);
}
function drawPigeon(x, y) {
if (isPigeonOnFire) {
// Add slight random movement to the flame position
let flameX = x - 20 + random(-5, 5);
let flameY = y - 20 + random(-5, 5);
textSize(64);
text('🔥', flameX, flameY);
}
textSize(32);
text('🕊️', x, y);
}
function checkMouthOpen() {
if (facePredictions.length > 0) {
const upperLip = facePredictions[0].keypoints[13];
const lowerLip = facePredictions[0].keypoints[14];
const distance = dist(upperLip.x, upperLip.y, lowerLip.x, lowerLip.y);
isMouthOpen = distance > 10;
}
}
function linePoint(x1, y1, x2, y2, px, py) {
let d1 = dist(px, py, x1, y1);
let d2 = dist(px, py, x2, y2);
let lineLen = dist(x1, y1, x2, y2);
let buffer = 0.1; // Extra distance since line is not a perfect pixel in p5.js
if (d1 + d2 >= lineLen - buffer && d1 + d2 <= lineLen + buffer) {
return true;
}
return false;
}
function drawLaserRays() {
if (facePredictions.length > 0 && pigeonPosition) {
const rightEye = facePredictions[0].keypoints[159];
const leftEye = facePredictions[0].keypoints[386];
const nose = facePredictions[0].keypoints[6];
// Calculate head center
let headCenter = createVector((leftEye.x + rightEye.x) / 2, (leftEye.y + rightEye.y) / 2);
// Determine head orientation
let headOrientation = createVector(nose.x - headCenter.x, nose.y - headCenter.y);
headOrientation.normalize();
// Adjust laser direction by reversing both x and y for correct yaw and pitch
let laserDirection = p5.Vector.fromAngle(atan2(headOrientation.y, headOrientation.x) );
let leftLaserEnd = createVector(leftEye.x + laserDirection.x * 1000, leftEye.y + laserDirection.y * 1000);
let rightLaserEnd = createVector(rightEye.x + laserDirection.x * 1000, rightEye.y + laserDirection.y * 1000);
stroke(255, 0, 0);
line(leftEye.x, leftEye.y, leftLaserEnd.x, leftLaserEnd.y);
line(rightEye.x, rightEye.y, rightLaserEnd.x, rightLaserEnd.y);
// Check for collisions with the pigeon
checkLaserHit(leftLaserEnd, rightLaserEnd, leftEye, rightEye);
}
}
function checkLaserHit(leftLaserEnd, rightLaserEnd, leftEye, rightEye) {
if (!pigeonPosition) {
return;
}
const laserHitLeft = collideLineCircle(leftEye.x, leftEye.y, leftLaserEnd.x, leftLaserEnd.y, pigeonPosition.x, pigeonPosition.y, 32);
const laserHitRight = collideLineCircle(rightEye.x, rightEye.y, rightLaserEnd.x, rightLaserEnd.y, pigeonPosition.x, pigeonPosition.y, 32);
if ((laserHitLeft || laserHitRight) && isPigeonFlying) {
isPigeonOnFire = true;
if (!burnSound.isPlaying()) {
burnSound.play();
}
if (!willReset) {
willReset = true
setTimeout(reset, 15000)
}
}
}
function collideLineCircle(x1, y1, x2, y2, cx, cy, diameter) {
let inside1 = dist(x1, y1, cx, cy) < diameter / 2;
let inside2 = dist(x2, y2, cx, cy) < diameter / 2;
if (inside1 || inside2) return true;
let len = dist(x1, y1, x2, y2);
let dot = (((cx - x1) * (x2 - x1)) + ((cy - y1) * (y2 - y1))) / pow(len, 2);
let closestX = x1 + (dot * (x2 - x1));
let closestY = y1 + (dot * (y2 - y1));
let onSegment = linePoint(x1, y1, x2, y2, closestX, closestY);
if (!onSegment) return false;
let distX = closestX - cx;
let distY = closestY - cy;
let distance = sqrt((distX * distX) + (distY * distY));
return distance <= diameter / 2;
}
function linePoint(x1, y1, x2, y2, px, py) {
let d1 = dist(px, py, x1, y1);
let d2 = dist(px, py, x2, y2);
let lineLen = dist(x1, y1, x2, y2);
let buffer = 0.1;
return (d1 + d2 >= lineLen - buffer && d1 + d2 <= lineLen + buffer);
}
// Rest of your code remains the same
function sayPeace() {
let utteranceEn = new SpeechSynthesisUtterance('Peace');
utteranceEn.lang = 'en-US';
speechSynthesis.speak(utteranceEn);
let utteranceHe = new SpeechSynthesisUtterance('שלום');
utteranceHe.lang = 'he-IL';
speechSynthesis.speak(utteranceHe);
let utteranceAr = new SpeechSynthesisUtterance('سلام،');
utteranceAr.lang = 'ar-SA';
speechSynthesis.speak(utteranceAr);
}
// Placeholder collision detection function
// Replace this with a more accurate collision detection if necessary
function collidePointLine(px, py, x1, y1, x2, y2) {
const d1 = dist(px, py, x1, y1);
const d2 = dist(px, py, x2, y2);
const lineLen = dist(x1, y1, x2, y2);
return d1 + d2 == lineLen;
}