gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Annoying problem

#7652 - Psyk - Sun Jun 22, 2003 1:26 pm

For collision detection i build up a collision map based on the tiles. It checks through every tile and if it's a walkable tile it will set the corresponding entry of the collision map to 0 and if not it sets it to 1. This works fine when i check it against the map data itself but when i try to check it against a pointer to the map data it just doesn't work. It doesn't come up with an error when i compile it, it just sets the whole collision map to 1s. Does anyone have any idea why it doesn't work?

#7653 - funkeejeffou - Sun Jun 22, 2003 1:54 pm

Could you please show the collision code and how you define your pointers?

#7657 - Psyk - Sun Jun 22, 2003 6:21 pm

Code:
   for(loopx=0;loopx<bg->sizeX;loopx++)
      for(loopy=0;loopy<bg->sizeY;loopy++)
         if(bg->mapData[bg->sizeX*loopy+loopx]==0x18)
            collisionMap[loopx][loopy]=0;
         else
            collisionMap[loopx][loopy]=1;



mapData is part of a structure for the bg that is the pointer to the actual map data. Here is how the pointer is defined:

Code:

const Bg rooma = {other structure data ,(u16*)room, more structure data};


room is the name of the actual map data.

#7741 - Cyberman - Mon Jun 23, 2003 7:55 pm

Psyk wrote:

Code:

const Bg rooma = {other structure data ,(u16*)room, more structure data};


room is the name of the actual map data.

Hmmm how about trying this instead
Code:

const Bg rooma = {other structure data ,(u16*)&room, more structure data};


I think the problem is actually bad coding habits, I've seen this mistake a lot, Assuming that putting a variable (room) will automatically dereference it. This only happens with type char and only in certain circumstances. I wouldn't depend on it.

ALWAYS use the dereference operator ( & ) to be sure you get a POINTER and not the actual variable or the first few elements of the array cast to a pointer (thus it point god knows where).

Cyb

#7747 - Psyk - Mon Jun 23, 2003 10:37 pm

Well i tried your suggestion but i still have the same problem. Still I'll remember to put the & operator in the future. Maybe there could be a problem here:
Code:
if(bg->mapData[bg->sizeX*loopy+loopx]==0x18)


Is there some operator or something that i'm missing out here?

#7784 - Psyk - Tue Jun 24, 2003 4:42 pm

I've sorted it now but I still don't know why it didn't work before. To fix it I added the pointer to the map data as a seperate argument for the function instead of it being part of the bg structure.

#7785 - johnny_north - Tue Jun 24, 2003 4:46 pm

I don't see anything wrong here. Are you certain that the map data actually contains 0x18 as one of the tile entries? Does this actually correspont to a pathway tile? Never hurts to double check.

#7786 - Psyk - Tue Jun 24, 2003 4:53 pm

Yeah it definately does as it worked before when i was refering directly to the data without a pointer and it works now that the pointer is seperate to the bg strucutre.

#7799 - GbaGuy - Tue Jun 24, 2003 11:19 pm

I thought that & was the Address Of operator and * was the
dereference operator...

Now I'm confused... :(

#7808 - Lupin - Wed Jun 25, 2003 1:25 pm

var1 = var2 & var3; //That's logical AND
var1 = &var2; //That will get var2's address and store it into var1

* is multiply and for dereferencing, but if you have double pointers you need paranthesis for dereferencing, like this:

(*var1)[2] = var3; //this would solve var1 first and then solve it again using array index

the context is important for these operators