#1329 - Psyk - Thu Jan 16, 2003 8:27 pm
How do I explicilty define a variable as only a single bit? I'm doing object collision and i just want a simple true or false value. I could define it as an 8bit value but that would waste space.
#1337 - Splam - Thu Jan 16, 2003 9:04 pm
You can use a bitfield in C (a bool basically).
eg
struct collision {
unsigned a: 1;
unsigned b: 1;
unsigned c: 1;
unsigned d: 1;
unsigned e: 1;
unsigned f: 1;
unsigned g: 1;
unsigned h: 1;
} coll;
then to check a bit in that you just do coll.? ? = the letter in the struct
You would of course need to do some kind of typedef on this to access your whole map as that struct only works on 1 byte. The 1; after each letter is the length (in bits) so you can even have a=4bits then b,c,d,e =1bit each. Also if using more than 1 bit you can make them signed or unsigned (can't do that obviosuly with 1 bit hence the unsigned).
-edit-
Of course it's probably a lot easier to do something like this...
Xpos = xpos of the thing you're checking for collisions.
if (collisionmap[Xpos>>3]&(1<<(Xpos&7)))
might need to be (0x80>>(Xpso&7) brain is in a lull at the moment ;)
that will divide your "coarse" postion by 8 to get you a byte from your collision map then mask out everything other than the bit you want to check inside that byte (based on the lower 3 bits of the xpos).
#1351 - Psyk - Thu Jan 16, 2003 11:30 pm
Thanks but i already have a working collision detection. I have an 2D array with each item corresponding to a tile on the bg map. I just wanted to know if there is a way to define it as an array of 1bit values.
#1377 - Touchstone - Fri Jan 17, 2003 11:07 am
If you just create a bitmask for your collision tiles and a bitmask for your objects then you could '&' them together. For instance: Code: |
TileMask (in binary) = 11100000
ObjMask (in binary) = 00001111
Collision = TileMask & ObjMask
|
To check collision if the object has moved one pixel to the left for instance the easiest way would be to create a new ObjMask that is aligned with TileMask.
_________________
You can't beat our meat
#1381 - imikeyi - Fri Jan 17, 2003 1:37 pm
ANSI C has no 'bit' variable, the lowest is a byte (char) unfortunately. Usually I represent booleans as a char.
_________________
microkernel advance
#1435 - KoCMoHaBT - Sat Jan 18, 2003 7:20 pm
Quote: |
You can use a bitfield in C (a bool basically).
eg
struct collision {
unsigned a: 1;
unsigned b: 1;
unsigned c: 1;
|
This syntax is don't work. GCC make a every bitfield a unsigned int, with aligment.
If you want this one worked you must use this one:
union {
struct {
unsigned int a : 1,b : 1,c : 1; // etc. only "","", don't "";""
}; // if anonymous structure don't work, give name to them
unsigned int data;
} collision;
something like this.
Another one thing -- you cant mix the datatypes in struct with bitfields.
#1437 - ampz - Sat Jan 18, 2003 8:26 pm
Easiest way is probably to use a few defines.
For example, this would create a 256 x 256 bit-array:
unsigned char map[32*256];
#define col(x,y) (1&(map[(y<<5)+(x>>3)]>>(x&7)))
#define setcol(x,y,val) val?map[(y<<5)+(x>>3)]|=1<<(x&7):map[(y<<5)+(x>>3)]&=~(1<<(x&7))
In your code, col(5,14) will return the value (1 or 0) stored in x=5, y=14
setcol(3,7,0) will set the bit at 3,7 to 0.
Note: Code is untested.