Newbs need help with collision detection.

Understanding the language, error messages, etc.

Newbs need help with collision detection.

Postby Duhjoker » Tue Aug 02, 2016 11:55 pm

All right so we finished up bitmap and tile map displaying and since this next guide is comepletely off subject it's time for a new thread.

So let's talk about collision detection. This function keeps your player sprites from walking into other sprites including terrain and and monster sprites.

I myself have looked around for a few days and though I have found some threads mentioning collision detection I have found no examples on the threads or the reference but the Gamebuino examples have a physics example kind of since there's lil notation of what the functions do. And to be honest I'm clueless as usual!

At this point you should have knowledge of displaying sprites and moving them using basic controls.

So where do I start?
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Newbs need help with collision detection.

Postby Duhjoker » Wed Aug 03, 2016 4:46 am

Ok I was googling Gamebuino bitmap collision and found a link to here to the reference for the gb.collide.bitmapbitmap.

Code: Select all
gb.collideBitmapBitmap(parameters)


It labels the coordinates for each collision in the parameters as.....

x1: horizontal coordinate of the first bitmap
y1: vertical coordinate of the first bitmap
b1: first bitmap
x2: horizontal coordinate of the second bitmap
y2: vertical coordinate of the the second bitmap
b2: second bitmap

Please correct me if m wrong but it looks like you need to the list the coordinates for each Sprite that you want solid or for your character Sprite to collide with. Fortunately you only have to list some of the coordinates for my maps, just the outside edges.

Unfortunately there is no example on the reference so I have no idea how exactly to implement it.


Update found the gab.collide.bitMapbitMap() example here ......

https://github.com/Rodot/Gamebuino/blob ... Bitmap.ino
Last edited by Duhjoker on Wed Aug 03, 2016 5:02 am, edited 1 time in total.
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Newbs need help with collision detection.

Postby Skyrunner65 » Wed Aug 03, 2016 4:53 am

From what I could tell, here's how the bitmap collision detection works:

If BLACK pixels are directly next to each other, they don't collide.
However, if BLACK pixels are overlapping, the it collides.

The function itself is structured so:

Code: Select all
gb.collideBitmapBitmap(bitmap1x,bitmap1y,bitmap1,bitmap2x,bitmap2y,bitmap2)

Just the X, then the Y, then the name of the bitmap (and you may be able to switch out the name for a variable, allowing for animated sprites).
Also, do keep in mind that the X and Y CAN use variables. :)
User avatar
Skyrunner65
 
Posts: 371
Joined: Thu Mar 20, 2014 5:37 pm
Location: NC,USA

Re: Newbs need help with collision detection.

Postby Duhjoker » Wed Aug 03, 2016 5:26 am

Uhhhh I've been looking at code all day and starting to get a lil fuzzy. Are you saying there is only a collision when one black pixel tries to overlap another.


Can this function be used with gb.display.tileMap? I've already have a working sketch so I don't need it to display anything.

How many coordinates can you add to the parameters.?
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Newbs need help with collision detection.

Postby Duhjoker » Wed Aug 03, 2016 5:40 am

Also my map is 14 X 8 and uses 6x6 pixel bit maps. Do I count by placement of each placed Sprite with in my tilemap routine or do I have to center the Sprite somehow. I don't really think I can explain this any better.

So if a sprite is labeled as being at y8, x8 on the tilemap do I also place the collision command coordinates at y8, x8?

Is it

Y+
X- X+
Y-

Code: Select all

if(gb.collideBitmapBitmap(player_x, player_y, playerBitmap, oponent_x, oponent_y, oponentBitmap)){
      gb.sound.playTick();
      gb.display.print(F("Colliding"));
    }



I don't need to use the gb.sound.play() or the gb.display.print but what command do I put below the if(go.collidebitMapbitMap() since I want my player Sprite to stop?
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Newbs need help with collision detection.

Postby Duhjoker » Wed Aug 03, 2016 6:31 am

ok I tried adding the gb.collideBitmapBitmap() command at the bottom. im getting a primary expression expected before ")" expression and I cant see whats supposed to go there

Code: Select all

#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;

const byte tilemap[] PROGMEM = {14,8,
6,6,
3,3,3,3,3,3,6,6,3,3,3,3,3,3,
3,3,3,5,3,6,6,6,3,3,3,3,3,3,
3,3,6,6,6,6,6,6,6,3,3,3,3,3,
3,6,6,6,6,6,6,6,6,6,3,3,3,3,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,
3,3,6,6,6,6,6,6,6,6,6,6,3,3,
3,3,6,6,6,6,6,6,6,6,6,6,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3};

const byte bush_bottom[] PROGMEM = {6,6,0x48,0x30,0x58,0x00,0x00,0x00,};
const byte bush_top[] PROGMEM = {6,6,0x00,0x00,0x00,0x30,0x48,0xb4,};
const byte bush[] PROGMEM = {6,6,0x30,0x48,0xb4,0x48,0x30,0x58,};
const byte rock_terrain[] PROGMEM = {B01011000, B10100100, B00010100, B10100000, B10000100, B01011000 } ;
const byte turtle_rock[] PROGMEM = {6,6,0x48,0xa4,0x14,0xa0,0x84,0x50,};
const byte port_noir[] PROGMEM = {B11111100, B11111100, B11111100, B11111100, B11111100,B11111100 };
const byte port_blanc[] PROGMEM = {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 } ;

const byte *spritesheet[] = {bush_bottom,bush_top,bush,rock_terrain,turtle_rock,port_noir,port_blanc};

const byte sever_front_1[] PROGMEM = {6,6,0x78,0x0,0x0,0x30,0x0,0xcc,};
const byte sever_front_2[] PROGMEM = {6,6,0x84,0x48,0x78,0x84,0x78,0x0,};
const byte sever_left_1[] PROGMEM = {6,6,0x78,0x80,0xc8,0xb4,0x0,0xcc,};
const byte sever_left_2[] PROGMEM = {6,6,0x4,0x58,0x30,0x0,0x78,0x0,};
const byte sever_rear_1[] PROGMEM = {6,6,0X78,0X78,0X0,0X30,0X0,0XCC,};
const byte sever_rear_2[] PROGMEM = {6,6,0x84,0x0,0x78,0x84,0x78,0x0,};
const byte sever_right_1[] PROGMEM = {6,6,0x78,0x4,0x4c,0xb4,0x0,0xcc,};
const byte sever_right_2[] PROGMEM = {6,6,0x80,0x68,0x30,0x0,0x78,0x0,};

  int player_x = 15;
  int player_y = 15;
  int player_direction = 0;

int x=0,y=0;


void setup() {
  gb.begin();
  gb.titleScreen(F("test"));
  gb.display.persistence = false;
}


void loop() {
  if(gb.update()){
    if (gb.buttons.repeat(BTN_RIGHT,1));
    if (gb.buttons.repeat(BTN_LEFT,1));
    if (gb.buttons.repeat(BTN_DOWN,1));
    if (gb.buttons.repeat(BTN_UP,1));
    gb.display.drawTilemap(x,y,tilemap,spritesheet); // draw the tilemap
//    gb.display.cursorY = 12; gb.display.println( x );gb.display.println( y );






 if(gb.buttons.repeat(BTN_UP,1)){
       gb.display.setColor(BLACK);{
    gb.display.drawBitmap(player_x,player_y,sever_rear_1);
       }
    gb.display.setColor(GRAY);{
    gb.display.drawBitmap(player_x,player_y,sever_rear_2);
    }
      player_direction = 1;
      player_y = player_y - 1;}
    if(player_y <= 0){
      player_y = 0;}



    if(gb.buttons.repeat(BTN_DOWN,1)){
      gb.display.setColor(BLACK);{
    gb.display.drawBitmap(player_x,player_y,sever_front_1);
    }
     gb.display.setColor(GRAY);{
    gb.display.drawBitmap(player_x,player_y,sever_front_2);
    }
      player_direction = 2;
      player_y = player_y + 1;}
    if(player_y >= 40){
      player_y = 40;}
 


    if(gb.buttons.repeat(BTN_RIGHT,1)){
       gb.display.setColor(BLACK);{
    gb.display.drawBitmap(player_x,player_y,sever_right_1);
    }
     gb.display.setColor(GRAY);{
    gb.display.drawBitmap(player_x,player_y,sever_right_2);
    }
      player_direction = 3;
      player_x = player_x + 1;}
    if(player_x >= 77){
      player_x = 77;}


    if(gb.buttons.repeat(BTN_LEFT,1)){
       gb.display.setColor(BLACK);{
    gb.display.drawBitmap(player_x,player_y,sever_left_1);
    }
    gb.display.setColor(GRAY);{
    gb.display.drawBitmap(player_x,player_y,sever_left_2);
    }
      player_direction = 4;
      player_x = player_x - 1;}
    if(player_x <= -2){
      player_x = -2;}
     
      ////////////PLAYER DIRECTION/////////////

 if (player_direction == 1){
        gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_rear_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_rear_2);
        }
 }
     
      else if (player_direction == 2){
        gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_front_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_front_2);
        }
      }
else if (player_direction == 3){
        gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_left_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_left_2);
        }
}
      else if (player_direction == 4){
        gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_right_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_right_2);
        }
      }
      else {  gb.display.setColor(BLACK);{
        gb.display.drawBitmap(player_x,player_y,sever_front_1);
      }
        gb.display.setColor(GRAY);{
        gb.display.drawBitmap(player_x,player_y,sever_front_2);
        }
      }
        if(gb.collideBitmapBitmap(2,4,4,3,5,4,)); /// player_x, player_y,progmem byte integer
  }
 
}


the bitmap names have to be added as byte integers because of the way they are stored in progmem
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Newbs need help with collision detection.

Postby Duhjoker » Wed Aug 03, 2016 9:02 am

Im looking around but cant see an example that can tell me hiw yo finish off my collision code.

Do i need to write a command for the player to do something when the character collides with it?
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Newbs need help with collision detection.

Postby naed » Wed Aug 03, 2016 10:22 am

Summoner123 has a post here that explains how he got collision detection working specifically with tilemaps

the other issues you may come up against it the fact you are using two different colored sprites, you may need to write a collision detection routine for both

there is also an example of collision detection in the Gamebuino library that you could look at
User avatar
naed
 
Posts: 140
Joined: Tue May 31, 2016 3:18 pm

Re: Newbs need help with collision detection.

Postby Duhjoker » Thu Aug 04, 2016 2:04 am

Yea i tried using the example from the arduino library but it wants more parameters inside the (). I posted it above. Trying to stay off the computer today but ill prolly check your info soon.

Off topic====

Im thinking of making my own gamebuino clone with more pixel resoloution. At about 176X220 which is perfect for zelda. Problem is all the ones i can find are 256 color, can a color screen be usef?
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Re: Newbs need help with collision detection.

Postby Duhjoker » Thu Aug 04, 2016 4:28 am

OK so I was looking at the thread you linked me to and did some reading and it turns out that gb.collideBitmapBitmap() doesn't work well with the tileMap command but gb.collide.rectrect() does so he suggested this.......

Code: Select all
for(i=0; i < gb.display.numcolision + 1; i++)
   {
    if(gb.collideRectRect(player.posx,player.posy,8,8,gb.display.solid[i].x,gb.display.solid[i].y,8,8)) /// what does the data in the parameters define
    {
          if(gb.display.solid[i].spritecol == floor1); //Do nothing because it's floor
else      if(gb.display.solid[i].spritecol == arvore)gb.popup(F("Tree"),10);
else      if(gb.display.solid[i].spritecol == agua2 )gb.popup(F("Water"),10); /////why two laguages
else      if(gb.display.solid[i].spritecol == agua3 )gb.popup(F("Water"),10);
else      if(gb.display.solid[i].spritecol == tijolo)gb.popup(F("Brick"),10); //what do the 10's define

      }   
  } 
   
    if(gb.buttons.repeat(BTN_B,1))
    {
      gb.display.print("CPU Load: ");
      gb.display.print(gb.getCpuLoad());
      gb.display.cursorX = 0;
    }


looks like I just need to add the command above to the code above by button press routine and that im just naming the sprites I want to be solid. what I don't understand is why list the name in both Spanish and English.

Update:: just remembered what gb.popup needs so I can cut those out since I don't want it to display anything.
User avatar
Duhjoker
 
Posts: 446
Joined: Sat Jul 02, 2016 4:57 am
Location: Where Palm trees grow

Next

Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 15 guests

cron