Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

Ads

#1 2011-02-22 18:11:55

Duh_Killer
New member
Registered: 2011-02-17
Posts: 5

Collision Detection

Hi all!! Excuse me for my bad english!

I'm actually developping a game just for fun, and I'm quite impressed by what I've accomplished so far!
But despite all the problems I've encountered and solved, there is still this one that I cannot solve myself!

Could someone explain how we can manage a collision detection in the gameboy?
I've tried lots of algorithme that worked more or less, but was never a perfect one...
Searching through internet I found only 2 pages explaining how they tried to do the collision detection function, but that's what i had already tried and it is not what I want..

Basically It would be saying wich tile is solid from the tileset, if you get over it or if you touch it you can't pass through it. And a function really simple is what I search.

So please help me with this huge problem! It is the only problem that stops me from finishing my game!
Thank you.

and I hope it isn't a really simple task because i've searched everywhere to know how to do it!

Offline

 

#2 2011-02-24 16:35:29

frax91
New member
Registered: 2011-02-12
Posts: 7

Re: Collision Detection

Maybe you could have a two dimenttional array representing the tileset (rows,cols) with 1's and 0's (1 for solid 0 for non solid)
and compare the player's pos to it.        i use to do this kind of things making games in javsascript +html5

for example

if (input & J_LEFT)
{
if (tilearray[x+1][y]==0)
{
move()
}
if (tilearray[x+1][y]==1)
{
dont_move()
}
}


I hope this shity explanation helps you.

Last edited by frax91 (2011-02-24 16:36:10)

Offline

 

#3 2011-02-24 19:23:14

Duh_Killer
New member
Registered: 2011-02-17
Posts: 5

Re: Collision Detection

Thx for answering!

Well I know that's the main idea of collision detection and that's what i tried already but here my sprite does 32x32 pixels and using this method becomes really difficult!
How can i avoid using a for loop looking at all y, y+1, y+2... y+32 pos and everytime checking if all tiles located at x+1 (x+33 in my example) are solid or not?

Is it the only way how collision detection work?

If you have any question regarding details on what i'm looking for or on comprehension please ask

And thx again frax91! I was a bit dismantled in front of this problem but now you gave me a little more faith in me to solve this problem!!!

EDIT : Plus, creating an 2D array to put 1 and 0 isn't an easy task either! if you take the map that does 30x18 (small map) tiles it does an array of 540 operands plus 8x8 pixels for each tiles which makes us a 64 * 540 = 34 560 pixels where to put 1 and 0!!!!

so if we just take tiles (and yet 540 tiles here) and we put 1 or 0 to each tile, when it comes to the "collision check" and say we have x,y positions of the player and let's say w,h (Width, Height) position of the 8x8 SOLID tile when we want to see if x+1 is "1" or "0" we would look if x+1 is in the (w,h) area! Is that it?? Would this work?? (it's reaaallly complicated...) Let me see if i can put this in code (roughly):

int x,y;                      //position of player    it is a sprite 32x32 and x is in the center left of the sprite
int w,h;                     //position of the solid tile
char MapArray[];       //the array where 1 and 0 are put (here MapArray[w*h]=1) The tile position in x,y is x=w*8 y=h*8 (because tiles are 8x8)

if(input & J_RIGHT)
  {
  for (int i=y-16; i <= y+16; i++)
  {
    if(x+33 >= w*8 & x+1 < w*8+8)
    {
      if(i >= h*8 & i < h*8+8)
      {
        DONOTHING
      }
    }
  }
  else
  {
  x++;
  }


That is if we already know that w and h are the SOLID tile position and that x+1 is in it...
I can't figure out how we can check in what tile x+1 is... For this we would call a function GiveW and GiveH that gives w and h that are solid tiles position and are the nearest from x and y..

That is really sh**ing me...

Last edited by Duh_Killer (2011-02-24 20:28:51)

Offline

 

#4 2011-02-24 22:38:59

frax91
New member
Registered: 2011-02-12
Posts: 7

Re: Collision Detection

I'm sorry I did a very short and obscure response xD I'll try to explain it better, First I have to say I haven't did really anthing involving sprites on game boy, only text but I will try to help.

in the map array (of course based on tiles, not pixels) you also represent the player. @=player

a example map,
1,1,1,1,1,1,1,1
1,0,0,0,0,0,0,1
1,0,0,0,@,0,0,1
1,1,1,1,1,1,1,1


int posx /*xposition in the tile map
int posy
int disp_x /* x displacement from the center of the tile
int disp_y

if(input & J_RIGHT
{
if (map[pos_x+1==0])
{
disp_x++; /*assuming that you move one pixel every time you push right
}
else {
noop
}
}

Then on you have some if's like

      if(disp_x>16)
    {
    map[posy][posx]="0" // you clean from the map the current player's position
    map[posy][posx+1]="@" // you put it one to right in the map
    disp_x=-disp_x; // this way it will be on posx+1 disp_x-16
}

now on the drawing function you use as x and y position of the player posx+disp_x posy+disp_y. doing the drawing using the tile as "model" can be a little bit tricky
but I'm sure you can do it,


All of this is VERY VERY schematic because you'll probably want to know other properties from the next tile like if it hurts you or if it's the exit but if you are just doing a simple maze game its pretty much all what you need. Sorry too for my scketchy english I'd like to know more exactly what are you doing.

Be tile based my friend!

Offline

 

#5 2011-02-25 07:23:06

Duh_Killer
New member
Registered: 2011-02-17
Posts: 5

Re: Collision Detection

Wow!!! I see now!!!

That's exactly it!! Thanks a lot!! I'll see if it works right now!

I'm just making a super mario like game but a bit more advanced (I am not talking about the collision detection...)!!!

EDIT : After experimenting it works!!!! There are few problems but this is alright!!! if i had to sum everything here how it works!

posx and posy are tile position of the player in the 2D maparray (where 1 and 0 are put)
x and y position in pixels
void CollisiondetectR is the functions that look if the tile in front of the player is 1 or 0
and thats it!

Here is how i coded the collision detection, if someone wants to improve what i put please reply!!

int x,y;
int posx,posy;                                             //tile position of the player basically posx=x/8 only if x is divisible by 8 same for posy=y/8
char MapCol[][];                                         //Map containing 1 and 0 (could be other numbers too)


int CollisionCheckRight( char postx, char posty, char MapCol[][], int Property)        //where Property is the tile Property (1 or 0)
{
    if(MapCol[posy][posx+1] == Property)
        return(1);
    return(0);
}

this is the function and we use it here!

if(input & J_RIGHT)
{
if(CollisionCheckRight(posx, posy, MapCol, 1) == 0)
              goRight();
}

and we have in goright() :

int pos=0;
pos++;                                                      //everytime we go x++ right


so the main function is:

while(1)                                                    //infinite loop
{
if(pos == 9)
    pos= 1 ;
    posx++;
else if(pos == 0)
    pos = 8;
    posx--;

if(input & j_right)
...
}

Here it is!!!
Thx a lot frax91!! I hope this helps people looking for this!

Last edited by Duh_Killer (2011-02-25 07:48:12)

Offline

 

#6 2011-02-25 08:46:10

frax91
New member
Registered: 2011-02-12
Posts: 7

Re: Collision Detection

If you are making a plataformer the colision detection on sprite corners is tricky too. tell me if you need help

I'm happy you got it, I hope you'll post the rom. Its good to see something allive in this forum.

Offline

 

#7 2011-02-25 17:00:44

Duh_Killer
New member
Registered: 2011-02-17
Posts: 5

Re: Collision Detection

Well you helped me a lot already i wont bother you again!!
And sure when i finish the rom i'll post it here!!
It's true that there aren't a lot of people around here.. fortunatly you were there!!

This collision always causes problem but now i understand how to manage it so in less than a day it'll be perfect!!!

But for the rest of the game it'll take some time cause i'm getting back to school.. I'll wait summer vacation to finish it, hope you'll be there when i post the rom!

Offline

 

#8 2015-03-09 12:16:05

Jesse
New member
Registered: 2015-03-09
Posts: 5

Re: Collision Detection

Hello,

First my apologize for posting in an old thread. I don't know if this forum has rules against thread bumping but most do. However the question I got is regarding this code Duh_Killer made.

I tried to copy the way the code was made but due to my original code I had to change it to fit, so I used pointers (like int* pointer). I did that because I wanted to load a different collision map everytime I entered another room. However working with this pointer made the compiler doing weird stuff, mainly spamming a sentence: "WARNING possibily wrote twice...".

I haven't tried this exact code, because the code only provides collision for one level. Every time I want to load a new level I need to make a whole new input function with only this line changing "if(CollisionCheckRight(posx, posy, MapCol, 1) == 0)";

Does anyknow got an idea how to best handle this, or how to work properly with pointers maybe? if it is needed I can post my source code of the game, but its a chaos though.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson