暂时减慢处理速度
Temporarily slow down speed in Processing
对于我们的学校作业,我们的任务是
Alter the Two Car objects program from the tutorial so that every time
the cars pass by each other, the cars slow down to 33% of their speed
and an orange vertical line is drawn between centers of the two cars
to indicate that the drivers are making eye contact.
但是,每当我尝试使用 if-else 条件语句或其他方法更改速度时,速度更改就会变成永久性的。其他时候,速度根本没有变化。我只画了橙色竖线。
这是我目前拥有的图像:
Screenshot of the program
这是它应该做的:
Video
代码如下:
// Example: Two Car objects
Car myCar1;
Car myCar2; // Two objects!
void setup() {
size(200,200);
// Parameters go inside the parentheses when the object is constructed.
myCar1 = new Car(color(255,0,0),0,100,2);
myCar2 = new Car(color(0,0,255),0,10,1);
}
void draw() {
background(255);
myCar1.drive();
myCar1.display();
myCar2.drive();
myCar2.display();
stroke(255,128,0);
line(myCar1.xpos,myCar1.ypos,myCar2.xpos,myCar2.ypos);
}
// Even though there are multiple objects, we still only need one class.
// No matter how many cookies we make, only one cookie cutter is needed.
class Car {
color c;
float xpos;
float ypos;
float xspeed;
// The Constructor is defined with arguments.
Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed;
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos,ypos,20,10);
}
void drive() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
}
}
非常感谢任何帮助。提前致谢。
[1]: https://i.stack.imgur.com/q0hAX.png
[2]: https://youtu.be/dIGr9RprfoE
我先停止任务:
Alter the Two Car objects program from the tutorial so that every time
the cars pass by each other, the cars slow down to 33% of their speed and an orange vertical line is drawn between centers of
the two cars to indicate that the drivers are making eye contact.
如何判断两辆车是否擦肩而过?
幸运的是,在这个简单的示例中,运动是一维的:在 X 轴上。
可以简单比较一下每辆车的xpos
例如
if(myCar1.xpos == myCar2.xpos){
println("slow down to 33% and draw orange line between cars");
}
您确实需要小心处理浮点数,因此数字完全匹配的几率比比较整数要小得多。
例如,您可以利用两个 x 位置之间的 dist()
function which computes the Euclidean distance between two pairs of points, however the cars don't move on x,y, just on x, so you could save a bit of computing power by simply computing abs()
olute 差异。:
float absdiff = abs(myCar1.xpos - myCar2.xpos);
您可以使用阈值来代替比较绝对距离。
假设两辆车之间的距离小于 20px,则认为它们正在相互超车。这将导致不那么刺耳的模拟,因为检查单个值 (distance == 0
) 最多会发生一瞬间 (frame),而范围 (distance < threshold
) 会持续更长的时间。
除此之外,就是画线(你已经这样做了)并将速度降低到 33%;
要注意的是,你需要一个额外的 属性 来让 Car
class 记住原来的速度,这样你就可以很容易地恢复到原来的速度,否则值会变成相对值并保持不变减少。否则,每次两辆车经过时,双方都会减速停下。
您可以使用额外的变量简单地将当前速度设置为这个初始值,当汽车不再相互通过时,或者设置为这个初始值的 33%,否则:
// Example: Two Car objects
Car myCar1;
Car myCar2; // Two objects!
// anything smaller than this distance means the cars pass each other
float passbyThreshold = 20;
void setup() {
size(600,200);
strokeWeight(3);
// Parameters go inside the parentheses when the object is constructed.
// speed up car 1 for testing to increase the odds of passing by
myCar1 = new Car(color(255,0,0),0,100,4);
myCar2 = new Car(color(0,0,255),0,10,1);
}
void draw() {
background(255);
myCar1.drive();
myCar1.display();
myCar2.drive();
myCar2.display();
// compute 1D distance between cars (abs() means it doesn't matter which one's faster)
float absdiff = abs(myCar1.xpos - myCar2.xpos);
text("distance:" + absdiff, 10, height-15);
// if the cars are close enough to be passing by
if(absdiff < passbyThreshold){
// slow down the cars to 33% of their speed
myCar1.slowDown();
myCar2.slowDown();
// draw the orange line between the centres
stroke(255,128,0);
line(myCar1.xpos,myCar1.ypos,myCar2.xpos,myCar2.ypos);
}else{
myCar1.revertSpeed();
myCar2.revertSpeed();
}
}
// Even though there are multiple objects, we still only need one class.
// No matter how many cookies we make, only one cookie cutter is needed.
class Car {
color c;
float xpos;
float ypos;
// original speed
float ospeed;
float xspeed;
// The Constructor is defined with arguments.
Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed;
// store original speed so xspeed can be restored to it
ospeed = tempXspeed;
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos,ypos,20,10);
text(xspeed,xpos - 10,ypos + 20);
}
void drive() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
}
void slowDown(){
// set speed to 33% of the original speed
xspeed = ospeed * .33;
}
void revertSpeed(){
// revert speed to the original value
xspeed = ospeed;
}
}
对于我们的学校作业,我们的任务是
Alter the Two Car objects program from the tutorial so that every time the cars pass by each other, the cars slow down to 33% of their speed and an orange vertical line is drawn between centers of the two cars to indicate that the drivers are making eye contact.
但是,每当我尝试使用 if-else 条件语句或其他方法更改速度时,速度更改就会变成永久性的。其他时候,速度根本没有变化。我只画了橙色竖线。
这是我目前拥有的图像: Screenshot of the program
这是它应该做的: Video
代码如下:
// Example: Two Car objects
Car myCar1;
Car myCar2; // Two objects!
void setup() {
size(200,200);
// Parameters go inside the parentheses when the object is constructed.
myCar1 = new Car(color(255,0,0),0,100,2);
myCar2 = new Car(color(0,0,255),0,10,1);
}
void draw() {
background(255);
myCar1.drive();
myCar1.display();
myCar2.drive();
myCar2.display();
stroke(255,128,0);
line(myCar1.xpos,myCar1.ypos,myCar2.xpos,myCar2.ypos);
}
// Even though there are multiple objects, we still only need one class.
// No matter how many cookies we make, only one cookie cutter is needed.
class Car {
color c;
float xpos;
float ypos;
float xspeed;
// The Constructor is defined with arguments.
Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed;
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos,ypos,20,10);
}
void drive() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
}
}
非常感谢任何帮助。提前致谢。
[1]: https://i.stack.imgur.com/q0hAX.png
[2]: https://youtu.be/dIGr9RprfoE
我先停止任务:
Alter the Two Car objects program from the tutorial so that every time the cars pass by each other, the cars slow down to 33% of their speed and an orange vertical line is drawn between centers of the two cars to indicate that the drivers are making eye contact.
如何判断两辆车是否擦肩而过?
幸运的是,在这个简单的示例中,运动是一维的:在 X 轴上。
可以简单比较一下每辆车的xpos
例如
if(myCar1.xpos == myCar2.xpos){
println("slow down to 33% and draw orange line between cars");
}
您确实需要小心处理浮点数,因此数字完全匹配的几率比比较整数要小得多。
例如,您可以利用两个 x 位置之间的 dist()
function which computes the Euclidean distance between two pairs of points, however the cars don't move on x,y, just on x, so you could save a bit of computing power by simply computing abs()
olute 差异。:
float absdiff = abs(myCar1.xpos - myCar2.xpos);
您可以使用阈值来代替比较绝对距离。
假设两辆车之间的距离小于 20px,则认为它们正在相互超车。这将导致不那么刺耳的模拟,因为检查单个值 (distance == 0
) 最多会发生一瞬间 (frame),而范围 (distance < threshold
) 会持续更长的时间。
除此之外,就是画线(你已经这样做了)并将速度降低到 33%;
要注意的是,你需要一个额外的 属性 来让 Car
class 记住原来的速度,这样你就可以很容易地恢复到原来的速度,否则值会变成相对值并保持不变减少。否则,每次两辆车经过时,双方都会减速停下。
您可以使用额外的变量简单地将当前速度设置为这个初始值,当汽车不再相互通过时,或者设置为这个初始值的 33%,否则:
// Example: Two Car objects
Car myCar1;
Car myCar2; // Two objects!
// anything smaller than this distance means the cars pass each other
float passbyThreshold = 20;
void setup() {
size(600,200);
strokeWeight(3);
// Parameters go inside the parentheses when the object is constructed.
// speed up car 1 for testing to increase the odds of passing by
myCar1 = new Car(color(255,0,0),0,100,4);
myCar2 = new Car(color(0,0,255),0,10,1);
}
void draw() {
background(255);
myCar1.drive();
myCar1.display();
myCar2.drive();
myCar2.display();
// compute 1D distance between cars (abs() means it doesn't matter which one's faster)
float absdiff = abs(myCar1.xpos - myCar2.xpos);
text("distance:" + absdiff, 10, height-15);
// if the cars are close enough to be passing by
if(absdiff < passbyThreshold){
// slow down the cars to 33% of their speed
myCar1.slowDown();
myCar2.slowDown();
// draw the orange line between the centres
stroke(255,128,0);
line(myCar1.xpos,myCar1.ypos,myCar2.xpos,myCar2.ypos);
}else{
myCar1.revertSpeed();
myCar2.revertSpeed();
}
}
// Even though there are multiple objects, we still only need one class.
// No matter how many cookies we make, only one cookie cutter is needed.
class Car {
color c;
float xpos;
float ypos;
// original speed
float ospeed;
float xspeed;
// The Constructor is defined with arguments.
Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed;
// store original speed so xspeed can be restored to it
ospeed = tempXspeed;
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos,ypos,20,10);
text(xspeed,xpos - 10,ypos + 20);
}
void drive() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
}
void slowDown(){
// set speed to 33% of the original speed
xspeed = ospeed * .33;
}
void revertSpeed(){
// revert speed to the original value
xspeed = ospeed;
}
}