Results 1 to 5 of 5

Thread: Collision detection

  1. #1
    Guest

    Collision detection

    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

  2. #2
    Senior Member Regular Contributor
    Join Date
    Oct 2000
    Location
    Donetsk,Ukraine
    Posts
    118

    Re: Collision detection

    gluProject, FeedBack buffer.
    multiplying by ModelView matrix works.

  3. #3
    Senior Member Regular Contributor
    Join Date
    Jul 2000
    Location
    Arlon, Belgium
    Posts
    477

    Re: Collision detection

    Hi,

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

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

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Dec 2007
    Posts
    1,077

    Re: Collision detection

    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.

    Code :
    // 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).]

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Dec 2007
    Posts
    1,077

    Re: Collision detection

    Here we go...

Similar Threads

  1. Collision Detection
    By ardalanbiz in forum OpenGL: General
    Replies: 5
    Last Post: 02-07-2004, 02:49 PM
  2. Collision detection.... please help
    By Music_Man in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 11-28-2002, 06:38 PM
  3. collision detection
    By imported_robert in forum OpenGL: Basic Coding
    Replies: 13
    Last Post: 02-17-2001, 03:09 AM
  4. collision detection
    By chip in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-08-2000, 07:09 AM
  5. collision detection contest: collision crazy
    By in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 09-19-2000, 02:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean