コード例 #1
0
ファイル: collision.cpp プロジェクト: YagiChanGames/CodeBox
 void action(BoundingFrustum const& f,Fn fn)const{
     for(auto const& node:nodes){
         if(f.Contains(node.bounds)!=DISJOINT)
             node.action(f,fn);
     }
     for(auto const& m:objects){
         if(f.Contains(m.bounds)!=DISJOINT)
             fn(m);
     }
 }
コード例 #2
0
ファイル: QuadTree.cpp プロジェクト: yorkhawke/Projekt3D_Per
void QuadTree::Render(ID3D11DeviceContext* DeviceContext, const XMMATRIX &projection, const XMMATRIX &view)
{
	//Frust på Projection sen multiplicera med inverse proj inverse view iverse world.


	BoundingFrustum frust;//göra om frustumet till worldspace för boxarna...

	frust.CreateFromMatrix(frust, projection);
	frust.Transform(frust, XMMatrixInverse(nullptr, view));//FUNKAR TESTAT!

	//if (frust.Intersects(box))
	//{
	//		if (!leaf)
	//		{
	//			for (int i = 0; i < 4; i++)
	//			{
	//				Children[i].Render(DeviceContext, projection,view,World);
	//			}
	//		}
	//		else
	//		{
	//			UINT32 vertexSize = sizeof(Vertex);
	//			UINT32 offset = 0;

	//			DeviceContext->IASetIndexBuffer(IndexB, DXGI_FORMAT_R32_UINT, 0);

	//			DeviceContext->IASetVertexBuffers(0, 1, &VertexB, &vertexSize, &offset);
	//			DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
	//			DeviceContext->DrawIndexed(nrIndices, 0, 0);
	//		}

	int testValue = frust.Contains(box);
	switch (testValue)
	{
	case 0:
		break;
	case 1:
		if (!leaf)
		{
			for (int i = 0; i < 4; i++)
			{
				Children[i].Render(DeviceContext, projection,view);
			}
		}
		else
		{
			UINT32 vertexSize = sizeof(Vertex);
			UINT32 offset = 0;

			DeviceContext->IASetIndexBuffer(IndexB, DXGI_FORMAT_R32_UINT, 0);

			DeviceContext->IASetVertexBuffers(0, 1, &VertexB, &vertexSize, &offset);
			DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
			DeviceContext->DrawIndexed(nrIndices, 0, 0);
		}
		break;
	default:
		if (!leaf)
		{
			for (int i = 0; i < 4; i++)
			{
				Children[i].Render(DeviceContext, projection, view);
			}
		}
		else
		{
			UINT32 vertexSize = sizeof(Vertex);
			UINT32 offset = 0;

			DeviceContext->IASetIndexBuffer(IndexB, DXGI_FORMAT_R32_UINT, 0);

			DeviceContext->IASetVertexBuffers(0, 1, &VertexB, &vertexSize, &offset);
			DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
			DeviceContext->DrawIndexed(nrIndices, 0, 0);
		}
		break;
	}
}
コード例 #3
0
//--------------------------------------------------------------------------------------
// Test collisions between pairs of collision objects using XNACollision functions
//--------------------------------------------------------------------------------------
void Collide()
{
    // test collisions between objects and frustum
    g_SecondarySpheres[0].collision = g_PrimaryFrustum.Contains( g_SecondarySpheres[0].sphere );
    g_SecondaryOrientedBoxes[0].collision = g_PrimaryFrustum.Contains( g_SecondaryOrientedBoxes[0].obox );
    g_SecondaryAABoxes[0].collision = g_PrimaryFrustum.Contains( g_SecondaryAABoxes[0].aabox );
    g_SecondaryTriangles[0].collision = g_PrimaryFrustum.Contains( g_SecondaryTriangles[0].pointa,
                                                                   g_SecondaryTriangles[0].pointb,
                                                                   g_SecondaryTriangles[0].pointc );

    // test collisions between objects and aligned box
    g_SecondarySpheres[1].collision = g_PrimaryAABox.Contains( g_SecondarySpheres[1].sphere );
    g_SecondaryOrientedBoxes[1].collision = g_PrimaryAABox.Contains( g_SecondaryOrientedBoxes[1].obox );
    g_SecondaryAABoxes[1].collision = g_PrimaryAABox.Contains( g_SecondaryAABoxes[1].aabox );
    g_SecondaryTriangles[1].collision = g_PrimaryAABox.Contains( g_SecondaryTriangles[1].pointa,
                                                                 g_SecondaryTriangles[1].pointb,
                                                                 g_SecondaryTriangles[1].pointc );

    // test collisions between objects and oriented box
    g_SecondarySpheres[2].collision = g_PrimaryOrientedBox.Contains( g_SecondarySpheres[2].sphere );
    g_SecondaryOrientedBoxes[2].collision = g_PrimaryOrientedBox.Contains( g_SecondaryOrientedBoxes[2].obox );
    g_SecondaryAABoxes[2].collision = g_PrimaryOrientedBox.Contains( g_SecondaryAABoxes[2].aabox );
    g_SecondaryTriangles[2].collision = g_PrimaryOrientedBox.Contains( g_SecondaryTriangles[2].pointa,
                                                                       g_SecondaryTriangles[2].pointb,
                                                                       g_SecondaryTriangles[2].pointc );

    // test collisions between objects and ray
    float fDistance = -1.0f;

    float fDist;
    if ( g_SecondarySpheres[3].sphere.Intersects( g_PrimaryRay.origin, g_PrimaryRay.direction, fDist ) )
    {
        fDistance = fDist;
        g_SecondarySpheres[3].collision = INTERSECTS;
    }
    else
        g_SecondarySpheres[3].collision = DISJOINT;

    if ( g_SecondaryOrientedBoxes[3].obox.Intersects( g_PrimaryRay.origin, g_PrimaryRay.direction, fDist ) )
    {
        fDistance = fDist;
        g_SecondaryOrientedBoxes[3].collision = INTERSECTS;
    }
    else
        g_SecondaryOrientedBoxes[3].collision = DISJOINT;

    if ( g_SecondaryAABoxes[3].aabox.Intersects( g_PrimaryRay.origin, g_PrimaryRay.direction, fDist ) )
    {
        fDistance = fDist;
        g_SecondaryAABoxes[3].collision =  INTERSECTS;
    }
    else
        g_SecondaryAABoxes[3].collision =  DISJOINT;

    if ( TriangleTests::Intersects( g_PrimaryRay.origin, g_PrimaryRay.direction,
                                    g_SecondaryTriangles[3].pointa,
                                    g_SecondaryTriangles[3].pointb,
                                    g_SecondaryTriangles[3].pointc,
                                    fDist ) )
    {
        fDistance = fDist;
        g_SecondaryTriangles[3].collision = INTERSECTS;
    }
    else
        g_SecondaryTriangles[3].collision = DISJOINT;

    // If one of the ray intersection tests was successful, fDistance will be positive.
    // If so, compute the intersection location and store it in g_RayHitResultBox.
    if( fDistance > 0 )
    {
        // The primary ray's direction is assumed to be normalized.
        XMVECTOR HitLocation = XMVectorMultiplyAdd( g_PrimaryRay.direction, XMVectorReplicate( fDistance ),
                                                    g_PrimaryRay.origin );
        XMStoreFloat3( &g_RayHitResultBox.aabox.Center, HitLocation );
        g_RayHitResultBox.collision = INTERSECTS;
    }
    else
    {
        g_RayHitResultBox.collision = DISJOINT;
    }
}