Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ShockBoy99

Quake 2 BSP Collision detection

This topic is 6030 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve been working on an algorithm to find the distance to a wall in any direction, mostly to check how close to the floor a person is.
  
// DO NOT USE!!! BUGGY

/////////////////////////////////////////////////////////////////////////////////////

float cBspTree::cNode::DistanceToWall (cVector point1, cVector point2)
/////////////////////////////////////////////////////////////////////////////////////

{
	ePointLoc	p1, p2;
	cVector		split;
	float		distance = 0;

	// Test the positioning of each point

	p1 = m_plane.TestPoint (point1);
	p2 = m_plane.TestPoint (point2);

	// Test if both points are in front or coplanar

	if (((p1 == PT_FRONT) || (p1 == PT_COPLANAR)) && 
		((p2 == PT_FRONT) || (p2 == PT_COPLANAR)))
	{
		// If the front node is a leaf, return the distance

		if (m_frontNode == NULL)
		{
			// Is the leaf solid?

			if (m_frontLeaf->GetCluster () == BSP_OUTOFCLUSTER)
				return 0;
			else
				return ((point1 - point2).GetLength ());
			
		}
		// Front node is a node

		return m_frontNode->DistanceToWall (point1, point2);
	}

	// Test if both points are in back

	if (((p1 == PT_BACK) || (p1 == PT_COPLANAR)) && 
		((p2 == PT_BACK) || (p2 == PT_COPLANAR)))
	{
		// If the back node is a leaf, return the distance

		if (m_backNode == NULL)
		{
			// Is the leaf solid?

			if (m_backLeaf->GetCluster () == BSP_OUTOFCLUSTER)
				return 0;
			else
				return ((point1 - point2).GetLength ());
			
		}
		// Back node is a node

		return m_backNode->DistanceToWall (point1, point2);
	}

	// Points split across the plane


	split = m_plane.GetIntersection (point1, point2);

	// First point is in front

	if (((p1 == PT_FRONT) || (p1 == PT_COPLANAR)) && 
		((p2 == PT_BACK)  || (p2 == PT_COPLANAR)))
	{
		// If the front node is a leaf, get the distance

		if (m_frontNode == NULL)
		{
			// Is the leaf solid?

			if (m_frontLeaf->GetCluster () == BSP_OUTOFCLUSTER)
				return 0;
			else
				distance += (point1 - split).GetLength ();
		}
		else
			distance += m_frontNode->DistanceToWall (point1, split);

		if (distance == 0.f)
			return 0.f;

		// If the back node is a leaf, get the distance

		if (m_backNode == NULL)
		{
			// Is the leaf not solid?

			if (m_backLeaf->GetCluster () != BSP_OUTOFCLUSTER)
				distance += (point2 - split).GetLength();
		}
		else
			distance += m_backNode->DistanceToWall (split, point2);
		
		if (distance == 0.f)
			return 0.f;
	}

	// First point is in back

	if (((p1 == PT_BACK)   || (p1 == PT_COPLANAR)) && 
		((p2 == PT_FRONT)  || (p2 == PT_COPLANAR)))
	{
		// If the back node is a leaf, get the distance

		if (m_backNode == NULL)
		{
			// Is the leaf solid?

			if (m_backLeaf->GetCluster () == BSP_OUTOFCLUSTER)
				return 0;
			else
				distance += (point1 - split).GetLength ();
		}
		else
			distance += m_backNode->DistanceToWall (point1, split);

		if (distance == 0.f)
			return 0.f;

		// If the front node is a leaf, get the distance

		if (m_frontNode == NULL)
		{
			// Is the leaf not solid?

			if (m_frontLeaf->GetCluster () != BSP_OUTOFCLUSTER)
				distance += (point2 - split).GetLength();
		}
		else
			distance += m_frontNode->DistanceToWall (split, point2);
	
		if (distance == 0.f)
			return 0.f;
	}

	// Return the distance

	return distance;
}
  
On solid ground, it works great, but on overhangs, it seems to be getting the distance to the bottom most floor. I''m sure it has something to do with the recursion, but I can''t seem to find out what it is. --------------------------------------------------------------------------------------- Before I couldn''t spell engineer, now I are one.

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!