gpt4 book ai didi

java - 当我更改窗口大小时,我的动画球不会在中间交叉

转载 作者:行者123 更新时间:2023-11-30 05:30:08 28 4
gpt4 key购买 nike

有两条车道,每条车道都包含一个球...当动画开始时,当默认窗口大小时,它会在中间交叉。当我更改窗口大小时,它不会在中间交叉。过去几周我一直在努力解决这个问题。

float x = 0.4*width/8; 
float y = 0.4*height/8;

void setup(){
size(600,600);
background(#C8F5F2);
frameRate(10);
}

void draw(){

fill(255);
noStroke();
rectMode(CENTER);
rect(width/2, 0, width/8, height*2); //vertical lane
rect(0, height/2, 2*width, height/8); //horizontal lane

fill(255,0,0,100);
ellipse( width/2, x, 0.8*width/8, 0.8*width/8); //vertical ellipse

fill(0,255,0,100);
ellipse( y, height/2, 0.8*height/8, 0.8*height/8); //horizontal
//ellipse

if(x < height - 0.4*width/8){
x = x + width/45;
}

if(y < width - 0.4*height/8){
y = y + height/20;
}
}

我希望我的答案是“在任何窗口大小的中间穿过球”

最佳答案

如果球要从中间穿过,那么它们必须同时通过不同的路径。

球的半径为:

float radius1 = 0.4*height/8; 
float radius2 = 0.4*width/8;

第一个球的轨迹在x轴上,从x=radius1x=width-radius1。第二个球的轨迹在 y 轴上,从 y=radius2y=height-radius2

因此“球的下一个位置可以通过以下方式计算:

x += (width - 2.0*radius1) / steps;
 y += (height - 2.0*radius2) / steps;

其中steps是每个球从开始到结束应该执行的步数。

进一步注意,x 轴是从左到右,y 轴是从上到下。请参阅示例:

float x, y; 
float steps = 20.0;

void setup(){
size(800,300);
background(#C8F5F2);
frameRate(10);

x = 0.4*height/8;
y = 0.4*width/8;
}

void draw(){

float radius1 = 0.4*height/8;
float radius2 = 0.4*width/8;

fill(255);
noStroke();
rectMode(CENTER);
rect(width/2, 0, width/8, height*2); //vertical lane
rect(0, height/2, 2*width, height/8); //horizontal lane

fill(255,0,0,100);
ellipse(x, height/2, radius1*2.0, radius1*2.0); //vertical ellipse

fill(0,255,0,100);
ellipse(width/2, y, radius2*2.0, radius2*2.0); //horizontal

if(x < width - radius1){
x += (width - 2.0*radius1) / steps;
}
if(y < height - radius2){
y += (height - 2.0*radius2) / steps;
}
}
<小时/>

.i need another thing to know and that is how to declare specific key to speed up and speed down the balls. like "Pressing the UP key results in doubling of speed and pressing the DOWN key results in halving of speed.

当按下一个键时,keyPressed()被执行。使用keyCode评估是否按下向上或向下 DOWN 并更改步骤:

void keyPressed() {
if (keyCode == DOWN)
steps /= 2.0;
else if (keyCode == UP)
steps *= 2.0;
}

关于java - 当我更改窗口大小时,我的动画球不会在中间交叉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57734795/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com