Jump to content

  • Log In with Google      Sign In   
  • Create Account


Calculating the bounded box around a 2D (or 3D) object.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Enerjak   Members   -  Reputation: 232

Like
0Likes
Like

Posted 30 March 2013 - 09:15 AM

http://puu.sh/2qGTC

 

As you can see from the image, I'm trying to calculate the bounding box of the pac-man "sprite" but I've got a few things to fix it seems I was wondering if there's anyway to calculate a bounding box (or sphere) for any object, whether 2D or 3D. Here's my code for the pac man "sprite."

 

glPushMatrix();
		glTranslated(mPosition.getX() + mTranslation.getX(), 
					 mPosition.getY() + mTranslation.getY(),0.0);
		glScaled(mScale.getX(),mScale.getY(),mScale.getZ());
		glTranslated(-mPosition.getX(),mPosition.getY(),0);
		glPopMatrix();

		Vertex* v2[4];
		glBegin(GL_POLYGON);
			glColor4d(0.5,0.5,1,0.2);

			v2[0] = new Vertex();
			v2[0]->setVertexd(-10.0 - Radius + mPosition.getX() - 4, -10.0 - Radius + mPosition.getY() - 4,0);
			v2[1] = new Vertex();
			v2[1]->setVertexd(-10.0 - Radius + mPosition.getX() - 4 ,  10.0 + Radius + mPosition.getY() - 4,0);
			v2[2] = new Vertex();
			v2[2]->setVertexd(10.0 + Radius + mPosition.getX() - 4, 10.0 + Radius + mPosition.getY() - 4,0);
			v2[3] = new Vertex();
			v2[3]->setVertexd( 10.0 + Radius + mPosition.getX() - 4, -10.0 - Radius + mPosition.getY() - 4,0);
			// rounded rectangle - accidental.
			/*
			for(unsigned int x = 0; x < Vertices.size(); x++) {
				Vertex* v = Vertices[x];
				vertX = v->getX();
				vertY = v->getY();
				v2[0] = new Vertex();
				v2[0]->setVertexd(-0.5 - mPosition.getX() - vertX, -0.5 - mPosition.getY() - vertY,0 );
				v2[1] = new Vertex();
				v2[1]->setVertexd(-0.5 - mPosition.getX() - vertX, 0.5 + mPosition.getY() + vertY,0);
				v2[2] = new Vertex();
				v2[2]->setVertexd(0.5 + mPosition.getX() + vertX, 0.5 + mPosition.getY() + vertY, 0);
				v2[3] = new Vertex();
				v2[3]->setVertexd(0.5 + mPosition.getX() + vertX, -0.5 - mPosition.getY() - vertY,0);

			}*/ 

		glEnd();

 

 



Sponsor:

#2 Waterlimon   Crossbones+   -  Reputation: 2458

Like
1Likes
Like

Posted 30 March 2013 - 09:57 AM

If you mean a 2D bounding box regardless of whether its 2D or 3D, you need to:

1.Transform all the points to 2D coordinates (using all the matrices, projection, model, etc. you pass to opengl) (you might want to find a way to ignore points you know not to affect the bounding box, like stuff inside the object, or make a simplified representation of it...)

(if its top down and practically 2D however, you can probably just ignore the depth and treat them like 2D points)

2.Find the maximum X coord you can find, max y, min X, min Y

3.You have an axis aligned bounding box


Edited by Waterlimon, 30 March 2013 - 09:58 AM.

o3o


#3 Enerjak   Members   -  Reputation: 232

Like
0Likes
Like

Posted 30 March 2013 - 09:55 PM

OK, going back and working through it and with a lot of help from a friend of mine I've come up with this: 

 

http://puu.sh/2r2ye

 

if you can't tell in the image, I am rendering a bounding box but.........looks more like a sphere then a box which i think is due to the fact that the shape's vertices are in a circular formation, I could be wrong, anyways, it's progress at least. here's my revised function to render a bounding box.

 

double minX = FLT_MAX;
		double minY = FLT_MAX;
		double maxX =-FLT_MAX;
		double maxY =-FLT_MAX;

		// try to draw the aabb
		glBegin(GL_POLYGON);
			glColor4d(0.55,0.8,1.0,0.5);
			for(int i = 0; i < Vertices.size(); i++) {
				Vertex* v = Vertices[i];
				
				// check minX x < minX;
				if(v->getX() < minX)
				{
					minX = v->getX();
				}

				// check minX x > minX;
				if(v->getX() > minX)
				{
					minX = v->getX();
				}

				// check y < miny
				if(v->getY() < minY)
				{
					minY = v->getY();
				}

				// check y > miny
				if(v->getY() > minY)
				{
					minY = v->getY();
				}

				// check x < maxX
				if(v->getX() < maxX)
				{
					maxX = v->getX();
				}

				// check x > maxX
				if(v->getX() > maxX)
				{
					maxX = v->getX();
				}


				// check y < maxY
				if(v->getY() < maxY)
				{
					maxY = v->getY();
				}

				// check y > maxY
				if(v->getY() > maxY)
				{
					maxY = v->getY();
				}

				glVertex2d(maxX + mPosition.getX(),maxY + mPosition.getY());
				glVertex2d(minX + mPosition.getX(),minY + mPosition.getY());
	
			}
		glEnd();

 

Not sure what else to do to get it right...........






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS