由于在尝试为3个移动对象进行碰撞检测编码时遇到麻烦,我知道这是我应该使用的矩形碰撞,但是很难获得帮助,我不确定如何使用abs方法或其他任何方法。
这是主要代码
Bouncy walter, wanda, wonker;
void setup()
{
size(500,500);
walter = new Bouncy(10,100,1,-1);
wanda = new Bouncy(10,150,-1,1);
wonker = new Bouncy(50,50,1,1);
}
void draw()
{
background(255);
walter.update();
wanda.update();
wonker.update();
}
this is the class of the code
class Bouncy
{
int x;
int y;
int dx;
int dy;
PImage nw, ne, sw, se;
Bouncy(int x, int y, int dx, int dy)
{
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
nw = loadImage("nw.png");
ne = loadImage("ne.png");
sw = loadImage("sw.png");
se = loadImage("se.png");
}
void update()
{
render();
move();
}
void render()
{
if (dx == -1 && dy == -1)
image(nw,x,y);
else if (dx == 1 && dy == -1)
image(ne,x,y);
else if (dx == -1 && dy == 1)
image(sw,x,y);
else if (dx == 1 && dy == 1)
image(se,x,y);
}
void checkCollisions()
{
int edge = 25; // half width of one of the PNG files
if (y<=(edge-edge)) // hit top border
dy=1; // switch to moving downwards
if (y>=height-(edge*2)) // hit bottom border
dy=-1; // switch to moving upwards
if (x<=edge-edge) // hit left border
dx=1; // switch to moving right
if (x>=width-(edge*2)) // hit right border
dx=-1;
}
boolean crash(Bouncy walter)
{
return (abs(this.x-walter.x) < 10 && abs(this.y-walter.y) < 10);
}
boolean collision(Bouncy wanda){
return (abs(this.x-wanda.x) < 10 && abs(this.y-wanda.y) < 10);
}
boolean collide(Bouncy wonker){
return (abs(this.x-wonker.x) < 10 && abs(this.y-wonker.y) < 10);
}
void move()
{
checkCollisions();
x += dx;
y += dy;
}
}
我在网上看到了使用布尔崩溃等方法的信息,但对我不起作用。