오늘은 seek-flee-pursue-evade-arrive 5종 종합선물세트 결정판!!!을 만들어 보도록 하겠습니다~^^*
어제는 함수 arrive()가 arrive 행동 전체 표현을 독립적으로 수행했었는데요~^^*
오늘은,
함수 flee()가 함수 seek()를 호출한 것처럼,
함수 pursue()가 함수 seek()를 호출한 것처럼,
함수 evade()가 함수 seek()를 호출하는 함수 pursue()를 호출한 것처럼,
함수 arrive()도 함수 seek()를 호출하도록 만들어 보겠습니다~^^*
네~~^^*
함수 seek()는 함수 flee()와 pursue()와 evade()와 arrive()의 근간이 되어 주네요~^^*
모든 길은 로마로 향하고 모든 함수는 seek()로 향한다~~~!!!^^*
아래 동영상 강의를 클릭하시면 오늘 공부내용을 바로 시작(6:05)하실 수 있어요~^^*
어제 만들어 함수 arrive()의 핵심 내용을 함수 seek()에 담아 보도록 하겠습니다~^^*
함수 seek()와 flee()와 pursue()와 evade()에 의한 pursuer의 움직임에서는 속력의 변화가 없었습니다. 함수 arrive()만이 유일하게, target에 가까이 갈수록 속력을 줄이게 되는데요.
함수 arrive()의 속력감소의 특성을 어떻게 함수 seek()에 담아볼 수 있을까요? 흠….
함수 seek()가 메인함수 draw()나 Class 소속함수 flee()나 pursue()의 호출을 받아 작동할 때는 평소처럼 등속운동을 하고, 함수 arrive()의 호출을 받을 경우만! 예외적으로! 감속운동을 하도록 만들면 어떨까요?
네~^^* 디폴트는 등속운동~~^^* 함수 arrive()의 호출의 경우에만 감속운동~~^^*
그럼 우리 핵심 코드를 살펴 볼까요~^^*
하나. 함수 arrive의 호출여부에 대한 정보를 함수 seek()의 인자로 추가하겠습니다.
둘. 함수 seek()는 평소에는 “함수 arrive의 호출이 아니다”를 default로 설정해 놓겠습니다.
셋. 함수 arrive는 함수 seek()를 호출하면서 “함수 arrive의 호출이 맞다”를 인자로 전달하겠습니다.
넷. default 상황이면 등속운동, 함수 arrive() 호출이면 감속운동을 하겠습니다.
arrive(target) {
return this.seek(target, true);
}
seek(target, arrival = false) {
let force = p5.Vector.sub(target, this.pos);
let desiredSpeed = this.maxSpeed;
if (arrival) {
let slowRadius = 100;
let distance = force.mag();
if (distance < slowRadius) {
desiredSpeed = map(distance, 0, slowRadius, 0, this.maxSpeed);
}
}
force.setMag(desiredSpeed);
force.sub(this.vel);
force.limit(this.maxForce);
return force;
}
이제 전체 코드를 살펴 보도록 할게요~^^*
<!DOCTYPE html>
<html>
<head>
https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.2.0/p5.min.js
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8" />
</head>
<body>
http://vehicle.js
http://sketch.js
</body>
</html>
class Vehicle {
constructor(x, y) {
this.pos = createVector(x, y);
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.maxSpeed = 6;
this.maxForce = 0.4;
this.r = 16;
}
evade(vehicle) {
let pursuit = this.pursue(vehicle);
pursuit.mult(-1);
return pursuit;
}
pursue(vehicle) {
let target = vehicle.pos.copy();
let prediction = vehicle.vel.copy();
prediction.mult(10);
target.add(prediction);
fill(0, 255, 0);
circle(target.x, target.y, 16);
return this.seek(target);
}
arrive(target) {
return this.seek(target, true);
}
flee(target) {
return this.seek(target).mult(-1);
}
seek(target, arrival = false) {
let force = p5.Vector.sub(target, this.pos);
let desiredSpeed = this.maxSpeed;
if (arrival) {
let slowRadius = 100;
let distance = force.mag();
if (distance < slowRadius) {
desiredSpeed = map(distance, 0, slowRadius, 0, this.maxSpeed);
}
}
force.setMag(desiredSpeed);
force.sub(this.vel);
force.limit(this.maxForce);
return force;
}
//함수 arrive가 arrive = true 값을 넘겨주어서
// target에게 가까이 갈 수록 속력을 줄입니다.
applyForce(force) {
this.acc.add(force);
}
update() {
this.vel.add(this.acc);
this.vel.limit(this.maxSpeed);
this.pos.add(this.vel);
this.acc.set(0, 0);
}
show() {
stroke(255);
strokeWeight(2);
fill(255);
push();
translate(this.pos.x, this.pos.y);
rotate(this.vel.heading());
triangle(-this.r, -this.r / 2, -this.r, this.r / 2, this.r, 0);
pop();
}
edges() {
if (this.pos.x > width + this.r) {
this.pos.x = -this.r;
} else if (this.pos.x < -this.r) {
this.pos.x = width + this.r;
}
if (this.pos.y > height + this.r) {
this.pos.y = -this.r;
} else if (this.pos.y < -this.r) {
this.pos.y = height + this.r;
}
}
}
class Target extends Vehicle {
constructor(x, y) {
super(x, y);
this.vel = p5.Vector.random2D();
this.vel.mult(5);
}
show() {
stroke(255);
strokeWeight(2);
fill("#F063A4");
push();
translate(this.pos.x, this.pos.y);
circle(0, 0, this.r * 2);
pop();
}
}
let vehicle;
function setup() {
createCanvas(400, 400);
vehicle = new Vehicle(100, 100);
}
function draw() {
background(0);
let target = createVector(mouseX, mouseY);
fill(255, 0, 0);
noStroke();
ellipse(target.x, target.y, 32);
let steering = vehicle.arrive(target);
vehicle.applyForce(steering);
vehicle.update();
vehicle.show();
}
pursuer가 target에 살짝쿵 안착하여 함께 장안의 화제 두바이 초콜릿을 나눠 먹으며 데이트를 즐길까요~^^* 두바이 초콜릿이 없어도 괜찮아요. 우리에겐 파스타치오 브라보콘도 있어요~^^*
지금까지 우리는 “나잡아봐~라~~”를 함수 seek(), flee(), pursue(), evade(), arrive()를 가지고 프로그램으로 표현해 보았습니다~~~^^* 그동안 수고 많으셨습니다~~^^* 감사합니다~~^^*
오늘도 저와 함께 코딩 공부해 주셔서 감사합니다~~^^*
내일 또 우리 만나서 코딩 공부 함께 할까요~~^^*
어떤 새로운 도전이 우리를 기다리고 있을까 궁금하시다면~^^*
호기심을 마음에 담고 기분 좋게 동네 여기저기를 배회해 보셔도 좋을 것 같아요~~^^*
앗! 저기! 우리의 새로운 도전이 길 모퉁이에서 고개를 빼꼼 내밀고 있네요!
네!!! 꿈은 이루어 집니다!!!
댓글 남기기