Java 矩形碰撞-不需要的重叠。(处理IDE)
所以我有一个简单的处理草图,块跟随鼠标。它有一个基本的碰撞功能,检测两个矩形之间的交点,然后将矩形a的位置设置为矩形B的位置减去矩形a的宽度(假设矩形B位于a的前面)。不幸的是,这种方法是不够的,并且矩形彼此稍微重叠。我真的希望这些矩形能像一条矩形一样完美地排列起来。有办法做到这一点吗?下面是我的可运行草图:Java 矩形碰撞-不需要的重叠。(处理IDE),java,processing,collision-detection,collision,Java,Processing,Collision Detection,Collision,所以我有一个简单的处理草图,块跟随鼠标。它有一个基本的碰撞功能,检测两个矩形之间的交点,然后将矩形a的位置设置为矩形B的位置减去矩形a的宽度(假设矩形B位于a的前面)。不幸的是,这种方法是不够的,并且矩形彼此稍微重叠。我真的希望这些矩形能像一条矩形一样完美地排列起来。有办法做到这一点吗?下面是我的可运行草图: class Block { color c = color(random(255), random(255), random(255)); float x = random(wid
class Block {
color c = color(random(255), random(255), random(255));
float x = random(width);
float speed = random(3, 6);
void run() {
float dir = mouseX - x;
dir /= abs(dir);
x += dir * speed;
fill(c);
rect(x, 300, 30, 60);
}
void collide() {
for (Block other : blocks) {
if (other != this) {
if (x + 30 > other.x && x + 30 <= other.x + 15)
x = other.x - 30;
else if (x < other.x + 30 && x > other.x + 15)
x = other.x + 30;
}
}
}
}
Block[] blocks = new Block[6];
void setup() {
size(600, 600);
for (int i = 0; i < blocks.length; i++)
blocks[i] = new Block();
}
void draw() {
background(255);
for (Block b : blocks) {
b.run();
b.collide();
}
}
void mousePressed() {
setup();
}
类块{
颜色c=颜色(随机(255)、随机(255)、随机(255));
浮动x=随机(宽度);
浮动速度=随机(3,6);
无效运行(){
float dir=mouseX-x;
dir/=abs(dir);
x+=方向*速度;
填充(c);
rect(x,300,30,60);
}
无效碰撞(){
用于(块其他:块){
如果(其他!=此){
如果(x+30>其他.x&&x+30其他.x+15)
x=其他。x+30;
}
}
}
}
块[]块=新块[6];
无效设置(){
大小(600600);
对于(int i=0;i
您好,我根据中的多对象碰撞示例更新了您的代码
想法是按以下顺序执行步骤:
Block
创建了一个新方法display()
,该方法在碰撞检测后更新位置后运行。矩形不会显示在run()
中,因为它们的位置不正确。
在设置中调用的方法overlap()
在初始化草图时负责重叠矩形
希望这有帮助
class Block {
color c = color(random(255), random(255), random(255));
float x = random(width);
float speed = random(3, 6);
void run() {
float dir = mouseX - x;
dir /= abs(dir);
x += dir * speed;
}
void display() {
fill(c);
rect(x, 300, 30, 60);
}
void collide() {
for (Block other : blocks) {
if (other != this) {
if (x + 30 > other.x && x + 30 <= other.x + 15) {
x = other.x - 30;
}
else if (x < other.x + 30 && x > other.x + 15) {
x = other.x + 30;
}
}
}
}
void overlap() {
for (Block other : blocks) {
if (other != this) {
if (x + 30 > other.x && x + 30 <= other.x + 30) {
x = other.x - 30;
}
}
}
}
}
Block[] blocks = new Block[6];
void setup() {
size(600, 600);
for (int i = 0; i < blocks.length; i++) {
blocks[i] = new Block();
}
for (Block b : blocks) {
b.overlap();
}
}
void draw() {
background(255);
for (Block b : blocks) {
b.run();
b.collide();
b.display();
}
}
void mousePressed() {
setup();
}
类块{
颜色c=颜色(随机(255)、随机(255)、随机(255));
浮动x=随机(宽度);
浮动速度=随机(3,6);
无效运行(){
float dir=mouseX-x;
dir/=abs(dir);
x+=方向*速度;
}
无效显示(){
填充(c);
rect(x,300,30,60);
}
无效碰撞(){
用于(块其他:块){
如果(其他!=此){
如果(x+30>其他.x&&x+30其他.x+15){
x=其他。x+30;
}
}
}
}
无效重叠(){
用于(块其他:块){
如果(其他!=此){
如果(x+30>other.x&&x+30您正在循环通过您的块,并且对于每个块,您都在再次循环通过您的块,一次检查另一个块的碰撞。但是,您永远不会处理一个块与其他两个块重叠的情况,这是当您看到重叠时实际发生的情况。谢谢。您对如何可以解决这个问题吗?太好了!谢谢!所以这只是一个简单的排序问题。小东西太好了!我很高兴你发现答案很有用!要将我的答案标记为已接受,请单击答案旁边的复选标记,将其从灰显切换为已填写(请参阅)