Is there a better way of checking for colliding

I have learnt one way of checking for colliding which is
Rect rectA{0,0,0,0}
but is there a better easier way?
if so how to i implement it into my code so it allows me to bump my head against a brick and not go through it?

Again Cody, what are you asking?

Are you having trouble getting the collide(Rect A, Rect B) working?

The easiest way is to give everything a rectangle, calculate where the player is going to move to and if that would collide, prevent the movement.

There’s a better way (if ‘better’ means more accurate to real world physics) but it’s not easier, it’s much much harder.

Wasn’t there a nice article in the Arduboy magazine about collision detection?

1 Like

What I mean is the way I said was not working for me as in it was annoying the heck out of me so is there another way like draw mask?

Posting your code here might help.

The problem with that is very few people can understand my code. But if you insist I must warn you!

Fear not, I’ve seen some very messy code in my time.
I still haven’t forgotten Apple’s goto fail; bug.

2 Likes

Okay you should expect the code at 11:30 Aus time :smile:

Doesn’t Australia have 2-3 different time zones?

Edit: 5 aparently. More than expected.

So 11:30 (assuming am) could be anywhere between 1 to 3 hours from now. I’ll probably be asleep in 3 hours (I hope).

2 Likes

Sure was :slight_smile: Volume 7 Page 38.

5 Likes

sorry but my code was not…saved cn you wait till a while?

How accurate does it need to be? There is a good article in issue 7, that is simple enough and should be sufficiently accurate (unless your wanting to check certain pixels for head shots and such)

2 Likes

I think I have something worked out :smile:

#include <Arduboy2.h>
Arduboy2 arduboy;
#include "Bitmaps.h"

//Tip vol is velocity

#define FRAMES 60

int gamestate = 0;

int8_t x = 0;
int8_t y = 40;




bool jump = false;

int volJ = 0;



int volL = 0;
int volR = 0;

int growx = 110;

void setup()
{
  arduboy.begin();
  arduboy.setFrameRate(FRAMES);
  arduboy.clear();
  

}

void loop()
{
  switch( gamestate )
  {
 case 0:
 arduboy.pollButtons();
 arduboy.clear();
 arduboy.drawBitmap(34,20,Logo,54,39,WHITE);
 arduboy.setRGBled(10,0,0);
 if(arduboy.justPressed(A_BUTTON))
 {
  gamestate = 1;
 }
 arduboy.display();
 break;
 case 1:
 delay(25);
 arduboy.clear();
 arduboy.pollButtons();
 arduboy.drawBitmap(x,y,logan,16,16,WHITE);


if(arduboy.pressed(RIGHT_BUTTON))
{
  volR++;
  volL--;
}
else
{
  volR--;
}





if(arduboy.pressed(LEFT_BUTTON))
{
  volL++;
  volR--;
}
else
{
  volL--;
}











if(arduboy.justPressed(A_BUTTON))
{
  jump = true;
  volJ = 8;
}
if(jump == true)
{
  y = y - volJ;
  volJ--;
}


































if(y > 40)
{
  y = 40;
  volJ = 0;
}

 if(volL > 0)
 {
  x = x - volL;
 }
 if(volR > 0)
 {
  x = x + volR;
 }
if(volR < 0)volR = 0;
if(volL < 0)volL = 0;



arduboy.drawBitmap(60,20,block,16,16);

if(x > 50 and x < 70 and y > 10 and y < 30)
{
  gamestate = 2;
 
}








arduboy.display();

break;
case 2:
delay(25);
arduboy.setRGBled(0,0,0);
 arduboy.clear();
 arduboy.pollButtons();

 arduboy.drawBitmap(x,y,logan,16,16,WHITE);


if(arduboy.pressed(RIGHT_BUTTON))
{
  volR++;
  volL--;
}
else
{
  volR--;
}





if(arduboy.pressed(LEFT_BUTTON))
{
  volL++;
  volR--;
}
else
{
  volL--;
}











if(arduboy.justPressed(A_BUTTON))
{
  jump = true;
  volJ = 8;
}
if(jump == true)
{
  y = y - volJ;
  volJ--;
}


































if(y > 40)
{
  y = 40;
  volJ = 0;
}

 if(volL > 0)
 {
  x = x - volL;
 }
 if(volR > 0)
 {
  x = x + volR;
 }
if(volR < 0)volR = 0;
if(volL < 0)volL = 0;











arduboy.display();

growx--;
if(growx < 0)growx = 0;

  arduboy.fillRect(0,0,growx,64);

























































    
}}


warning your eyes might fall out and burn and then crumble into dust!

First off put notes in there to keep track of what is what,
Second get rid of all the white space

That being said those if statements are going to give you issues
By telling the code to do something IF the left button is pressed is fine but the ELSE isn’t needed, because that will happen anytime the left button isn’t pressed.

There are other issues as well but they might still do what you are trying to do

//Add notes like this
//It will save you headaches

Also just a tip whenever you make a ton of canges SAVE IT FIRST IN ANOTHER FILE so if you screw it up beyond all recognition. You have a place to go back to

Also don’t use delay, a delay is a pause it tells the whole thing to pause at that point in the loop

And I see the same lines of code multiple times in multiple places,

1 Like

There’s a lot of things I could suggest for improving your code, but this isn’t code review so I won’t go into those things unless you want me to.

One thing I will say though is to go easy on the spacing, most of the other issues are understandable, but the spacing made it almost unreadable so I had to adjust the spacing before reading it:

#include <Arduboy2.h>

Arduboy2 arduboy;

#include "Bitmaps.h"

//Tip vol is velocity

#define FRAMES 60

int gamestate = 0;

int8_t x = 0;
int8_t y = 40;

bool jump = false;

int volJ = 0;

int volL = 0;
int volR = 0;

int growx = 110;

void setup()
{
  arduboy.begin();
  arduboy.setFrameRate(FRAMES);
  arduboy.clear();
}

void loop()
{
	switch( gamestate )
	{
		case 0:
			arduboy.pollButtons();
			arduboy.clear();
			arduboy.drawBitmap(34,20,Logo,54,39,WHITE);
			arduboy.setRGBled(10,0,0);
			if(arduboy.justPressed(A_BUTTON))
			{
				gamestate = 1;
			}
			arduboy.display();
			break;
		case 1:
			delay(25);
			arduboy.clear();
			arduboy.pollButtons();
			arduboy.drawBitmap(x,y,logan,16,16,WHITE);

			if(arduboy.pressed(RIGHT_BUTTON))
			{
				volR++;
				volL--;
			}
			else
			{
				volR--;
			}

			if(arduboy.pressed(LEFT_BUTTON))
			{
				volL++;
				volR--;
			}
			else
			{
				volL--;
			}

			if(arduboy.justPressed(A_BUTTON))
			{
				jump = true;
				volJ = 8;
			}
			if(jump == true)
			{
				y = y - volJ;
				volJ--;
			}

			if(y > 40)
			{
				y = 40;
				volJ = 0;
			}

			if(volL > 0)
			{
				x = x - volL;
			}
			if(volR > 0)
			{
				x = x + volR;
			}
		 
			if(volR < 0)volR = 0;
			if(volL < 0)volL = 0;

			arduboy.drawBitmap(60,20,block,16,16);

			if(x > 50 and x < 70 and y > 10 and y < 30)
			{
				gamestate = 2;
		 
			}
			arduboy.display();
			break;
		case 2:
			delay(25);
			arduboy.setRGBled(0,0,0);
			arduboy.clear();
			arduboy.pollButtons();

			arduboy.drawBitmap(x,y,logan,16,16,WHITE);

			if(arduboy.pressed(RIGHT_BUTTON))
			{
				volR++;
				volL--;
			}
			else
			{
				volR--;
			}

			if(arduboy.pressed(LEFT_BUTTON))
			{
				volL++;
				volR--;
			}
			else
			{
				volL--;
			}

			if(arduboy.justPressed(A_BUTTON))
			{
				jump = true;
				volJ = 8;
			}

			if(jump == true)
			{
				y = y - volJ;
				volJ--;
			}

			if(y > 40)
			{
				y = 40;
				volJ = 0;
			}

			if(volL > 0)
			{
				x = x - volL;
			}
			if(volR > 0)
			{
				x = x + volR;
			}
			if(volR < 0)volR = 0;
			if(volL < 0)volL = 0;

			arduboy.display();

			growx--;
			if(growx < 0) growx = 0;

			arduboy.fillRect(0,0,growx,64);
			break;
	}
}

I’m guessing logan is your character?
An ‘block’ is the obstacle?

It’ll be a bit awkward to fit in because of the way you’ve got things structured, but basically, something like this is what you want:
(To replace your own code at around line 90.)

int8_t tempX = x;
if(volL > 0)
{
	tempX = x - volL;
}
if(volR > 0)
{
	tempX = x + volR;
}
if(volR < 0) volR = 0;
if(volL < 0) volL = 0;
	
Rect playerRect = { tempX, y, 16, 16 };
Rect blockRect = { 60, 20, 16, 16 };
if(!arduboy.collide(playerRect, blockRect))
{
	x = tempX;
}

Here tempX is the x coordinate that the player is about to move to, but before you move the player to that position you check if the player (in that position) would be colliding with the block. If the player’s new position would cause them to intersect the block, you don’t move them, you let them stay where they are.

Collision in video games is just a clever illusion.
Technically (when using this approach) there is no collision, just the refusal to move the player into an object.


Agreed.

Firstly if you wanted something to happen when left was pressed and something different to happen when it wasn’t then using an if and an else is absolutely a valid option.

In this case though it’s a bit more complicated, there are other more interesting problems with what’s going on in the if-else around the button checking.

This is why a lot of programmers use github.

delay is ok if used properly, the trick is to not over-use is and to know when to use a more robust technique.

Correct, and there are at least two important changes that could help mitigate that.

Look at those if statements, there else will always counteract the other

Not quite.

if(arduboy.pressed(RIGHT_BUTTON))
{
	volR++;
	volL--;
}
else
{
	volR--;
}

if(arduboy.pressed(LEFT_BUTTON))
{
	volL++;
	volR--;
}
else
{
	volL--;
}

There are 4 conditions to consider:

  1. Right not pressed, left not pressed
  2. Right pressed, left not pressed
  3. Right not pressed, left pressed
  4. Right pressed, left pressed

Case 1 executed statements: volR--; volL--;
Case 2 executed statements: volR++; volL--; volL--;
Case 3 executed statements: volR--; volL++; volR--;
Case 4 executed statements: volR++; volL--; volL++; volR--;

So only case 4 results in complete counteraction.
Granted though, it’s hardly an ideal solution.

1 Like