我有一個程序處理一個彈跳球和矩形。我可以得到矩形兩側的碰撞,但我不知道如何找到角落。這是我到目前爲止有:圓和矩形碰撞
int radius = 20;
float circleX, circleY; //positions
float vx = 3, vy = 3; //velocities float boxW, boxH; //bat dimensions
void setup(){
size(400,400);
ellipseMode(RADIUS);
rectMode(RADIUS);
circleX = width/4;
circleY = height/4;
boxW = 50;
boxH = 20;
}
void draw(){
background(0);
circleX += vx;
circleY += vy;
//bouncing off sides
if (circleX + radius > width || circleX < radius){ vx *= -1; } //left and right
if (circleY + radius > height || circleY < radius){ vy *= -1; } //top and bottom
if (circleY + radius > height){
circleY = (height-radius)-(circleY-(height-radius)); } //bottom correction
//bouncing off bat
if (circleY + radius > mouseY - boxH && circleX > mouseX - boxW && circleX < mouseX + boxW){
vy *= -1; //top
}
if (circleX - radius < mouseX + boxW && circleY > mouseY - boxH && circleY < mouseY + boxH){
vx *= -1; //right
}
if (circleY - radius > mouseY + boxH && circleX > mouseX - boxW && circleX < mouseX + boxW){
vy *= -1; //bottom
}
if (circleX + radius < mouseX - boxW && circleY > mouseY - boxH && circleY < mouseY + boxH){
vx *= -1; //left
}
if ([CORNER DETECTION???]){
vx *= -1;
vy *= -1;
}
ellipse(circleX,circleY,radius,radius);
rect(mouseX,mouseY,boxW,boxH);
}
我不知道要放什麼東西在if語句來檢測碰撞角落。
您需要重新考慮對幾何體的「印象」。首先:一個圈子在一個角落實際上是不可能的。第二:你沒有碰到一個角落,你同時擊中雙方。你需要改變你的邏輯思想,你可能會同時觸及多個方面。粗略瀏覽一下你的前兩個陳述似乎已經涵蓋了這些內容。 –
你有沒有得到這個清理? –