PDA

View Full Version : Collision bug - unit problem?



NFMynster
02-21-2017, 03:33 PM
Hello!
I'm having a problem right now with the collision tutorial I'm doing, as it seems many of these tutorials set the sizes of their objects. I think the problem is within how I'm checking the size of the collision area, as the units (probably) aren't in pixels.

Youtube video of current result. (https://youtu.be/ZjwestWaU8s)

This is the code for drawing the red rectagle around the object, which seems to be working perfectly as the rectangle is drawn around the sprite image.

void Collider::Draw()
{
#if _DEBUG
glPushMatrix();

glTranslatef(GetGameObject()->Transform->Position->X, GetGameObject()->Transform->Position->Y, GetGameObject()->Transform->Position->Z);
glScalef(scale->X, scale->Y, scale->Z);
glDisable(GL_TEXTURE_2D);
glLineWidth(1);
glBegin(GL_LINE_LOOP);
glColor3f(color.X, color.Y, color.Z);
glVertex2d(-0.5, -(SpriteHeight / SpriteWidth) / 2);
glVertex2d(0.5, -(SpriteHeight / SpriteWidth) / 2);
glVertex2d(0.5, (SpriteHeight / SpriteWidth) / 2);
glVertex2d(-0.5, (SpriteHeight / SpriteWidth) / 2);
glColor3f(1.0f, 1.0f, 1.0f);
glEnd();
glEnable(GL_TEXTURE_2D);

glPopMatrix();
#endif
}
This is the actual collision code. The list of colliders is just because collider is an component, and by putting all those colliders components in a vector list, I can compare them.

GameManager *gm = GameManager::getInstance();
if (doCollisionChecks)
{
for (std::vector<Collider*>::iterator it = gm->Colliders->begin(); it != gm->Colliders->end(); ++it)
{
if ((*it) != this)
{
GameObject *other = (*it)->GetGameObject();
//X-Axis
if (GetGameObject()->Transform->Position->X + (SpriteWidth/100) >= other->Transform->Position->X &&
other->Transform->Position->X + ((*it)->SpriteWidth/100) >= GetGameObject()->Transform->Position->X)
{
//Y-Axis
if (GetGameObject()->Transform->Position->Y + (SpriteHeight/100) >= other->Transform->Position->Y &&
other->Transform->Position->Y + ((*it)->SpriteHeight/100) >= GetGameObject()->Transform->Position->Y)
{
//COLLISION
printf("Collision ");

}

}

}
}
}
The "printf(Collision)" is where the line turns blue and the collision check work, however, it's not working as it should.
I'm dividing the sprite width with 100, because the actual pixel size of the image is 320x640 and therefor the collision would be way too big if I did not. Or what?
Any suggestions?

Dark Photon
02-22-2017, 06:25 AM
This doesn't appear to be an OpenGL question. Do you have any specific questions related to OpenGL?