# Hitbox detection?

**URL:** <https://discourse.processing.org/t/hitbox-detection/41871>\
**Category:** Beginners\
**Created:** [April 29, 2023, 11:50pm UTC](https://discourse.processing.org/t/hitbox-detection/41871 "2023-04-29T23:50:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [April 29, 2023, 11:50pm UTC](https://discourse.processing.org/t/hitbox-detection/41871/1 "2023-04-29T23:50:56Z")

</div>

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/2/b2596871f528d2961f8bceda5f1ff81c3a4db08b.png)

the terrain is a 2d array, and thre player can move just a tiny but so how do i do it? i dont wanna use a pgraphics with get()

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [April 30, 2023, 12:21am UTC](https://discourse.processing.org/t/hitbox-detection/41871/2 "2023-04-30T00:21:11Z")

</div>

a collision detection algorithm might be appropriate. take a look at this website for examples and explanations for code

> **[Collision Detection](https://www.jeffreythompson.org/collision-detection/)**
>
> An online book about collision detection using Processing.

interactive examples on this website

> **[Processing Collision Detection](https://happycoding.io/tutorials/processing/collision-detection)**
>
> Learn how to trigger an action when objects overlap in Processing!

and a point shape collision detection on this website

https://openprocessing.org/sketch/783497/embed/

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 30, 2023, 10:02am UTC](https://discourse.processing.org/t/hitbox-detection/41871/3 "2023-04-30T10:02:33Z")

</div>

Do you want to detect if the player is inside this area?

Then you can use the above mentioned _collision detection_.

When it’s a point _p_ inside a rectangle _rect_ you can just say:

```auto
if (pX > rectX &&
   pX < rectX + rectwidth &&
   pY > rectY && 
   pY < rectY + rectHeight) {
       ...
}

```

(adjust to your variable names)

Chrisir

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 30, 2023, 1:15pm UTC](https://discourse.processing.org/t/hitbox-detection/41871/4 "2023-04-30T13:15:32Z")

</div>

Slight modification to @Chrisirs code

```auto
if (pX >= rectX && pX < rectX + rectwidth 
            && pY >= rectY && pY < rectY + rectHeight) {
  // …
}

```

It is probable that the tile’s position and size are stored as integers but the players position `[pX, pY]` are stored in floats. If that is the case then there are values of `pX` and `pY` which will not ‘hit’ any tile. Genrally it is better to use a half-open range to avoid this problem.

---

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [May 1, 2023, 1:38am UTC](https://discourse.processing.org/t/hitbox-detection/41871/5 "2023-05-01T01:38:44Z")

</div>

so i wanted to make it so if the player is not touching any blocks, they fall.  
here is my code:

```auto
for (int ix = -2; ix < resolution+4; ix++) {
        for (int iy = 0; iy < height/(width/resolution)+4; iy++) {
          if (terrainbase[playerxpos+ix][playerypos+iy] == 0) {
            rect(visxpos+ix+width/resolution*(ix-1), visypos+iy+width/resolution*(iy-1), width/resolution+1, width/resolution+1);
            if (visxpos+ix+width/resolution*(ix-1)-50 < mouseX-25 && visxpos+ix+width/resolution*(ix-1)+50 > mouseX+50
            && visypos+iy+width/resolution*(iy-1) < mouseY && visypos+iy+width/resolution*(iy-1)+50 > mouseY) {
              visypos -= (width/resolution)/10;
              //println(frameCount);
            }
          }
        }
      }

```

the first & second for loops just loop through the area the player can see.

terrainbase is the array, that saves the world

“width/resolution” is the width of a tile in the world

the first if() statement checks if a tile is air

the first rectangle int the loop draws rectangles on the screen where also the tiles are

the second if() statement tries to check if the player is touching only air.

“visxpos+ix+width/resolution\*(ix-1)” and the others that look very alike, are the current tile of the terrain that is air

mouseX and mouseY represent the postion of the player on screen

“visypos -= (width/resolution)/10” moves the player down.

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/8/188e6b92fb60d7decb8d1da67ef5cb682b69da6b.png)  
that big black box is the player.

problem is sometimes the player isnt moved down or it is beeing moved down even when they are touching something.

im trying to do this kind of collision:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/6/361d12fa91d752ff6331e11654719aea34a71f28.png)  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/6/6685000f02007d9b39d28d9d1634390b5d51df87.png)  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/d/8d7f187ee9d3900cfc4ed5c532e3efcdca6720aa.png)  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/a/eaf54dfa2b497fcab5bb9a8132664a3b98159fd5.png)

blue means fall, green means stay.

i hope this kinda helps.

---

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [May 1, 2023, 1:39am UTC](https://discourse.processing.org/t/hitbox-detection/41871/6 "2023-05-01T01:39:42Z")

</div>

also the game is a side view, not top down!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 2, 2023, 9:44pm UTC](https://discourse.processing.org/t/hitbox-detection/41871/7 "2023-05-02T21:44:14Z")

</div>

I am not sure I understand your code, because I don’t know your entire code.

* * *

But basically you need to detect the cell the player is in.

Then you check whether the cell underneath it is not air.

Set a boolean marker falling

- to true when it’s air
- to false when it’s solid

Where you move the player check `falling` :

```auto
if(falling) 
    playerY++; // or what your variable name is called 

```

make `falling` a global variable: `boolean falling = false;`

Chrisir

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 2, 2023, 10:55pm UTC](https://discourse.processing.org/t/hitbox-detection/41871/8 "2023-05-02T22:55:31Z")

</div>

here is an example… without the marker

I am not too happy with it

```auto
PVector player=new PVector(800, 400);
boolean jump=false;

String mapPattern =
  "111110111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111";
ArrayList<Cell>map=new ArrayList();

// ----------------------------------------------------------------------------------

void setup() {
  size(900, 900);

  int i=0;
  for (char c : mapPattern.toCharArray()) {
    if (c=='1')
      map.add(new Cell(i*27, 500, true)) ;
    else map.add(new Cell(i*27, 500, false )) ;

    i++;
  }//for
  //
}//func

void draw() {
  background (0, 0, 211);

  //display player
  fill(255, 0, 0);
  stroke(0);
  ellipse(player.x, player.y, 10, 20);

  //display map
  for (Cell c : map) {
    c.display();
  }

  managePlayer();

  keysPlayer();
}//func

// -------------------------------------------------------------------------------

void managePlayer() {
  // fall/lift
  for (Cell c : map) {
    // are we inside the cell (in x)
    if (player.x-5 >= c.cell.x &&
      player.x+5 < c.cell.x+c.w ) {
      // hit x
      if (player.y+10 < c.cell.y || c.air)
        player.y++; // fall
      else jump=false; // stop jump

      // we can lift the player when it's too low
      if (!c.air) {
        if (player.y+10 > c.cell.y) {
          player.y--;
        }
      }
    }//if
  }//for
}

void keysPlayer() {
  // player keys
  if (!keyPressed)
    return;

  if (keyCode==LEFT)
    player.x-=2;
  else if (keyCode==RIGHT)
    player.x+=2;
  else if (key==' ') {
    if (!jump)
      player.y-=32; // jump
    jump=true;
  }
}

// ====================================================================================================================

class Cell {

  PVector cell=new PVector(800, 400);
  float w=27, h=27;
  boolean air=true;

  // constr
  Cell(float x, float y,
    boolean full ) {
    cell=new PVector(x, y);
    if (full)
      air=false;
  }//constr

  void display() {
    if (air) {
      noFill();
      noStroke();
    } else {
      fill(0, 255, 0);
      stroke(0);
    }

    rect(cell.x, cell.y,
      w, h);
  }//method
  //
}//class
//

```

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [May 3, 2023, 2:29am UTC](https://discourse.processing.org/t/hitbox-detection/41871/9 "2023-05-03T02:29:50Z")

</div>

The world is a 2-D array. Just compute the left, right, top, and bottom array indexes that the player overlaps and only check the rectangle of cells in the grid that the player could possibly touch.

---

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [May 3, 2023, 4:23pm UTC](https://discourse.processing.org/t/hitbox-detection/41871/10 "2023-05-03T16:23:17Z")

</div>

guys i actually did it! it works now! thank youuu!!!
