Creating a hit box?

I am having trouble with hit boxes, the enemy only stops when it gets to s specific point not when it touches the hole enemy and I can figure out how

float X,Y,Xc,Yc,Xm,Ym,Xl,Xr,Yt,Yb;
float H1,H2,H3,H1x,H1y,Ev;
float Hbar=3;
float Pdamx=100width/1000, pdamy=100height/1000;
color H1G, H1B;
int EM =1;
int EMi =1;
enemy em1;
enemy em2;
void setup(){
em1 = new enemy(color(255,0,0),0,300,2);
em2 = new enemy(color(255,0,0),0,700,2);
H1G= color(255,0,0,255);
H1B = color(0,0,0,0);
Ev=255;
size(1000,1000);
X=2;Y=2;Yc=500height/1000;Xc=500width/1000;Xl=300width/1000;Xr=700width/1000;Yt=700height/1000;Yb=300height/1000;

}

void draw(){

background(150);
rectMode(CENTER);
if(X==2){Xm=Xc;}
if(X==1){Xm=Xl;}
if(X==3){Xm=Xr;}
if(Y==2){Ym=Yc;}
if(Y==1){Ym=Yb;}
if(Y==3){Ym=Yt;}
//health
print(Hbar);
if(Hbar==3){
fill(H1G);
rect(25,25,50,50);
}
if(Hbar==2){
fill(0,255,0);
rect(25,25,50,50);
}
if(Hbar==1){
fill(255,150,255);
rect(25,25,50,50);
}
if(Hbar==0){
fill(0);
rect(25,25,50,50);
}

//board
fill(255);
rect(500width/1000,500height/1000,200width/1000,200height/1000);
rect(700width/1000,500height/1000,200width/1000,200height/1000);
rect(500width/1000,700height/1000,200width/1000,200height/1000);
rect(700width/1000,700height/1000,200width/1000,200height/1000);
rect(500width/1000,300height/1000,200width/1000,200height/1000);
rect(300width/1000,500height/1000,200width/1000,200height/1000);
rect(300width/1000,300height/1000,200width/1000,200height/1000);
rect(700width/1000,300height/1000,200width/1000,200height/1000);
rect(300width/1000,700height/1000,200width/1000,200height/1000);
//player
fill(0);
rect(Xm,Ym,100width/1000,100height/1000);
//enamy
em1.drive();
em1.display();
em1.hit();
em2.drive();
em2.display();
em2.hit();

}
//movement
void keyReleased(){
if (key == ‘a’) {
X=X-1;

}
if (key == ‘d’){
X=X+1;
}
if (key == ‘w’){
Y=Y-1;
}
if (key == ‘s’) {
Y=Y+1;
}
}
class enemy {
color c;
float xpos;
float ypos;
float xspeed;
int boxX=200;
int boxY=100;
// The Constructor is defined with arguments.
enemy(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed;
}

void display() {
stroke(0);
fill©;
rectMode(CENTER);
rect(xpos,ypos,boxX,boxY);
}

void drive() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
}
void hit(){
if(xpos==Xm&&ypos==Ym){
Hbar=Hbar-1;
xpos=0;
xspeed=0;
}
}
}

Hello, and welcome to the forum :slight_smile:

First things first, whenever you post code, please, format it; it’s harder to read if it is not formatted properly. You can type in your formatted code by clicking the symbol </>, or just by surrounding it with back ticks `.

Secondly, your code does not work, it has many syntax errors like 100height/1000 instead of 100 * height / 1000, (which is actually height / 10).


That said, your problem is here

As it is, a collision occurs only when the center of the enemy and the player’s touch. You need to check for a rectangle-rectangle collision.

2 Likes