PDA

View Full Version : Collision detection



03-10-2001, 12:45 PM
I am trying to implement simple collision detection in my code.

I need to figure out how the position of the moving object i.e. the user keeps changing in relation to the world. I have tried multiplying points in the bounding rectangle by the modelview matrix to get the transformed vertices but it does not seem to work. Is there a better way to do it?

Thanks.

Vibi

RandyU
03-10-2001, 08:15 PM
gluProject, FeedBack buffer.
multiplying by ModelView matrix works.

Leyder Dylan
03-11-2001, 10:41 AM
Hi,

On my site : http://slug-production.ibelgique.com

You'll find a detection collisio based on the 23 's NeHe 's tut.

Michael Steinberg
03-11-2001, 03:03 PM
Leyder, I think you mailed me. I'll post that code here, so everybody can improve it and so that I don't have to mail it everyone, since I have not time currently. I don't give any guarantees, but it's working for me. Have fun.




// reads 16 bit integer
short file_read_short( FILE *file )
{
short result;

fread( &result, 2, 1, file );
return result;
}

// reads 16 bit unsigned integer
unsigned short file_read_ushort( FILE *file )
{
unsigned short result;

fread( &result, 2, 1, file );
return result;
}

// reads 32 bit integer
int file_read_int( FILE *file )
{
int result;
fread( &result, 4, 1, file );
return result;
}

// reads 32 bit unsigned integer
unsigned int file_read_uint( FILE *file )
{
unsigned int result;
fread( &result, 4, 1, file );
return result;
}

// reads 32 bit float
float file_read_float( FILE *file )
{
float result;

fread( &result, 4, 1, file );
return result;
}


int texture_load_bmp( char file_name[_MAX_PATH] )
{
FILE *file;
BITMAPFILEHEADER file_header;
BITMAPINFOHEADER info_header;
RGBQUAD *palette;
int i;

file = fopen( file_name, "rb" );
if( !file ) {
printf("\nInvalid filename %s", file_name );

return 0;
}

// read fileheader structure
file_header.bfType = file_read_ushort( file );
file_header.bfSize = file_read_uint ( file );
file_header.bfReserved1 = file_read_ushort( file );
file_header.bfReserved2 = file_read_ushort( file );
file_header.bfOffBits = file_read_uint ( file );

// read infoheader structure
info_header.biSize = file_read_uint ( file ); // 4
info_header.biWidth = file_read_int ( file ); // 4
info_header.biHeight = file_read_int ( file ); // 4
info_header.biPlanes = file_read_ushort( file ); // 2
info_header.biBitCount = file_read_ushort( file ); // 2
info_header.biCompression = file_read_uint ( file ); // 4
info_header.biSizeImage = file_read_uint ( file ); // 4
info_header.biXPelsPerMeter = file_read_int ( file ); // 4
info_header.biYPelsPerMeter = file_read_int ( file ); // 4
info_header.biClrUsed = file_read_uint ( file ); // 4
info_header.biClrImportant = file_read_uint( file ); // 4
// -
// 40 bytes
fseek( file, info_header.biSize-40, SEEK_CUR );

if( info_header.biCompression != BI_RGB ) {
printf("\nOnly Uncompressed BMP files are supported");
return 0;
}

//
// read palette
//
if( info_header.biBitCount == 1 ) {
palette = (RGBQUAD*) malloc( 2*sizeof(RGBQUAD) );
for( i=0; i<2; i++ ) file_read_rgbquad( &amp;palette[i], file );
printf( "\nMonochrome texture" );
} else if( info_header.biBitCount == 4 ) {
palette = (RGBQUAD*) malloc( 16*sizeof(RGBQUAD) );
for( i=0; i<16; i++ ) file_read_rgbquad( &amp;palette[i], file );
} else if( info_header.biBitCount == 8 ) {
palette = (RGBQUAD*) malloc( 256*sizeof(RGBQUAD) );
for( i=0; i<256; i++ ) file_read_rgbquad( &amp;palette[i], file );
} else if( info_header.biBitCount == 16 ) { // no palette for abouve 16 bits
fseek( file, info_header.biClrUsed*sizeof(RGBQUAD), SEEK_CUR ); // fixme, is that correct?
palette = NULL;
} else if( info_header.biBitCount == 24 ) {
palette = NULL;
} else if( info_header.biBitCount == 32 ) {
palette = NULL;
} else {
printf("\nUnsupported Bitcount");
return 0;
}

//
// read image data
//
int num_pixels = info_header.biWidth * info_header.biHeight;
int num_bits;
int read_bytes;

if( info_header.biBitCount == 1 ) {
num_bits = num_pixels;
if( num_bits % 8 ) {
read_bytes = (num_bits+1)/8; // read a byte more for odd pixelcount
} else read_bytes = num_bits/8;
//printf("\nPosition: %u Reading %u bytes", ftell(file), read_bytes);
} else if( info_header.biBitCount == 4 ) {
num_bits = num_pixels*4;
if( num_bits%8 ) {
read_bytes = num_bits/8+1;
} else read_bytes = num_bits/8;
//printf("\nReading %u bytes", read_bytes);
} else {
read_bytes = num_pixels * info_header.biBitCount / 8;
}

unsigned char *img_array = (unsigned char*) malloc( read_bytes );
fread( img_array, 1, read_bytes, file );
fclose( file );

//
// process image data
//
byte *RGBimage = (byte*) malloc( num_pixels*3*sizeof(byte) );


if( info_header.biBitCount == 1 ) {
unsigned char bit;
unsigned char look_at;
unsigned char masks[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };

for( i=0; i<num_pixels; i++ ) {
look_at = img_array[i/8]; // current byte
bit = look_at&amp;masks[i%8]; // mask current bit in byte
bit>>=(7-(i%8)); // move bit to first pos
RGBimage[i*3+0] = palette[bit].rgbRed;
RGBimage[i*3+1] = palette[bit].rgbGreen;
RGBimage[i*3+2] = palette[bit].rgbBlue;
}
} else if( info_header.biBitCount == 4 ) {
unsigned char nibble;
unsigned char look_at;
unsigned char masks[2] = { 240, 15 };

for( i=0; i<num_pixels; i++ ) {
look_at = img_array[i/2]; // current byte
nibble = look_at&amp;masks[i%2];
nibble>>=(((i+1)%2)*4);
RGBimage[i*3+0] = palette[nibble].rgbRed;
RGBimage[i*3+1] = palette[nibble].rgbGreen;
RGBimage[i*3+2] = palette[nibble].rgbBlue;
}
} else if( info_header.biBitCount == 8 ) {
for( i=0; i<num_pixels; i++ ) {
RGBimage[i*3+0] = palette[img_array[i]].rgbRed;
RGBimage[i*3+1] = palette[img_array[i]].rgbGreen;
RGBimage[i*3+2] = palette[img_array[i]].rgbBlue;
}
} else if( info_header.biBitCount == 16 ) {
unsigned short mask[3] = { 31744, 992, 31 }; // 31 = blue, 992 = green, 31744 = red
unsigned short *pointer;
unsigned char r, g, b;
for( i=0; i<num_pixels; i++ ) {
pointer = (unsigned short*)&amp;img_array[i*2];
r = (*pointer&amp;mask[0])>>10;
g = (*pointer&amp;mask[1])>>5;
b = (*pointer&amp;mask[2]);
RGBimage[i*3+0] = r*8;
RGBimage[i*3+1] = g*8;
RGBimage[i*3+2] = b*8;
}
} else if( info_header.biBitCount == 24 ) {
for( i=0; i<num_pixels; i++ ) {
RGBimage[i*3+0] = img_array[i*3+2];
RGBimage[i*3+1] = img_array[i*3+1];
RGBimage[i*3+2] = img_array[i*3+0];
}
} else if( info_header.biBitCount == 32 ) {
for( i=0; i<num_pixels; i++ ) {
RGBimage[i*3+0] = img_array[i*3+0];
RGBimage[i*3+1] = img_array[i*4+1];
RGBimage[i*3+2] = img_array[i*4+2];
}
} else {
// some error
if( palette ) free( palette );
free( img_array );
return 0;
}

format = GL_RGB;
width = info_header.biWidth;
height = info_header.biHeight;

free( img_array );
if( palette ) free( palette );

pic_buffer = RGBimage;

return 1;
}


[This message has been edited by Michael Steinberg (edited 03-11-2001).]

Michael Steinberg
03-19-2001, 07:14 AM
Here we go...