collisions not being detected

Started by
1 comment, last by Malazar 16 years, 6 months ago
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'
Advertisement
The most suspicious part of the code would be the pillar's position. According to your debug print-out, the pillar's x-z position should be (0, 0). I would first verify that assumption at run-time, by printing the position out or viewing it in the debugger.

Also, are you only checking collisions against a single pillar? It appears that your collision method sets CollisionWith to false if no collision is found, meaning that if multiple collision tests are performed, and the last test happens to evaluate to false, it will be as if no collisions were detected at all.

It might also be helpful to see the collision response code, if any. i.e. how are you determining whether or not you found a collision at run-time?

Quote:Original post by extralongpants
.

Also, are you only checking collisions against a single pillar? It appears that your collision method sets CollisionWith to false if no collision is found, meaning that if multiple collision tests are performed, and the last test happens to evaluate to false, it will be as if no collisions were detected at all.


*facepalm*

yeah, that'd do it.

sometimes it takes a fresh set of eyes to see the obvious mistakes. thanks :D

This topic is closed to new replies.

Advertisement