Exemplo n.º 1
0
bool ActorLoader::LoadCollisionGeo(const char* str, size_t n)
{
	bool loadOkay = true;
	std::stringstream ss = strm::FromString(str, n);
	std::string path;
	std::string type;
	int32_t subset;

	ss >> strm::Ignore(':');
	ss >> path;
	ss >> subset;
	ss >> type;
	if (!ss.fail())
	{
		CollisionGeometry* newGeo = new CollisionGeometry();
		newGeo->m_resource = path;
		newGeo->m_subset = subset;
		newGeo->m_owner = m_actor;

		CollisionType ctype = GetCollisionType(type);

		if (ctype == CT_Ground)
			m_actor->m_collision->m_ground.push_back(newGeo);
		else if (ctype == CT_Solid)
			m_actor->m_collision->m_walls.push_back(newGeo);
		else
			delete newGeo;
	}

	return !ss.fail();
}
Exemplo n.º 2
0
    void StaticMeshActorProxy::LoadFile(const std::string &fileName)
    {
        dtCore::Object *obj = static_cast<dtCore::Object*>(GetActor());

        //First load the mesh (with cacheing on).
        if (obj->LoadFile(fileName,true) == NULL)
        {
           if (!fileName.empty())
              LOG_ERROR("Error loading mesh file: " + fileName);
            return;
        }

        //We need this little hack to ensure that when a mesh is loaded, the collision
        //properties get updated properly.
        SetCollisionType(GetCollisionType());

        //std::cout << "Writing node file." << std::endl;
        //osgDB::writeNodeFile(*obj->GetOSGNode(),"testtextures.osg");
        //std::cout << "Done writing node file." << std::endl;

        //Next, run our visitor over the loaded mesh and extract the texture slots.
//         const std::string texGroupName = "Textures";
//         const std::string texPropBaseName = "Channel";
//         ExtractTexturesVisitor tv;
//         obj->GetOSGNode()->accept(tv);
//
//         //Now dynamically add each texture attribute as a resource property.
//         ExtractTexturesVisitor::TextureList &texList = tv.GetTextureList();
//         ExtractTexturesVisitor::TextureList::iterator texItor;
//
//         std::ostringstream ss;
//         std::cout << "NumTextures: " << texList.size() << std::endl;
//         std::cout << "NumTexCoords: " << tv.GetMaxTexCoordCount() << std::endl;

//         for (texItor=texList.begin(); texItor!=texList.end(); ++texItor) {
//             ss.clear();
//             ss.str("");
//             ss << texPropBaseName << count++;
//
//             //First see if we already have a resource
//
//             TextureEntry *texEntry = new TextureEntry(const_cast<osg::Texture2D *>(texItor->get()));
//             this->mTextureSlots.push_back(texEntry);
//             ss << texPropBaseName << count++;
//             AddProperty(new dtDAL::ResourceActorProperty(*this,dtDAL::DataType::TEXTURE,
//                 ss.str(),"Texture",dtDAL::MakeFunctor(*texEntry,&TextureEntry::LoadFile),
//                 "A texture slot in the static mesh.",texGroupName));
//         }
    }
Exemplo n.º 3
0
bool DynamicObject::CheckCollision(GameObject *other)
{
	#pragma region BB and BB
	if(GetCollisionType() == BB && other->GetCollisionType() == BB)
	{
		float otherX = other->GetX();
		float otherY = other->GetY();
		int otherBoundUp = other->GetBoundUp();
		int otherBoundDown = other->GetBoundDown();
		int otherBoundLeft = other->GetBoundLeft();
		int otherBoundRight = other->GetBoundRight();
		
		if(x + boundRight > otherX - otherBoundLeft &&
		   x - boundLeft < otherX + otherBoundRight &&
		   y + boundDown > otherY - otherBoundUp &&
		   y - boundUp < otherY + otherBoundDown)
		   return true;
		else
			return false;
	}
	#pragma endregion
	#pragma region BB and TBB
	else if(GetCollisionType() == BB && other->GetCollisionType() == TBB)
	{
		
		int xPoint1 = other->GetXPoint1();
		int yPoint1 = other->GetYPoint1();
		int xPoint2 = other->GetXPoint2();
		int yPoint2 = other->GetYPoint2();
		int xPoint3 = other->GetXPoint3();
		int yPoint3 = other->GetYPoint3();

		//1) Check if any of the triangle’s points are within the rectangle, if yes then intersection is true.
		if((xPoint1 > x-boundLeft && xPoint1 < x + boundRight && yPoint1 > y - boundUp && yPoint1 < y + boundDown)
			|| (xPoint2 > x-boundLeft && xPoint2 < x + boundRight && yPoint2 > y - boundUp && yPoint2 < y + boundDown)
			|| (xPoint3 > x-boundLeft && xPoint3 < x + boundRight && yPoint3 > y - boundUp && yPoint3 < y + boundDown))
		{
			return true;
		}

		//**************************************************************************************************************************************************************\\

		//2) Check if any of the corners of the rectangle is in the triangle
		/*
		als een van de hoeken van een vierkant in de driehoek is, is er collision
		omdat de lijnen elkaar niet raken, hoeven we alleen maar naar het centrum van het vierkant te kijken. (de x en y)

					 B
				    / \
				   /   \
				  /     \
				 /   P   \      P'
				/         \
			  A ----------- C 
		*/
			
		//Bereken totale oppervlakte van driehoek (ABC)
		
		float Px, Py;

		Px = x-boundLeft;
		Py = y-boundUp;
		
		if(Is_p_in_triangle(xPoint1, yPoint1, xPoint2, yPoint2, xPoint3, yPoint3, Px, Py))
			return true;
		
		Px = x+boundRight;

		if(Is_p_in_triangle(xPoint1, yPoint1, xPoint2, yPoint2, xPoint3, yPoint3, Px, Py))
			return true;

		Py = y+boundDown;

		if(Is_p_in_triangle(xPoint1, yPoint1, xPoint2, yPoint2, xPoint3, yPoint3, Px, Py))
			return true;

		Px = x-boundLeft;

		if(Is_p_in_triangle(xPoint1, yPoint1, xPoint2, yPoint2, xPoint3, yPoint3, Px, Py))
			return true;

		//**************************************************************************************************************************************************************\\
		//This has been commented out, because it is for very precise collision checking, but it's not neccessary and it would only slow things down
		//3) Check if any of the triangle’s lines intersect any of the rectangle’s lines, if yes then intersection is true. (Not necessary with current objects)
		/*
		float a = 0;
		float b = 0;
		//line 1
		//y = ax+b
		if(xPoint1 - xPoint2 != 0)
		{
			a = ((yPoint1 - yPoint2)/(xPoint1 - xPoint2));
			b = (yPoint1 - (a*xPoint1));
			if(xPoint1 <= xPoint2)
			{
				for(int i = xPoint1; i<xPoint2; i++)
				{
					if(i >= x - boundLeft && i <= x + boundRight && (a*i+b) >= y - boundUp && (a*i+b) <= y + boundDown)
						return true;
				}
			}
			else if(xPoint1 > xPoint2)
			{
				for(int i = xPoint1; i>xPoint2; i--)
				{
					if(i >= x - boundLeft && i <= x + boundRight && (a*i+b) >= y - boundUp && (a*i+b) <= y + boundDown)
						return true;
				}
			}
		}

		//line 2
		//y = ax+b
		if(xPoint2 - xPoint3 != 0)
		{
			a = ((yPoint2 - yPoint3)/(xPoint2 - xPoint3));
			b = (yPoint2 - (a*xPoint2));
			if(xPoint2 <= xPoint3)
			{
				for(int i = xPoint2; i<xPoint3; i++)
				{
					if(i >= x - boundLeft && i <= x + boundRight && (a*i+b) >= y - boundUp && (a*i+b) <= y + boundDown)
						return true;
				}
			}
			else if(xPoint2 > xPoint3)
			{
				for(int i = xPoint2; i>xPoint3; i--)
				{
					if(i >= x - boundLeft && i <= x + boundRight && (a*i+b) >= y - boundUp && (a*i+b) <= y + boundDown)
						return true;
				}
			}
		}
		//line 3
		//y = ax+b
		if(xPoint3-xPoint1!=0)
			a = ((yPoint3 - yPoint1)/(xPoint3 - xPoint1));
			b = (yPoint3 - (a*xPoint3));
			if(xPoint3 <= xPoint1)
			{
				for(int i = xPoint3; i<xPoint1; i++)
				{
					if(i >= x - boundLeft && i <= x + boundRight && (a*i+b) >= y - boundUp && (a*i+b) <= y + boundDown)
						return true;
				}
			}
			else if(xPoint3 > xPoint1)
			{
				for(int i = xPoint3; i>xPoint1; i--)
				{
					if(i >= x - boundLeft && i <= x + boundRight && (a*i+b) >= y - boundUp && (a*i+b) <= y + boundDown)
						return true;
				}
			}
			*/
		return false;
	}
	#pragma endregion
	#pragma region TBB and BB
	else if(GetCollisionType() == TBB && other->GetCollisionType() == BB)
	{
		
		int boundUp = other->GetBoundUp();
		int boundDown = other->GetBoundDown();
		int boundLeft = other->GetBoundLeft();
		int boundRight = other->GetBoundRight();

		float x = other->GetX();
		float y = other->GetY();

		//1) Check if any of the triangle’s points are within the rectangle, if yes then intersection is true.
		if((xPoint1 > x-boundLeft && xPoint1 < x + boundRight && yPoint1 > y - boundUp && yPoint1 < y + boundDown)
			|| (xPoint2 > x-boundLeft && xPoint2 < x + boundRight && yPoint2 > y - boundUp && yPoint2 < y + boundDown)
			|| (xPoint3 > x-boundLeft && xPoint3 < x + boundRight && yPoint3 > y - boundUp && yPoint3 < y + boundDown))
		{
			return true;
		}

		//**************************************************************************************************************************************************************\\

		//2) Check if any of the corners of the rectangle is in the triangle
		/*
		als een van de hoeken van een vierkant in de driehoek is, is er collision
		omdat de lijnen elkaar niet raken, hoeven we alleen maar naar het centrum van het vierkant te kijken. (de x en y)

					 B
				    / \
				   /   \
				  /     \
				 /   P   \      P'
				/         \
			  A ----------- C 
		*/
			
		//Bereken totale oppervlakte van driehoek (ABC)
		
		float Px, Py;

		Px = x-boundLeft;
		Py = y-boundUp;
		
		if(Is_p_in_triangle(xPoint1, yPoint1, xPoint2, yPoint2, xPoint3, yPoint3, Px, Py))
			return true;
		
		Px = x+boundRight;

		if(Is_p_in_triangle(xPoint1, yPoint1, xPoint2, yPoint2, xPoint3, yPoint3, Px, Py))
			return true;

		Py = y+boundDown;

		if(Is_p_in_triangle(xPoint1, yPoint1, xPoint2, yPoint2, xPoint3, yPoint3, Px, Py))
			return true;

		Px = x-boundLeft;

		if(Is_p_in_triangle(xPoint1, yPoint1, xPoint2, yPoint2, xPoint3, yPoint3, Px, Py))
			return true;
	}
	#pragma endregion
	#pragma region BB and CBB
	else if(GetCollisionType() == BB && other->GetCollisionType() == CBB)
	{
		//Check for 8 points in de bounding box (the * on the box)
		/*
			*----*----*
			|         |
			*         *
			|         |
			*----*----*
		*/
		if(Is_p_in_circle(other->GetX(), other->GetY(),other->GetRadius(),x-boundLeft,y-boundUp))
			return true;
		else if(Is_p_in_circle(other->GetX(), other->GetY(),other->GetRadius(),x,y-boundUp))
			return true;
		else if(Is_p_in_circle(other->GetX(), other->GetY(),other->GetRadius(),x+boundRight,y-boundUp))
			return true;
		else if(Is_p_in_circle(other->GetX(), other->GetY(),other->GetRadius(),x-boundLeft,y))
			return true;
		else if(Is_p_in_circle(other->GetX(), other->GetY(),other->GetRadius(),x+boundRight,y))
			return true;
		else if(Is_p_in_circle(other->GetX(), other->GetY(),other->GetRadius(),x-boundLeft,y+boundDown))
			return true;
		else if(Is_p_in_circle(other->GetX(), other->GetY(),other->GetRadius(),x,y+boundDown))
			return true;
		else if(Is_p_in_circle(other->GetX(), other->GetY(),other->GetRadius(),x+boundRight,y+boundDown))
			return true;
		else
			false;
	}
	#pragma endregion
	#pragma region CBB and BB
	else if(GetCollisionType() == CBB && other->GetCollisionType() == BB)
	{
		//Check for 8 points in de bounding box (the * on the box)
		/*
			*----*----*
			|         |
			*         *
			|         |
			*----*----*
		*/
		if(Is_p_in_circle(x, y,radius, other->GetX()-other->GetBoundLeft(), other->GetY()-other->GetBoundUp()))
			return true;
		else if(Is_p_in_circle(x, y,radius, other->GetX(), other->GetY() - other->GetBoundUp()))
			return true;
		else if(Is_p_in_circle(x, y,radius, other->GetX()+other->GetBoundRight(), other->GetY()-other->GetBoundUp()))
			return true;
		else if(Is_p_in_circle(x, y,radius, other->GetX()-other->GetBoundLeft(), other->GetY()))
			return true;
		else if(Is_p_in_circle(x, y,radius, other->GetX()+other->GetBoundRight(), other->GetY()))
			return true;
		else if(Is_p_in_circle(x, y,radius, other->GetX()-other->GetBoundLeft(), other->GetY()+other->GetBoundDown()))
			return true;
		else if(Is_p_in_circle(x, y,radius, other->GetX(), other->GetY()+other->GetBoundDown()))
			return true;
		else if(Is_p_in_circle(x, y,radius, other->GetX()+other->GetBoundDown(), other->GetY()+other->GetBoundDown()))
			return true;
		else
			return false;
	}
	#pragma endregion
	return false;
}