所以我正在尝试制作一个平台游戏,我刚刚开始研究碰撞。现在碰撞非常毛刺,只有在矩形的侧面而不是顶部才有效(如果你运行代码,这更有意义)。我想知道如何让碰撞更干净,总体上不那么毛刺。
float x = 100;
float y = 100;
float vy = 0;
float vx = 0;
boolean rightCollision = false;
boolean leftCollision = false;
void setup(){
size(700, 500);
}
void draw(){
background(0);
fill(255);
rect(x, y, 50, 50);
y += vy;
vy += .3;
x += vx;
vx -= vx * 0.07;
if(y > 400){
vy = 0;
y = 400;
}
if(left){
x -= 1;
vx -= .5;
}
if(right){
x += 1;
vx += .5;
}
if(touching(x, y, 50, 50, 400, 325, 100, 500) && keyCode == RIGHT){
rightCollision = true;
}
else{
rightCollision = false;
}
if(rightCollision){
vx -= 2;
}
if(touching(x, y, 50, 50, 400, 325, 100, 500) && keyCode == LEFT){
leftCollision = true;
}
else{
leftCollision = false;
}
if(leftCollision){
vx += 2;
}
fill(167);
noStroke();
rect(0, 450, 1000, 50);
rect(400, 325, 100, 500);
}
boolean left = false;
boolean right = false;
void keyPressed(){
if(keyCode == RIGHT || key == 'd'){
right = true;
}
if(keyCode == LEFT || key == 'a'){
left = true;
}
if(keyCode == UP || key == 'w'){
if(y == 400){
vy -= 10;
}
}
}
void keyReleased(){
if(keyCode == RIGHT || key == 'd'){
right = false;
}
if(keyCode == LEFT || key == 'a'){
left = false;
}
}
boolean touching(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2){
return x1 + w1 >= x2 && x2 + w2 >= x1 && y1 + h1 >= y2 && y2 + h2 >= y1;
}
您需要在玩家移动时检测碰撞(vx!=0
,vy!=0
),而不仅仅是在按键时。
将移动设置为0并在检测到碰撞时限制玩家的位置:
int pw = 50, ph = 50;
int ox = 400, oy = 325, ow = 100, oh = 400;
topCollision = touching(x, y, pw, ph, ox, oy, ow, oh) && vy > 0.0 && y < oy;
if (topCollision){
vy = 0;
y = oy-ph;
}
rightCollision = touching(x, y, pw, ph, ox, oy, ow, oh) && vx < 0.0;
if (rightCollision){
vx = 0;
x = ox+ow;
}
leftCollision = touching(x, y, pw, ph, ox, oy, ow, oh) && vx > 0.0;
if (leftCollision) {
vx = 0;
x = ox-pw;
}
使用
boolean touching(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2){
return x1 + w1 > x2 && x2 + w2 > x1 && y1 + h1 > y2 && y2 + h2 > y1;
}