Пример #1
0
        public static void RayTestSingle(ref IndexedMatrix rayFromTrans, ref IndexedMatrix rayToTrans,
                          CollisionObject collisionObject,
                          CollisionShape collisionShape,
                          ref IndexedMatrix colObjWorldTransform,
                          RayResultCallback resultCallback)
        {
            SphereShape pointShape = BulletGlobals.SphereShapePool.Get();
            pointShape.Initialize(0.0f);
            pointShape.SetMargin(0f);
            ConvexShape castShape = pointShape;

            if (collisionShape.IsConvex())
            {
                BulletGlobals.StartProfile("rayTestConvex");
                CastResult castResult = BulletGlobals.CastResultPool.Get();
                castResult.m_fraction = resultCallback.m_closestHitFraction;

                ConvexShape convexShape = collisionShape as ConvexShape;
                VoronoiSimplexSolver simplexSolver = BulletGlobals.VoronoiSimplexSolverPool.Get();
                //#define USE_SUBSIMPLEX_CONVEX_CAST 1
                //#ifdef USE_SUBSIMPLEX_CONVEX_CAST

                // FIXME - MAN - convexcat here seems to make big difference to forklift.

                SubSimplexConvexCast convexCaster = BulletGlobals.SubSimplexConvexCastPool.Get();
                convexCaster.Initialize(castShape, convexShape, simplexSolver);

                //GjkConvexCast convexCaster = new GjkConvexCast(castShape, convexShape, simplexSolver);


                //#else
                //btGjkConvexCast	convexCaster(castShape,convexShape,&simplexSolver);
                //btContinuousConvexCollision convexCaster(castShape,convexShape,&simplexSolver,0);
                //#endif //#USE_SUBSIMPLEX_CONVEX_CAST

                if (convexCaster.CalcTimeOfImpact(ref rayFromTrans, ref rayToTrans, ref colObjWorldTransform, ref colObjWorldTransform, castResult))
                {
                    //add hit
                    if (castResult.m_normal.LengthSquared() > 0.0001f)
                    {
                        if (castResult.m_fraction < resultCallback.m_closestHitFraction)
                        {

                            //if (resultCallback.m_closestHitFraction != 1f)
                            //{
                            //    int ibreak = 0;
                            //    convexCaster.calcTimeOfImpact(ref rayFromTrans, ref rayToTrans, ref colObjWorldTransform, ref colObjWorldTransform, castResult);
                            //}

                            //#ifdef USE_SUBSIMPLEX_CONVEX_CAST
                            //rotate normal into worldspace
                            castResult.m_normal = rayFromTrans._basis * castResult.m_normal;
                            //#endif //USE_SUBSIMPLEX_CONVEX_CAST

                            castResult.m_normal.Normalize();
                            LocalRayResult localRayResult = new LocalRayResult(
                                    collisionObject,
                                    //null, // updated to allow different ctor on struct
                                    ref castResult.m_normal,
                                    castResult.m_fraction
                                );

                            bool normalInWorldSpace = true;
                            resultCallback.AddSingleResult(ref localRayResult, normalInWorldSpace);

                        }
                    }
                }
                castResult.Cleanup();
                BulletGlobals.SubSimplexConvexCastPool.Free(convexCaster);
                BulletGlobals.VoronoiSimplexSolverPool.Free(simplexSolver);

                BulletGlobals.StopProfile();
            }
            else
            {
                if (collisionShape.IsConcave())
                {
                    BulletGlobals.StartProfile("rayTestConcave");
                    if (collisionShape.GetShapeType() == BroadphaseNativeTypes.TRIANGLE_MESH_SHAPE_PROXYTYPE && collisionShape is BvhTriangleMeshShape)
                    {
                        ///optimized version for btBvhTriangleMeshShape
                        BvhTriangleMeshShape triangleMesh = (BvhTriangleMeshShape)collisionShape;
                        IndexedMatrix worldTocollisionObject = colObjWorldTransform.Inverse();
                        IndexedVector3 rayFromLocal = worldTocollisionObject * rayFromTrans._origin;
                        IndexedVector3 rayToLocal = worldTocollisionObject * rayToTrans._origin;

                        IndexedMatrix transform = IndexedMatrix.Identity;
                        using (BridgeTriangleRaycastCallback rcb = BulletGlobals.BridgeTriangleRaycastCallbackPool.Get())
                        {
                            rcb.Initialize(ref rayFromLocal, ref rayToLocal, resultCallback, collisionObject, triangleMesh, ref transform);
                            rcb.m_hitFraction = resultCallback.m_closestHitFraction;
                            triangleMesh.PerformRaycast(rcb, ref rayFromLocal, ref rayToLocal);
                        }
                    }
                    else if (collisionShape.GetShapeType() == BroadphaseNativeTypes.TERRAIN_SHAPE_PROXYTYPE && collisionShape is HeightfieldTerrainShape)
                    {
                        ///optimized version for btBvhTriangleMeshShape
                        HeightfieldTerrainShape heightField = (HeightfieldTerrainShape)collisionShape;
                        IndexedMatrix worldTocollisionObject = colObjWorldTransform.Inverse();
                        IndexedVector3 rayFromLocal = worldTocollisionObject * rayFromTrans._origin;
                        IndexedVector3 rayToLocal = worldTocollisionObject * rayToTrans._origin;

                        IndexedMatrix transform = IndexedMatrix.Identity;
                        using (BridgeTriangleConcaveRaycastCallback rcb = BulletGlobals.BridgeTriangleConcaveRaycastCallbackPool.Get())
                        {
                            rcb.Initialize(ref rayFromLocal, ref rayToLocal, resultCallback, collisionObject, heightField, ref transform);
                            rcb.m_hitFraction = resultCallback.m_closestHitFraction;
                            heightField.PerformRaycast(rcb, ref rayFromLocal, ref rayToLocal);
                        }
                    }
                    else
                    {
                        //generic (slower) case
                        ConcaveShape concaveShape = (ConcaveShape)collisionShape;

                        IndexedMatrix worldTocollisionObject = colObjWorldTransform.Inverse();

                        IndexedVector3 rayFromLocal = worldTocollisionObject * rayFromTrans._origin;
                        IndexedVector3 rayToLocal = worldTocollisionObject * rayToTrans._origin;

                        //ConvexCast::CastResult
                        IndexedMatrix transform = IndexedMatrix.Identity;
                        using (BridgeTriangleConcaveRaycastCallback rcb = BulletGlobals.BridgeTriangleConcaveRaycastCallbackPool.Get())
                        {
                            rcb.Initialize(ref rayFromLocal, ref rayToLocal, resultCallback, collisionObject, concaveShape, ref transform);
                            rcb.m_hitFraction = resultCallback.m_closestHitFraction;

                        IndexedVector3 rayAabbMinLocal = rayFromLocal;
                        MathUtil.VectorMin(ref rayToLocal, ref rayAabbMinLocal);
                        IndexedVector3 rayAabbMaxLocal = rayFromLocal;
                        MathUtil.VectorMax(ref rayToLocal, ref rayAabbMaxLocal);

                            concaveShape.ProcessAllTriangles(rcb, ref rayAabbMinLocal, ref rayAabbMaxLocal);
                        }
                    }
                    BulletGlobals.StopProfile();
                }
                else
                {
                    BulletGlobals.StartProfile("rayTestCompound");
                    ///@todo: use AABB tree or other BVH acceleration structure, see btDbvt
                    if (collisionShape.IsCompound())
                    {

                        CompoundShape compoundShape = collisionShape as CompoundShape;
                        Dbvt dbvt = compoundShape.GetDynamicAabbTree();


                        RayTester rayCB = new RayTester(
                            collisionObject,
                            compoundShape,
                            ref colObjWorldTransform,
                            ref rayFromTrans,
                            ref rayToTrans,
                            resultCallback);
#if !DISABLE_DBVT_COMPOUNDSHAPE_RAYCAST_ACCELERATION
                        if (dbvt != null)
                        {
                            IndexedVector3 localRayFrom = colObjWorldTransform.InverseTimes(ref rayFromTrans)._origin;
                            IndexedVector3 localRayTo = colObjWorldTransform.InverseTimes(ref rayToTrans)._origin;

                            Dbvt.RayTest(dbvt.m_root, ref localRayFrom, ref localRayTo, rayCB);
                        }
                        else
#endif //DISABLE_DBVT_COMPOUNDSHAPE_RAYCAST_ACCELERATION
                        {
                            for (int i = 0, n = compoundShape.GetNumChildShapes(); i < n; ++i)
                            {
                                rayCB.Process(i);
                            }
                        }
                        rayCB.Cleanup();
                        BulletGlobals.StopProfile();
                    }
                }
            }
            BulletGlobals.SphereShapePool.Free(pointShape);
        }
Пример #2
0
        public void DrawXNA(ref IndexedMatrix m, CollisionShape shape, ref IndexedVector3 color, DebugDrawModes debugMode, ref IndexedVector3 worldBoundsMin, ref IndexedVector3 worldBoundsMax, ref IndexedMatrix view, ref IndexedMatrix projection)
        {
            //btglMultMatrix(m);
            if (shape == null)
            {
                return;
            }

            if (shape.GetShapeType() == BroadphaseNativeTypes.UNIFORM_SCALING_SHAPE_PROXYTYPE)
            {
                UniformScalingShape scalingShape = (UniformScalingShape)shape;
                ConvexShape convexShape = scalingShape.GetChildShape();
                float scalingFactor = scalingShape.GetUniformScalingFactor();
                IndexedMatrix scaleMatrix = IndexedMatrix.CreateScale(scalingFactor);
                IndexedMatrix finalMatrix = scaleMatrix * m;
                DrawXNA(ref finalMatrix, convexShape, ref color, debugMode, ref worldBoundsMin, ref worldBoundsMax,ref view,ref projection);
                return;
            }
            if (shape.GetShapeType() == BroadphaseNativeTypes.COMPOUND_SHAPE_PROXYTYPE)
            {
                CompoundShape compoundShape = (CompoundShape)shape;
                for (int i = compoundShape.GetNumChildShapes() - 1; i >= 0; i--)
                {
                    IndexedMatrix childTrans = compoundShape.GetChildTransform(i);
                    CollisionShape colShape = compoundShape.GetChildShape(i);
                    IndexedMatrix childMat = childTrans;

					//childMat = MathUtil.bulletMatrixMultiply(m, childMat);
                    //childMat = childMat * m;
                    childMat = m * childMat;

                    
					
					DrawXNA(ref childMat, colShape, ref color, debugMode, ref worldBoundsMin, ref worldBoundsMax,ref view,ref projection);
                }
            }
            else
            {

                bool useWireframeFallback = true;

                if ((debugMode & DebugDrawModes.DBG_DrawWireframe) == 0)
                {
                    ///you can comment out any of the specific cases, and use the default
                    ///the benefit of 'default' is that it approximates the actual collision shape including collision margin
                    //BroadphaseNativeTypes shapetype = m_textureEnabled ? BroadphaseNativeTypes.MAX_BROADPHASE_COLLISION_TYPES : shape.getShapeType();
                    BroadphaseNativeTypes shapetype = shape.GetShapeType();
                    switch (shapetype)
                    {
                        case BroadphaseNativeTypes.BOX_SHAPE_PROXYTYPE:
                            {
                                BoxShape boxShape = shape as BoxShape;
                                IndexedVector3 halfExtents = boxShape.GetHalfExtentsWithMargin();

                                DrawSolidCube(ref halfExtents, ref m, ref view, ref projection,ref color);
                                //drawSolidSphere(halfExtents.X, 10, 10, ref m, ref view, ref projection);
                                //drawCylinder(halfExtents.X, halfExtents.Y, 1, ref m, ref view, ref projection);
                                //drawSolidCone(halfExtents.Y, halfExtents.X, ref m, ref view, ref projection);

                                //DrawText("Hello World", new IndexedVector3(20, 20, 0), new IndexedVector3(255, 255, 255));
                                useWireframeFallback = false;
                                break;
                            }


                        case BroadphaseNativeTypes.SPHERE_SHAPE_PROXYTYPE:
                            {
                                SphereShape sphereShape = shape as SphereShape;
                                float radius = sphereShape.GetMargin();//radius doesn't include the margin, so draw with margin
								DrawSolidSphere(radius, 10, 10, ref m, ref view, ref projection, ref color);
                                //glutSolidSphere(radius,10,10);
                                useWireframeFallback = false;
                                break;
                            }
                        case BroadphaseNativeTypes.CAPSULE_SHAPE_PROXYTYPE:
		                    {
                                CapsuleShape capsuleShape = shape as CapsuleShape;

			                    float radius = capsuleShape.GetRadius();
			                    float halfHeight = capsuleShape.GetHalfHeight();

			                    int upAxis = capsuleShape.GetUpAxis();

			                    IndexedVector3 capStart = IndexedVector3.Zero;
			                    capStart[upAxis] = -halfHeight;

                                IndexedVector3 capEnd = IndexedVector3.Zero;
                                capEnd[upAxis] = halfHeight;

			                    // Draw the ends
			                    {

				                    IndexedMatrix childTransform = IndexedMatrix.Identity;
                                    childTransform._origin = m * capStart;
									DrawSolidSphere(radius, 5, 5, ref childTransform, ref view, ref projection, ref color);
			                    }

			                    {
                                    IndexedMatrix childTransform = IndexedMatrix.Identity;
                                    childTransform._origin = m * capEnd;
									DrawSolidSphere(radius, 5, 5, ref childTransform, ref view, ref projection, ref color);
                                }

                                DrawCylinder(radius, halfHeight, upAxis, ref m, ref view, ref projection,ref color);
                                break;
		                    }
                        case BroadphaseNativeTypes.CONE_SHAPE_PROXYTYPE:
                            {
                                ConeShape coneShape = (ConeShape)(shape);
                                int upIndex = coneShape.GetConeUpIndex();
                                float radius = coneShape.GetRadius();//+coneShape.getMargin();
                                float height = coneShape.GetHeight();//+coneShape.getMargin();
                                IndexedMatrix rotateMatrix = IndexedMatrix.Identity;


                                switch (upIndex)
                                {
                                    case 0:
                                        rotateMatrix = IndexedMatrix.CreateRotationX(-MathUtil.SIMD_HALF_PI);
                                        break;
                                    case 1:
                                        break;
                                    case 2:
                                        rotateMatrix = IndexedMatrix.CreateRotationX(MathUtil.SIMD_HALF_PI);
                                        break;
                                    default:
                                        {
                                            break;
                                        }
                                };

                                IndexedMatrix translationMatrix = IndexedMatrix.CreateTranslation(0f, 0f, -0.5f * height);

                                IndexedMatrix resultant = m * rotateMatrix * translationMatrix;

                                DrawSolidCone(height, radius, ref resultant, ref view, ref projection, ref color);
                                useWireframeFallback = false;
                                break;

                            }


                        case BroadphaseNativeTypes.STATIC_PLANE_PROXYTYPE:
                            {
                                StaticPlaneShape staticPlaneShape = shape as StaticPlaneShape;
                                float planeConst = staticPlaneShape.GetPlaneConstant();
                                IndexedVector3 planeNormal = staticPlaneShape.GetPlaneNormal();
                                IndexedVector3 planeOrigin = planeNormal * planeConst;
                                IndexedVector3 vec0, vec1;
                                TransformUtil.PlaneSpace1(ref planeNormal, out vec0, out vec1);
                                float vecLen = 100f;
                                IndexedVector3 pt0 = planeOrigin + vec0 * vecLen;
                                IndexedVector3 pt1 = planeOrigin - vec0 * vecLen;
                                IndexedVector3 pt2 = planeOrigin + vec1 * vecLen;
                                IndexedVector3 pt3 = planeOrigin - vec1 * vecLen;

                                // Fallback to debug draw - needs tidying
                                IndexedVector3 colour = new IndexedVector3(255, 255, 255);
                                DrawLine(ref pt0, ref pt1, ref colour);
                                DrawLine(ref pt1, ref pt2, ref colour);
                                DrawLine(ref pt2, ref pt3, ref colour);
                                DrawLine(ref pt3, ref pt1, ref colour);

                                break;

                            }

                        case BroadphaseNativeTypes.CYLINDER_SHAPE_PROXYTYPE:
                            {
                                CylinderShape cylinder = (CylinderShape)(shape);
                                int upAxis = cylinder.GetUpAxis();

                                float radius = cylinder.GetRadius();
                                float halfHeight = cylinder.GetHalfExtentsWithMargin()[upAxis];
								DrawCylinder(radius, halfHeight, upAxis, ref m, ref view, ref projection, ref color);
                                break;
                            }

                        default:
                            {
                                if (shape.IsConvex())
                                {
                                    ShapeCache	sc=Cache(shape as ConvexShape);

                                    //if (shape.getUserPointer())
                                    {
                                        //glutSolidCube(1.0);
                                        ShapeHull hull = sc.m_shapehull/*(btShapeHull*)shape.getUserPointer()*/;

                                        int numTriangles = hull.NumTriangles();
                                        int numIndices = hull.NumIndices();
                                        int numVertices = hull.NumVertices(); 
                                        if (numTriangles > 0)
                                        {
                                            int index = 0;
                                            IList<int> idx = hull.m_indices;
                                            IList<IndexedVector3> vtx = hull.m_vertices;

                                            for (int i = 0; i < numTriangles; i++)
                                            {
                                                int i1 = index++;
                                                int i2 = index++;
                                                int i3 = index++;
                                                Debug.Assert(i1 < numIndices &&
                                                    i2 < numIndices &&
                                                    i3 < numIndices);

                                                int index1 = idx[i1];
                                                int index2 = idx[i2];
                                                int index3 = idx[i3];
                                                Debug.Assert(index1 < numVertices &&
                                                    index2 < numVertices &&
                                                    index3 < numVertices);

                                                IndexedVector3 v1 = m * vtx[index1];
                                                IndexedVector3 v2 = m * vtx[index2];
                                                IndexedVector3 v3 = m * vtx[index3];
                                                IndexedVector3 normal = IndexedVector3.Cross((v3-v1),(v2-v1));
                                                normal.Normalize();

                                                Vector2 tex = new Vector2(0,0);
                                                AddVertex(ref v1, ref normal,ref tex);
                                                AddVertex(ref v2, ref normal, ref tex);
                                                AddVertex(ref v3, ref normal, ref tex);
                                            }
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }

                    /// for polyhedral shapes
                    if (debugMode == DebugDrawModes.DBG_DrawFeaturesText && (shape.IsPolyhedral()))
                    {
                        PolyhedralConvexShape polyshape = (PolyhedralConvexShape)shape;
                        {
                            //BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),polyshape.getExtraDebugInfo());

                            IndexedVector3 colour = new IndexedVector3(255, 255, 255);
                            for (int i = 0; i < polyshape.GetNumVertices(); i++)
                            {
                                IndexedVector3 vtx;
                                polyshape.GetVertex(i, out vtx);
                                String buf = " " + i;
                                DrawText(buf, ref vtx, ref colour);
                            }

                            for (int i = 0; i < polyshape.GetNumPlanes(); i++)
                            {
                                IndexedVector3 normal;
                                IndexedVector3 vtx;
                                polyshape.GetPlane(out normal, out vtx, i);
                                float d = IndexedVector3.Dot(vtx, normal);
                                vtx *= d;

                                String buf = " plane " + i;
                                DrawText(buf, ref vtx, ref colour);
                            }
                        }
                    }

                    if (shape.IsConcave() && !shape.IsInfinite())//>getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE||shape.getShapeType() == GIMPACT_SHAPE_PROXYTYPE)
                    //		if (shape.getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
                    {
                        ConcaveShape concaveMesh = shape as ConcaveShape;

                        XNADrawcallback drawCallback = new XNADrawcallback(this,ref m);
                        drawCallback.m_wireframe = (debugMode & DebugDrawModes.DBG_DrawWireframe) != 0;

                        concaveMesh.ProcessAllTriangles(drawCallback, ref worldBoundsMin, ref worldBoundsMax);

                    }

                    //glDisable(GL_DEPTH_TEST);
                    //glRasterPos3f(0,0,0);//mvtx.x(),  vtx.y(),  vtx.z());
                    if ((debugMode & DebugDrawModes.DBG_DrawText) != 0)
                    {
                        IndexedVector3 position = IndexedVector3.Zero;
                        IndexedVector3 colour = new IndexedVector3(255, 255, 255);
                        DrawText(shape.GetName(), ref position, ref colour);
                    }

                    if ((debugMode & DebugDrawModes.DBG_DrawFeaturesText) != 0)
                    {
                        //drawText(shape.getEx]
                        //BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),shape.getExtraDebugInfo());
                    }
                    //glEnable(GL_DEPTH_TEST);

                    ////	glPopMatrix();	
                    //if(m_textureenabled) glDisable(GL_TEXTURE_2D);
                    //  }
                    //    glPopMatrix();
            }
        }
Пример #3
0
        /// objectQuerySingle performs a collision detection query and calls the resultCallback. It is used internally by rayTest.
        public static void ObjectQuerySingle(ConvexShape castShape, ref IndexedMatrix convexFromTrans, ref IndexedMatrix convexToTrans,
                          CollisionObject collisionObject, CollisionShape collisionShape,
                          ref IndexedMatrix colObjWorldTransform,
                          ConvexResultCallback resultCallback, float allowedPenetration)
        {
            if (collisionShape.IsConvex())
            {

                BulletGlobals.StartProfile("convexSweepConvex");
                CastResult castResult = BulletGlobals.CastResultPool.Get();
                castResult.m_allowedPenetration = allowedPenetration;
                castResult.m_fraction = resultCallback.m_closestHitFraction;//float(1.);//??

                ConvexShape convexShape = collisionShape as ConvexShape;
                VoronoiSimplexSolver simplexSolver = BulletGlobals.VoronoiSimplexSolverPool.Get();
                GjkEpaPenetrationDepthSolver gjkEpaPenetrationSolver = BulletGlobals.GjkEpaPenetrationDepthSolverPool.Get();

                ContinuousConvexCollision convexCaster1 = BulletGlobals.ContinuousConvexCollisionPool.Get();
                convexCaster1.Initialize(castShape, convexShape, simplexSolver, gjkEpaPenetrationSolver);
                //btGjkConvexCast convexCaster2(castShape,convexShape,&simplexSolver);
                //btSubsimplexConvexCast convexCaster3(castShape,convexShape,&simplexSolver);

                IConvexCast castPtr = convexCaster1;

                if (castPtr.CalcTimeOfImpact(ref convexFromTrans, ref convexToTrans, ref colObjWorldTransform, ref colObjWorldTransform, castResult))
                {
                    //add hit
                    if (castResult.m_normal.LengthSquared() > 0.0001f)
                    {
                        if (castResult.m_fraction < resultCallback.m_closestHitFraction)
                        {
                            castResult.m_normal.Normalize();
                            LocalConvexResult localConvexResult = new LocalConvexResult
                                        (
                                            collisionObject,
                                            //null, // updated to allow different ctor on struct
                                            ref castResult.m_normal,
                                            ref castResult.m_hitPoint,
                                            castResult.m_fraction
                                        );

                            bool normalInWorldSpace = true;
                            resultCallback.AddSingleResult(ref localConvexResult, normalInWorldSpace);

                        }
                    }
                }
                BulletGlobals.ContinuousConvexCollisionPool.Free(convexCaster1);
                BulletGlobals.GjkEpaPenetrationDepthSolverPool.Free(gjkEpaPenetrationSolver);
                BulletGlobals.VoronoiSimplexSolverPool.Free(simplexSolver);
                castResult.Cleanup();
                BulletGlobals.StopProfile();
            }
            else
            {
				if (collisionShape.IsConcave())
				{
					if (collisionShape.GetShapeType() == BroadphaseNativeTypes.TRIANGLE_MESH_SHAPE_PROXYTYPE)
					{
						BulletGlobals.StartProfile("convexSweepbtBvhTriangleMesh");
						BvhTriangleMeshShape triangleMesh = (BvhTriangleMeshShape)collisionShape;
						IndexedMatrix worldTocollisionObject = colObjWorldTransform.Inverse();
                        IndexedVector3 convexFromLocal = worldTocollisionObject * convexFromTrans._origin;
                        IndexedVector3 convexToLocal = worldTocollisionObject * convexToTrans._origin;
						// rotation of box in local mesh space = MeshRotation^-1 * ConvexToRotation

						IndexedMatrix rotationXform = new IndexedMatrix(worldTocollisionObject._basis * convexToTrans._basis,new IndexedVector3(0));

                        using (BridgeTriangleConvexcastCallback tccb = BulletGlobals.BridgeTriangleConvexcastCallbackPool.Get())
                        {
                            tccb.Initialize(castShape, ref convexFromTrans, ref convexToTrans, resultCallback, collisionObject, triangleMesh, ref colObjWorldTransform);
                            tccb.m_hitFraction = resultCallback.m_closestHitFraction;
                            tccb.m_allowedPenetration = allowedPenetration;

                            IndexedVector3 boxMinLocal;
                            IndexedVector3 boxMaxLocal;
                            castShape.GetAabb(ref rotationXform, out boxMinLocal, out boxMaxLocal);
                            triangleMesh.PerformConvexCast(tccb, ref convexFromLocal, ref convexToLocal, ref boxMinLocal, ref boxMaxLocal);
                        }
						BulletGlobals.StopProfile();
					}
					else
					{
						if (collisionShape.GetShapeType() == BroadphaseNativeTypes.STATIC_PLANE_PROXYTYPE)
						{
                            CastResult castResult = BulletGlobals.CastResultPool.Get();
							castResult.m_allowedPenetration = allowedPenetration;
							castResult.m_fraction = resultCallback.m_closestHitFraction;
							StaticPlaneShape planeShape = collisionShape as StaticPlaneShape;
							ContinuousConvexCollision convexCaster1 = new ContinuousConvexCollision(castShape, planeShape);

							if (convexCaster1.CalcTimeOfImpact(ref convexFromTrans, ref convexToTrans, ref colObjWorldTransform, ref colObjWorldTransform, castResult))
							{
								//add hit
								if (castResult.m_normal.LengthSquared() > 0.0001f)
								{
									if (castResult.m_fraction < resultCallback.m_closestHitFraction)
									{
										castResult.m_normal.Normalize();
										LocalConvexResult localConvexResult = new LocalConvexResult
											(
											collisionObject,
                                            //null, // updated to allow different ctor on struct
											ref castResult.m_normal,
											ref castResult.m_hitPoint,
											castResult.m_fraction
											);

										bool normalInWorldSpace = true;
										resultCallback.AddSingleResult(ref localConvexResult, normalInWorldSpace);
									}
								}
							}
                            castResult.Cleanup();
						}
						else
						{
							BulletGlobals.StartProfile("convexSweepConcave");
							ConcaveShape concaveShape = (ConcaveShape)collisionShape;
                            IndexedMatrix worldTocollisionObject = colObjWorldTransform.Inverse();
                            IndexedVector3 convexFromLocal = worldTocollisionObject * convexFromTrans._origin;
                            IndexedVector3 convexToLocal = worldTocollisionObject * convexToTrans._origin;
                            // rotation of box in local mesh space = MeshRotation^-1 * ConvexToRotation
                            IndexedMatrix rotationXform = new IndexedMatrix(worldTocollisionObject._basis * convexToTrans._basis, new IndexedVector3(0));

                            using (BridgeTriangleConvexcastCallback tccb = BulletGlobals.BridgeTriangleConvexcastCallbackPool.Get())
                            {
                                tccb.Initialize(castShape, ref convexFromTrans, ref convexToTrans, resultCallback, collisionObject, concaveShape, ref colObjWorldTransform);
                                tccb.m_hitFraction = resultCallback.m_closestHitFraction;
                                tccb.m_allowedPenetration = allowedPenetration;
                                IndexedVector3 boxMinLocal;
                                IndexedVector3 boxMaxLocal;
                                castShape.GetAabb(ref rotationXform, out boxMinLocal, out boxMaxLocal);

							IndexedVector3 rayAabbMinLocal = convexFromLocal;
							MathUtil.VectorMin(ref convexToLocal, ref rayAabbMinLocal);
							//rayAabbMinLocal.setMin(convexToLocal);
							IndexedVector3 rayAabbMaxLocal = convexFromLocal;
							//rayAabbMaxLocal.setMax(convexToLocal);
							MathUtil.VectorMax(ref convexToLocal, ref rayAabbMaxLocal);

                                rayAabbMinLocal += boxMinLocal;
                                rayAabbMaxLocal += boxMaxLocal;
                                concaveShape.ProcessAllTriangles(tccb, ref rayAabbMinLocal, ref rayAabbMaxLocal);
                                BulletGlobals.StopProfile();
                            }
						}
					}
				}
				else
				{
					///@todo : use AABB tree or other BVH acceleration structure!
					if (collisionShape.IsCompound())
					{
						BulletGlobals.StartProfile("convexSweepCompound");
						CompoundShape compoundShape = (CompoundShape)collisionShape;
						for (int i = 0; i < compoundShape.GetNumChildShapes(); i++)
						{
							IndexedMatrix childTrans = compoundShape.GetChildTransform(i);
							CollisionShape childCollisionShape = compoundShape.GetChildShape(i);
							IndexedMatrix childWorldTrans = colObjWorldTransform * childTrans;
							// replace collision shape so that callback can determine the triangle
							CollisionShape saveCollisionShape = collisionObject.GetCollisionShape();
							collisionObject.InternalSetTemporaryCollisionShape(childCollisionShape);

							LocalInfoAdder my_cb = new LocalInfoAdder(i, resultCallback);
							my_cb.m_closestHitFraction = resultCallback.m_closestHitFraction;


							ObjectQuerySingle(castShape, ref convexFromTrans, ref convexToTrans,
								collisionObject,
								childCollisionShape,
								ref childWorldTrans,
								my_cb, allowedPenetration);
							// restore
							collisionObject.InternalSetTemporaryCollisionShape(saveCollisionShape);
						}
						BulletGlobals.StopProfile();
					}
				}
            }
        }
Пример #4
0
		public virtual void	DrawShadow(ref IndexedMatrix m, ref IndexedVector3 extrusion,CollisionShape shape,ref IndexedVector3 worldBoundsMin,ref IndexedVector3 worldBoundsMax)
        {
	        if(shape.GetShapeType() == BroadphaseNativeTypes.UNIFORM_SCALING_SHAPE_PROXYTYPE)
	        {
		        UniformScalingShape scalingShape = (UniformScalingShape)(shape);
		        ConvexShape convexShape = scalingShape.GetChildShape();
		        float	scalingFactor = (float)scalingShape.GetUniformScalingFactor();
		        IndexedMatrix tmpScaling = IndexedMatrix.CreateScale(scalingFactor);
                tmpScaling *= m;
		        DrawShadow(ref tmpScaling,ref extrusion,convexShape,ref worldBoundsMin,ref worldBoundsMax);

		        return;
	        }
	        else if(shape.GetShapeType()==BroadphaseNativeTypes.COMPOUND_SHAPE_PROXYTYPE)
	        {
		        CompoundShape compoundShape = (CompoundShape)(shape);
		        for (int i=compoundShape.GetNumChildShapes()-1;i>=0;i--)
		        {
			        IndexedMatrix childTrans = compoundShape.GetChildTransform(i);
			        CollisionShape colShape = compoundShape.GetChildShape(i);
                    //float childMat[16];
                    //childTrans.getOpenGLMatrix(childMat);
                    IndexedVector3 transformedExtrude = childTrans._basis * extrusion;
			        DrawShadow(ref childTrans,ref transformedExtrude,colShape,ref worldBoundsMin,ref worldBoundsMax);
		        }
	        }
	        else
	        {
		        bool useWireframeFallback = true;
                if (shape.IsConvex())
                {
                    ShapeCache	sc=Cache(shape as ConvexShape);
                    ShapeHull hull  = sc.m_shapehull;
                    //glBegin(GL_QUADS);
                    for(int i=0;i<sc.m_edges.Count;++i)
                    {			
                        float d=IndexedVector3.Dot(sc.m_edges[i].n[0],extrusion);
                        if((d*IndexedVector3.Dot(sc.m_edges[i].n[1],extrusion))<0)
                        {
                            int	q=	d<0?1:0;
                            IndexedVector3	a=	hull.m_vertices[sc.m_edges[i].v[q]];
                            IndexedVector3	b=	hull.m_vertices[sc.m_edges[i].v[1-q]];
                            IndexedVector3 ae = a + extrusion;
                            IndexedVector3 be = b + extrusion;
                            Vector2 tex = new Vector2(0,0);
                            // fix me.
                            IndexedVector3 normal = new IndexedVector3(0,1,0);
                            // gl_quad turned into two triangles.
                            AddVertex(ref a, ref normal,ref tex);
                            AddVertex(ref b, ref normal, ref tex);
                            AddVertex(ref be, ref normal, ref tex);
                            AddVertex(ref be, ref normal, ref tex);
                            AddVertex(ref ae, ref normal, ref tex);
                            AddVertex(ref a, ref normal, ref tex);

                        }
                    }
                    //glEnd();
                }

	        }

	        if (shape.IsConcave())//>getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE||shape.getShapeType() == GIMPACT_SHAPE_PROXYTYPE)
		        //		if (shape.getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
	        {
		        ConcaveShape concaveMesh = (ConcaveShape) shape;

		        XNADrawcallback drawCallback = new XNADrawcallback(this,ref m);
		        drawCallback.m_wireframe = false;

		        concaveMesh.ProcessAllTriangles(drawCallback,ref worldBoundsMin,ref worldBoundsMax);

	        }
            //glPopMatrix();

        }
Пример #5
0
 //! Use this method for adding children. Only Convex shapes are allowed.
 public void AddChildShape(CollisionShape shape)
 {
     Debug.Assert(shape.IsConvex());
     m_childShapes.Add(shape);
 }
Пример #6
0
 //! Use this method for adding children. Only Convex shapes are allowed.
 public void AddChildShape(ref IndexedMatrix localTransform, CollisionShape shape)
 {
     Debug.Assert(shape.IsConvex());
     m_childTransforms.Add(localTransform);
     m_childShapes.Add(shape);
 }
Пример #7
0
 //! Use this method for adding children. Only Convex shapes are allowed.
 public void AddChildShape(CollisionShape shape)
 {
     Debug.Assert(shape.IsConvex());
     m_childShapes.Add(shape);
 }
Пример #8
0
 //! Use this method for adding children. Only Convex shapes are allowed.
 public void AddChildShape(ref IndexedMatrix localTransform, CollisionShape shape)
 {
     Debug.Assert(shape.IsConvex());
     m_childTransforms.Add(localTransform);
     m_childShapes.Add(shape);
 }