Skip to main content
gamedev.net
🔒 Locked

Collisions

Started by TheShadow 1 reply 400+ views
Original Post
TheShadow
TheShadow
What is the best way of implementing collision detection in openGL orthographic view if i use quads for everything... tiles... player... everything... Every object is drawn with a class, an object class.
  
Object::Object() 
{
	spAnimation = false;
	spriteCounter = 0;
}

BOOL Object::draw_object(GLfloat posx, GLfloat posy, GLfloat direction, int textureID)
{
	
	glLoadIdentity();
	glPushMatrix();
	glTranslatef(posx + 0.5, posy+ 0.5, 0.0f);
	glRotatef(direction, 0.0f, 0.0f, 1.0f);
	if(spAnimation == true)
	{
		glBindTexture(GL_TEXTURE_2D, texture[sprite]);
	}
	else
	{
		glBindTexture(GL_TEXTURE_2D, texture[textureID]);
	}
	glBegin(GL_QUADS);
		glTexCoord2f(1.0f, 1.0f); glVertex2f( 0.5f, 0.5f);
		glTexCoord2f(1.0f, 0.0f); glVertex2f( 0.5f,  -0.5f);
		glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.5f,  -0.5f);
		glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.5f, 0.5f);
	glEnd();
	glPopMatrix();
	return TRUE;

}

void Object::Animate(char *pArray, int rate, int spSize)
{
			

			if(spTimer.GetElapsedTime() > rate)
			{
				
				
				if(spriteCounter == spSize)
				{
					spriteCounter %= spSize;
					
				}

				spTimer.Initialize();
				sprite =  pArray[spriteCounter];
				spriteCounter++;
				
			}
			
}
  
Now if I handle objects by having a structure in my main program loop
  
struct sObject
{
	float x;
	float y;
	float rotation;
	int sprite;
	bool status;
	bool cloack;
	int direction;
} ;
  
With which for every Object i declare... I also declare an object structure so i can modify position, rotation, single sprite, status (for keypress), cloacking and direction. So with this method how can I make it so that I know when my objects collide... much like a CheckForCollision() Function ... but that would have to be done for EVERY object... its quite hard for me... im new at this
Mecha Engineer (Making Real Humanoid Suits)
Ilici
Ilici
if u want quad to quad collision its simple

just a huge if
here are ur quads:

  
p1
+------+
| p2 |
| +-x-----+
| | |
+----x |
| |
| |
+-------+

u can easylly get the collision conditions:

there are 4:
here is 1: p2.x > p1.x && p2.x < p1.x + quad1.width
and another: p2.y > p1.y && p2.y < p1.y + quad1.height
the others are similar

if u want better collision use bimap masks

for a bimap, trace its contour and fill the inside of that region with 1 in a matrix

make a zero filled matrix in which fit both quads who ur testin (after u do the quad to quad collison) and put the bimaps by ANDing , if u get any ones in the matrix then collision!!! (not the fastest method but works dont think ur gonna need it )

also u dont have to test all the quads, just the ones that are visible (when u draw them, put the visible ones in a linked list and teste only those )

[edited by - Ilici on April 22, 2003 3:44:05 PM]
Kyo
Kyo
OT: Is quads the only way of doing 2D in OpenGL?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.