Checking for enemy collisions

So recently I asked you if I could check collisions through tiles. Now I’m asking is there a way of doing that for a sepret non map tile?

1 Like
bool Intersect(unsigned Min0, unsigned Max0, unsigned Min1, unsigned Max1){return ((Max0 > Min1) && (Min0 < Max1));}

bool Collision(unsigned x, unsigned y, unsigned x1, unsigned y1,byte S1,byte S2) {return (Intersect(x,x+S1,x1,x1+S2)&&Intersect(y,y+S1,y1,y1+S2));}

yes however you would have to check against all other non map tiles The above code is for equal sized square sprites

Is there a better way this seems overly complicated.

1 Like

if your sprites are of a fixed size yes

bool Collision(unsigned x, unsigned y, unsigned x1, unsigned y1) {return (((x+8 > x1) && (x < x1+8))&&((y+8 > y1) && (y < y1+8)));}
1 Like

what about making the enemy a tile? Is that possible?

What do you mean by making the enemy a tile?

You can check if a x or y vaulue is on a tile could you do this for enemy’s by turning them into tiles?

Yea You can do im confused on what your checking for?

A) Are you checking if an enemy is on a specific tile
B) Collisions between the enemy and the player

A collison. So if there touching arduboy prints a text saying ouch is that possible or are tiles at a fixed point.

Can i have a look at your code?

If you look through this you can find it.

1 Like

That might be an older form of the code because it doesn’t contain anything about the Enemy sprites or such

if it is tile based then a simple check should surfice


That’s the thing how would you make a enemy as a tile? And place it in screen?

Rectangle collisions are simple.

The easiest way (in my mind) is to check if the rectangles don’t intersect and then invert the result.

There’s 4 conditions to consider that show a rectangle isn’t colliding:

(All 4 images taken from this blog.)

If you then have a rectangle class defined like this:
(Disclaimer: don’t worry about what & and const do, just worry about x, y, width, height, getLeft(), getRight(), getTop() and getBottom().)

class Rectangle
  int16_t x;
  int16_t y;
  uint8_t width;
  uint8_t height;

  Rectangle(void) = default;
  Rectangle(int16_t x, int16_t y, uint8_t width, uint8_t height)
  : x(x), y(y), width(width), height(height) {}

  int16_t getLeft(void) const { return this->x; }
  int16_t getRight(void) const { return this->x + this->width; }
  int16_t getTop(void) const { return this->y; }
  int16_t getBottom(void) const { return this->y + this->height; }

Then the function for checking intersection can be defined like this:

bool instersects(const Rectangle & rectangleA, const Rectangle & rectangleB)
	return !
		(rectangleA.getRight() < rectangleB.getLeft()) || // Condition i
		(rectangleA.getLeft() > rectangleB.getRight()) ||  // Condition ii
		(rectangleA.getTop() > rectangleB.getBottom()) ||  // Condition iii
		(rectangleA.getBottom() < rectangleB.getTop()) // Condition iv

See how each of the lines is effectively a translation of the conditions in the diagrams.
Checking that the right side of A is less than the left side of B is the same as diagram i, checking the left side of A is greater than the right side of B is the same as diagram ii etc.

All those conditions are logically or-ed (||) which means if any one of them is true then the whole expression is true.
Then those are all wrapped in some curly braces to make sure they’re grouped as a single expression.
Then the not operator (!) is applied, which turns true into false and false into true, so effectively it’s defining intersection as two rectangle not-not-intersecting (i.e. it finds out if they aren’t intersecting and then gives the opposite result to say whether they are intersecting).

There are already a Rect and arduboy.collide in the Arduboy2 library. The collide function is effectively the same, but the Rect is just a simple struct with x, y, width and height and you need to be able to reference your Arduboy2 object from where you want to use collide because it’s not static.

As for how to manage enemies, that’s a bit more of a difficult one.

Rule 1 of programming is that computers are stupid and they won’t do anything unless they’ve been told to.
The Arduboy has no concept of an enemy, you have to give it one, which means you at least need to know the basics of using structs.
To have multiple enemies you also need to know about arrays.

If you don’t know about structs and arrays.
Then read about arrays and read about structs.

1 Like

Thanks I will try this today