# Creating a hit box?

**URL:** <https://discourse.processing.org/t/creating-a-hit-box/17469>\
**Category:** Coding Questions\
**Created:** [January 30, 2020, 10:54pm UTC](https://discourse.processing.org/t/creating-a-hit-box/17469 "2020-01-30T22:54:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lupos](https://avatars.discourse-cdn.com/v4/letter/l/d9b06d/32.png) [@lupos](https://discourse.processing.org/u/lupos)\
**Post date:** [January 30, 2020, 10:54pm UTC](https://discourse.processing.org/t/creating-a-hit-box/17469/1 "2020-01-30T22:54:12Z")

</div>

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=100_width/1000, pdamy=100_height/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=500_height/1000;Xc=500_width/1000;Xl=300_width/1000;Xr=700_width/1000;Yt=700_height/1000;Yb=300_height/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(500_width/1000,500_height/1000,200_width/1000,200_height/1000);  
rect(700_width/1000,500_height/1000,200_width/1000,200_height/1000);  
rect(500_width/1000,700_height/1000,200_width/1000,200_height/1000);  
rect(700_width/1000,700_height/1000,200_width/1000,200_height/1000);  
rect(500_width/1000,300_height/1000,200_width/1000,200_height/1000);  
rect(300_width/1000,500_height/1000,200_width/1000,200_height/1000);  
rect(300_width/1000,300_height/1000,200_width/1000,200_height/1000);  
rect(700_width/1000,300_height/1000,200_width/1000,200_height/1000);  
rect(300_width/1000,700_height/1000,200_width/1000,200_height/1000);  
//player  
fill(0);  
rect(Xm,Ym,100_width/1000,100_height/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;  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![trikaphundo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/trikaphundo/32/7913_2.png) [@trikaphundo](https://discourse.processing.org/u/trikaphundo)\
**Post date:** [January 31, 2020, 1:41pm UTC](https://discourse.processing.org/t/creating-a-hit-box/17469/2 "2020-01-31T13:41:44Z")

</div>

Hello, and welcome to the forum 🙂

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

> [@lupos](#):
>
> ```auto
> void hit(){
> if(xpos == Xm && ypos == Ym){ // they collide when centers touch
> Hbar=Hbar-1;
> xpos=0;
> xspeed=0; // makes the enemy not move after a hit
> }
> 
> ```

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.
