有没有办法让圆圈从可移动物体的顶部反弹?
Is there a way for me to make the circle bounce off the top of the movable object?
基本上我只是想要一些关于如何使圆圈从可移动物体上反弹的指导。我遇到了麻烦,已经尝试了三个小时,因此转向论坛寻求帮助。我尝试了多个 'if' 语句,但显然我没有正确理解 none 正在工作。谢谢:)
我已经尝试了 3 个小时来使用不同的 if 语句来解决这个问题。
float x;
float easing = 1;
float circle_x = 1;
float circle_y = 30;
float rad = 12.5;
float gravity = 0.98;
float move_x = 5;
float move_y = 5;
void setup() {
size(640, 480);
frameRate(60);
}
void draw() {
background(#87CEEB);
fill(#7cfc00);
rect(0, 430, 640, 80);
float targetX = mouseX;
float dx = targetX - x;
x += dx * easing;
fill(#000000);
rect(x, 400, 30, 30);
rect(x-20, 390, 70, 10);
rect(x, 430, 5, 20);
rect(x+25, 430, 5, 20);
ellipse(circle_x, circle_y, 25, 25);
circle_x = circle_x + move_x;
circle_y = circle_y + move_y;
if (circle_x > width) {
circle_x = width;
move_x = -move_x;
}
if (circle_y > height) {
circle_y = height;
move_y = -move_y;
}
if (circle_x < 0) {
circle_x = 0;
move_x = -move_x;
}
if (circle_y < 0) {
circle_y = 0;
move_y= -move_y;
}
}
将任何变量插入 if 语句并只接收返回:球被我的鼠标光标(而不是对象)反弹,出现故障的圆圈和断断续续的图像。
需要检查小球的x坐标是否在物体的范围内(objW
是物体的宽度):
circle_x > x && circle_x < x + objW
如果小球的y坐标已经达到物体的高度(objH
是物体的高度,circleR
是球的半径):
circle_y > objH - circleR
此外,重要的是先进行命中测试,然后再进行物体弹跳地面的测试。一种好的风格是在 else if
语句中执行此操作:
int objX1 = -20;
int objX2 = 70;
int objH = 390;
int circleR = 25/2;
if (circle_x > x + objX1 && circle_x < x + objX2 && circle_y > objH - circleR ) {
circle_y = objH-circleR;
move_y = -move_y;
}
else if (circle_y > height) {
circle_y = height;
move_y = -move_y;
}
else if (circle_y < 0) {
circle_y = 0;
move_y= -move_y;
}
此外我建议先计算球的位置,然后在当前位置绘制球:
float x;
float easing = 1;
float circle_x = 1;
float circle_y = 30;
float rad = 12.5;
float gravity = 0.98;
float move_x = 5;
float move_y = 5;
void setup() {
size(640, 480);
frameRate(60);
}
void draw() {
background(#87CEEB);
fill(#7cfc00);
rect(0, 430, 640, 80);
float targetX = mouseX;
float dx = targetX - x;
x += dx * easing;
circle_x = circle_x + move_x;
circle_y = circle_y + move_y;
if (circle_x > width) {
circle_x = width;
move_x = -move_x;
}
else if (circle_x < 0) {
circle_x = 0;
move_x = -move_x;
}
int objW = 70;
int objH = 390;
int circleR = 25/2;
if (circle_x > x && circle_x < x + objW && circle_y > objH-circleR ) {
circle_y = objH-circleR;
move_y = -move_y;
}
else if (circle_y > height) {
circle_y = height;
move_y = -move_y;
}
else if (circle_y < 0) {
circle_y = 0;
move_y= -move_y;
}
fill(#000000);
rect(x, 400, 30, 30);
rect(x-20, 390, 70, 10);
rect(x, 430, 5, 20);
rect(x+25, 430, 5, 20);
ellipse(circle_x, circle_y, 25, 25);
}
基本上我只是想要一些关于如何使圆圈从可移动物体上反弹的指导。我遇到了麻烦,已经尝试了三个小时,因此转向论坛寻求帮助。我尝试了多个 'if' 语句,但显然我没有正确理解 none 正在工作。谢谢:)
我已经尝试了 3 个小时来使用不同的 if 语句来解决这个问题。
float x;
float easing = 1;
float circle_x = 1;
float circle_y = 30;
float rad = 12.5;
float gravity = 0.98;
float move_x = 5;
float move_y = 5;
void setup() {
size(640, 480);
frameRate(60);
}
void draw() {
background(#87CEEB);
fill(#7cfc00);
rect(0, 430, 640, 80);
float targetX = mouseX;
float dx = targetX - x;
x += dx * easing;
fill(#000000);
rect(x, 400, 30, 30);
rect(x-20, 390, 70, 10);
rect(x, 430, 5, 20);
rect(x+25, 430, 5, 20);
ellipse(circle_x, circle_y, 25, 25);
circle_x = circle_x + move_x;
circle_y = circle_y + move_y;
if (circle_x > width) {
circle_x = width;
move_x = -move_x;
}
if (circle_y > height) {
circle_y = height;
move_y = -move_y;
}
if (circle_x < 0) {
circle_x = 0;
move_x = -move_x;
}
if (circle_y < 0) {
circle_y = 0;
move_y= -move_y;
}
}
将任何变量插入 if 语句并只接收返回:球被我的鼠标光标(而不是对象)反弹,出现故障的圆圈和断断续续的图像。
需要检查小球的x坐标是否在物体的范围内(objW
是物体的宽度):
circle_x > x && circle_x < x + objW
如果小球的y坐标已经达到物体的高度(objH
是物体的高度,circleR
是球的半径):
circle_y > objH - circleR
此外,重要的是先进行命中测试,然后再进行物体弹跳地面的测试。一种好的风格是在 else if
语句中执行此操作:
int objX1 = -20;
int objX2 = 70;
int objH = 390;
int circleR = 25/2;
if (circle_x > x + objX1 && circle_x < x + objX2 && circle_y > objH - circleR ) {
circle_y = objH-circleR;
move_y = -move_y;
}
else if (circle_y > height) {
circle_y = height;
move_y = -move_y;
}
else if (circle_y < 0) {
circle_y = 0;
move_y= -move_y;
}
此外我建议先计算球的位置,然后在当前位置绘制球:
float x;
float easing = 1;
float circle_x = 1;
float circle_y = 30;
float rad = 12.5;
float gravity = 0.98;
float move_x = 5;
float move_y = 5;
void setup() {
size(640, 480);
frameRate(60);
}
void draw() {
background(#87CEEB);
fill(#7cfc00);
rect(0, 430, 640, 80);
float targetX = mouseX;
float dx = targetX - x;
x += dx * easing;
circle_x = circle_x + move_x;
circle_y = circle_y + move_y;
if (circle_x > width) {
circle_x = width;
move_x = -move_x;
}
else if (circle_x < 0) {
circle_x = 0;
move_x = -move_x;
}
int objW = 70;
int objH = 390;
int circleR = 25/2;
if (circle_x > x && circle_x < x + objW && circle_y > objH-circleR ) {
circle_y = objH-circleR;
move_y = -move_y;
}
else if (circle_y > height) {
circle_y = height;
move_y = -move_y;
}
else if (circle_y < 0) {
circle_y = 0;
move_y= -move_y;
}
fill(#000000);
rect(x, 400, 30, 30);
rect(x-20, 390, 70, 10);
rect(x, 430, 5, 20);
rect(x+25, 430, 5, 20);
ellipse(circle_x, circle_y, 25, 25);
}