I hope someone out there can answer my question. Currently I am doing some collision detection using D3DXVectors. I have the collision down, but what I need is what to do AFTER the collision has been made.
I have a player object and the object the player is being collided with and pass these into my function.
Basically, I have this so far (variables names have been changed for clarity):
D3DXVECTOR3 Collision::wallCol(D3DXVECTOR3 player, D3DXVECTOR3 object, float playerWidth, float playerDepth, float objectWidth, float objectDepth)
{
//store the values of the objects into temps
//left hand side of a box
float minX = object.x - (objectWidth / 2);
//right hand side of a box
float maxX = object.x + (objectWidth / 2);
//front of a box
float minZ = object.z - (objectDepth / 2);
//back of a box
float maxZ = object.z + (objectDepth / 2);
//store the players positions into temps
float pminX = player.x - (playerWidth / 2);
float pmaxX = player.x + (playerWidth / 2);
float pminZ = player.z - (playerDepth / 2);
float pmaxZ = player.z + (playerDepth / 2);
//check to see if we are in the box at all
if (pminX <= maxX && pmaxX >= minX &&
pminZ <= maxZ && pmaxZ >= minZ)
{
//x collision for the left side of the block
if (pmaxX >= minX && pmaxX <= maxX)
{
pmaxX = minX - 50;
player.x = pmaxX;
return player;
}
//x collision for the right side of the block
if (pminX <= maxX && pminX >= minX)
{
pminX = maxX + 50;
player.x = pminX;
return player;
}
//z collision for the front
if (pmaxZ >= minZ && pmaxZ <= maxZ)
{
pmaxZ = minZ - 20;
player.z = pmaxZ;
return player;
}
//z collision for the back
if (pminZ <= maxZ && pminZ >= minZ)
{
pminZ = maxZ + 20;
player.z = pminZ;
return player;
}
}
return player;
}
As you can see, I would like to move my player object away from whatever it is that it is bumping into. The problem I am having is that no matter what, my player will always move either to the left or the right on the X axis because it accounts for the X first, it never has the chance to even get to the Z if statements.
I've considered making 2 Booleans to tell if the player makes contact with the X or the Z first, but since the first if statement is to even see if we are in the box, I believe it would trigger them both at the same time and there would be no way to tell which came first.
If anyone could offer some advice on this problem, it would be greatly appreciated! Thank you.