关于如何使用此处的代码创建 "Object collision detection" 有什么想法吗?
Any ideas on how i could create a "Object collision detection" with the code i have here?
我正在尝试制作一款游戏,如果主角 (gubbe
) 与敌人 (fiende
) 发生碰撞,角色将占据 damage(Healthdisplay/HW)
。我尝试通过多种方式进行碰撞检测,主要是 if(x.intersects(y)){}
。我卡住了,不知道如何解决。我把主要代码放在这里 VVV。如果需要更多代码,只需说出并输入所需的代码即可。
HW 是应该造成伤害的生命值池。
无效显示是主要 character/rectangle 已创建。
敌人在另一个页面正在处理中
class gubbe {
float x = 0;
float y = 0;
int HW = 20;
//rect sidor
int h1 = 100;
int w1 = 100;
//----
//VV movement VV
void move() {
if (keyPressed) {
if (key == 'w' || key == 'W') {
//terms, move forward, Y-led.
y -= 100;
}
}
if (keyPressed) {
if (key == 'a' || key == 'A') {
//terms, move right, X-led.
x -= 100;
}
}
if (keyPressed) {
if (key == 's' || key == 'S') {
//terms, move backwards, Y-led.
y += 100;
}
}
if (keyPressed) {
if (key == 'd' || key == 'D') {
//terms, move left, X-led.
x += 100;
}
}
}
// VV visual VV
void display() {
//Grabb
fill(127);
stroke(0);
rect(x, y, w1, h1);
}
void display2() {
fill(127);
stroke(0);
rect(x, y, w1, h1);
}
// VV Edgechechning VV
void checkEdges() {
if (x + 100 > width) {
x -= 50;
} else if (x < 0) {
x += 50;
} else if (y > height) {
y -= 50;
} else if (y < 0) {
y += 50;
} else {
}
}
// VV Fighting VV
void Fight() {
if (keyPressed) {
if (key == 'm'|| key == 'M') {
//villkor, flytta höger, X-led.
fill(255, 0, 0);
stroke(255, 0, 0);
rect(x, y-100, 100, 100);
}
}
}
// VV health bars VV
void BarsDisplay() {
fill(204, 204, 204);
stroke(204, 204, 204);
rect(x, y-30, 100, 9);
}
void HealthDisplay() {
fill(0, 0, 255);
stroke(255, 0, 0);
rect(x, y-31, HW, 3);
!!! here there should be collision detection!!!
}
void XpDisplay() {
fill(255, 255, 0);
stroke(255, 255, 0);
rect(x, y-22, 2, 1);
//if(om dödar fiende){
// width+=x
//}
//else{
//}
}
}
你的物体看起来是长方形的,所以如果沿 x 轴和 y 轴的两个中心之间的距离小于每个物体在那个方向上的一半大小,就会发生碰撞轴相加。
拿一张纸画下来,你就会看到了!
class gubbe
中检查与 fiende
类型对象的交集的方法可能如下所示(我假设 fiende
是一个矩形,具有属性 x
、y
、w1
、h1
:
class gubbe {
// [...]
Boolean intersects(fiende f){
// if intersecting then return 'true', else return 'false'
return this.x < f.x+f.w1 && f.x < this.x+this.w1 &&
this.y < f.y+f.h1 && f.y < this.y+this.h1;
}
// [...]
}
调用方法如下:
gubbe g = new gubbe();
gubbe f = new fiende();
if (g.intersects(f)) {
// do somthing
// [...]
}
解释:
要检查矩形的碰撞,您必须检查矩形在两个维度上是否重叠。
对于从 x1
到 x1+w1
的范围和从 x2
到 x2+w2
的第二个范围,有以下可能的情况:
不重叠:
x1 x1+w1
+----+
+----+
x2 x2+w2
x1 x1+w1
+----+
+----+
x2 x2+w2
重叠
x1 x1+w1
+--------------+
+----+
x2 x2+w2
x1 x1+w1
+----+
+---------------+
x2 x2+w2
x1 x1+w1
+---------+
+----------+
x2 x2+w2
x1 x1+w1
+----------+
+----------+
x2 x2+w2
这意味着,如果
,则范围重叠
x1 < x2+w2 AND x2 < x1+w1
对于分别由原点和大小 (x1
、y1
、w1
h1
) 定义的 2 个矩形 (x2
、y2
, w2
h2
) 条件为:
x1 < x2+w2 AND x2 < x1+w1 AND y1 < y2+h2 AND y2 < y1+h1
这导致以下功能:
Boolean intersect(float x1, float y1, float w1, float h1,
float x2, float y2, float w2, float h2){
// if intersecting then return 'true', else return 'false'
return x1 < x2+w2 && x2 < x1+w1 &&
y1 < y2+h2 && y2 < y1+h1;
}
我正在尝试制作一款游戏,如果主角 (gubbe
) 与敌人 (fiende
) 发生碰撞,角色将占据 damage(Healthdisplay/HW)
。我尝试通过多种方式进行碰撞检测,主要是 if(x.intersects(y)){}
。我卡住了,不知道如何解决。我把主要代码放在这里 VVV。如果需要更多代码,只需说出并输入所需的代码即可。
HW 是应该造成伤害的生命值池。
无效显示是主要 character/rectangle 已创建。
敌人在另一个页面正在处理中
class gubbe {
float x = 0;
float y = 0;
int HW = 20;
//rect sidor
int h1 = 100;
int w1 = 100;
//----
//VV movement VV
void move() {
if (keyPressed) {
if (key == 'w' || key == 'W') {
//terms, move forward, Y-led.
y -= 100;
}
}
if (keyPressed) {
if (key == 'a' || key == 'A') {
//terms, move right, X-led.
x -= 100;
}
}
if (keyPressed) {
if (key == 's' || key == 'S') {
//terms, move backwards, Y-led.
y += 100;
}
}
if (keyPressed) {
if (key == 'd' || key == 'D') {
//terms, move left, X-led.
x += 100;
}
}
}
// VV visual VV
void display() {
//Grabb
fill(127);
stroke(0);
rect(x, y, w1, h1);
}
void display2() {
fill(127);
stroke(0);
rect(x, y, w1, h1);
}
// VV Edgechechning VV
void checkEdges() {
if (x + 100 > width) {
x -= 50;
} else if (x < 0) {
x += 50;
} else if (y > height) {
y -= 50;
} else if (y < 0) {
y += 50;
} else {
}
}
// VV Fighting VV
void Fight() {
if (keyPressed) {
if (key == 'm'|| key == 'M') {
//villkor, flytta höger, X-led.
fill(255, 0, 0);
stroke(255, 0, 0);
rect(x, y-100, 100, 100);
}
}
}
// VV health bars VV
void BarsDisplay() {
fill(204, 204, 204);
stroke(204, 204, 204);
rect(x, y-30, 100, 9);
}
void HealthDisplay() {
fill(0, 0, 255);
stroke(255, 0, 0);
rect(x, y-31, HW, 3);
!!! here there should be collision detection!!!
}
void XpDisplay() {
fill(255, 255, 0);
stroke(255, 255, 0);
rect(x, y-22, 2, 1);
//if(om dödar fiende){
// width+=x
//}
//else{
//}
}
}
你的物体看起来是长方形的,所以如果沿 x 轴和 y 轴的两个中心之间的距离小于每个物体在那个方向上的一半大小,就会发生碰撞轴相加。
拿一张纸画下来,你就会看到了!
class gubbe
中检查与 fiende
类型对象的交集的方法可能如下所示(我假设 fiende
是一个矩形,具有属性 x
、y
、w1
、h1
:
class gubbe {
// [...]
Boolean intersects(fiende f){
// if intersecting then return 'true', else return 'false'
return this.x < f.x+f.w1 && f.x < this.x+this.w1 &&
this.y < f.y+f.h1 && f.y < this.y+this.h1;
}
// [...]
}
调用方法如下:
gubbe g = new gubbe();
gubbe f = new fiende();
if (g.intersects(f)) {
// do somthing
// [...]
}
解释:
要检查矩形的碰撞,您必须检查矩形在两个维度上是否重叠。
对于从 x1
到 x1+w1
的范围和从 x2
到 x2+w2
的第二个范围,有以下可能的情况:
不重叠:
x1 x1+w1
+----+
+----+
x2 x2+w2
x1 x1+w1
+----+
+----+
x2 x2+w2
重叠
x1 x1+w1
+--------------+
+----+
x2 x2+w2
x1 x1+w1
+----+
+---------------+
x2 x2+w2
x1 x1+w1
+---------+
+----------+
x2 x2+w2
x1 x1+w1
+----------+
+----------+
x2 x2+w2
这意味着,如果
,则范围重叠x1 < x2+w2 AND x2 < x1+w1
对于分别由原点和大小 (x1
、y1
、w1
h1
) 定义的 2 个矩形 (x2
、y2
, w2
h2
) 条件为:
x1 < x2+w2 AND x2 < x1+w1 AND y1 < y2+h2 AND y2 < y1+h1
这导致以下功能:
Boolean intersect(float x1, float y1, float w1, float h1,
float x2, float y2, float w2, float h2){
// if intersecting then return 'true', else return 'false'
return x1 < x2+w2 && x2 < x1+w1 &&
y1 < y2+h2 && y2 < y1+h1;
}