I have been working on a simple first person demo for moving arouns a room containing a pillar, this basic room can be easily extended to create larger spaces, etc, however, my problem at the moment is that i cannot get the collisions to work.
my algorithm is as follows:
void MeshObject::collision(D3DXVECTOR3 &p, BoundingBox &box, D3DXVECTOR3 &b)
{
// p = position of mesh
// box = bounding box of mesh being collided with
// b = position of mesh being collided with
if(p.x >= box._min.x + b.x && p.x <= box._max.x + b.x && p.z >= box._min.z + b.z && p.z <= box._max.z + b.z)
{
CollisionWith = 1;
}
else CollisionWith = 0;
}
this is then called simply like so:
PLAYER.collision(PLAYER.Position, Pillar.BOX, Pillar.Position);
the movement is then called like so:
if(PLAYER.CollisionWith == 0)
{
if (GetAsyncKeyState(VK_LEFT) & 0x8000)
{
}
if (GetAsyncKeyState(VK_RIGHT) & 0x8000)
{
}
if (GetAsyncKeyState(VK_UP) )
{
PLAYER.Velocity.x = sin(PLAYER.THETA);
PLAYER.Velocity.z = cos(PLAYER.THETA);
PLAYER.IsForceBeingApplied = 1;
}
else if (GetAsyncKeyState(VK_DOWN) )
{
PLAYER.Velocity.x = -sin(PLAYER.THETA);
PLAYER.Velocity.z = -cos(PLAYER.THETA);
PLAYER.IsForceBeingApplied = 1;
}
else {
PLAYER.IsForceBeingApplied = 0;
}
the trouble is, that the player can pass right through the objects still, despite the if statement checking weather the player is colliding with anything. the collisionWith variable IS being set to one at some point, as if i remove the "else" statement resetting it to zero once the player is no longer colliding with anything, then the player will be unable to move at all (as the initial setup has the player moved from the centre of the room - where the pillar is).
i have a debug string set up to allow me to monitor the variables as they go, and this is what i see:
PLAYER x = -0.000000
PLAYER z = -10.000000
Pillar min z = -11.666668
Pillar min x = -11.666668
Pillar max x = 11.666664
clearly, the player has moved 1.666668 into the pillar's bounding box, yet there is no collision detected, despite the algorithm stating that there SHOULD be one detected. (the player can move further than 1.666668 into the mesh, i just took an example position close to the edge of the box as this would be close to the point at which the CollisionWith variable should have changed.
can anyone see what's wrong with this and why it's not detecting these collisions?
Thanks in advance,
Mal'