나는 튀는 공과 직사각형을 처리하는 프로그램이 있습니다. 직사각형의 측면에 대한 충돌을 맞출 수는 있지만 모서리를 얻는 방법을 모릅니다.원과 사각형 충돌
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 문에 넣어 해야할지 모르겠어 : 이것은 내가 지금까지있는 것입니다.
형상에 대한 "인상"을 재고해야합니다. 첫째, 서클이 구석에 닿는 것은 물리적으로 불가능합니다. 두 번째 : 당신은 구석에 충돌하지 않는다, 당신은 두면을 동시에 때렸다. 동시에 여러 측면에 부딪 힐 수 있다는 생각으로 논리를 다시 작성해야합니다. 처음 두 문장에 그 단순한 내용이 담긴 것 같습니다. –
당신은 이것을 소트 해 봤어? –