有没有办法在 java 中制作圆形碰撞箱?
Is there a way to make circular hitboxes in java?
我正在尝试制作一个简单的游戏,您只需点击一些圆圈,我想知道是否可以制作圆形的碰撞框而不是矩形的。
这段代码是在processing 3.5.1 btw中写的...
我放出了一些代码,基本上是我设置主菜单的代码。
//Random ellipse position
int posx = (int)random(100, 1820);
int posy = (int)random(100, 980);
//Score count
int score = 0;
//Circlesize/Difficulty
int circlesize = 100;
//Hitbox
float hitsize = 50;
void setup(){
size(1920,1080);
background(255);
}
void draw(){
//What do to if the circle is pressed
if(mousePressed && mouseX >= (posx - hitsize) && mouseX <= (posx + hitsize) && mouseY >= (posy - hitsize) && mouseY <= (posy + hitsize)){
//Randomize next location for ellipse
posx = (int)random(100,1820);
posy = (int)random(100, 980);
//Make a new ellipse
background(255);
fill(255, 0, 0);
ellipse(posx, posy, circlesize, circlesize);
//Add a point to score
score ++;
}
}
简短回答:是的。你只需要检查圆心之间的距离。如果该距离小于两个圆的半径之和,则圆相交。
无耻的自我推销:here是Processing中碰撞检测的教程。
根据 Kevin 所说,
您可以在 if 中使用代码 dist(x1, y1, x2, y2) < 50
来检查前两个参数 (x1, y1) 是否与x2 和 y2 的 50px。
一个例子:
int buttonX = 200;// the x position of the circle
int buttonY = 200;// the y position of the circle
ellipse(buttonX, buttonY,100,100);// the button
// if mouseX and mouseY are inside the radius of the 100px diameter button then it is active
if(dist(mouseX, mouseY, buttonX, buttonY) < 50) {
// the mouse is inside of the circle
}
希望对你有所帮助,有什么问题欢迎随时提问
我正在尝试制作一个简单的游戏,您只需点击一些圆圈,我想知道是否可以制作圆形的碰撞框而不是矩形的。
这段代码是在processing 3.5.1 btw中写的... 我放出了一些代码,基本上是我设置主菜单的代码。
//Random ellipse position
int posx = (int)random(100, 1820);
int posy = (int)random(100, 980);
//Score count
int score = 0;
//Circlesize/Difficulty
int circlesize = 100;
//Hitbox
float hitsize = 50;
void setup(){
size(1920,1080);
background(255);
}
void draw(){
//What do to if the circle is pressed
if(mousePressed && mouseX >= (posx - hitsize) && mouseX <= (posx + hitsize) && mouseY >= (posy - hitsize) && mouseY <= (posy + hitsize)){
//Randomize next location for ellipse
posx = (int)random(100,1820);
posy = (int)random(100, 980);
//Make a new ellipse
background(255);
fill(255, 0, 0);
ellipse(posx, posy, circlesize, circlesize);
//Add a point to score
score ++;
}
}
简短回答:是的。你只需要检查圆心之间的距离。如果该距离小于两个圆的半径之和,则圆相交。
无耻的自我推销:here是Processing中碰撞检测的教程。
根据 Kevin 所说,
您可以在 if 中使用代码 dist(x1, y1, x2, y2) < 50
来检查前两个参数 (x1, y1) 是否与x2 和 y2 的 50px。
一个例子:
int buttonX = 200;// the x position of the circle
int buttonY = 200;// the y position of the circle
ellipse(buttonX, buttonY,100,100);// the button
// if mouseX and mouseY are inside the radius of the 100px diameter button then it is active
if(dist(mouseX, mouseY, buttonX, buttonY) < 50) {
// the mouse is inside of the circle
}
希望对你有所帮助,有什么问题欢迎随时提问