Ejemplo n.º 1
0
 public unsafe static void GetHitNormalLocal(this LocalRayResult obj, out OpenTK.Vector3 value)
 {
     fixed(OpenTK.Vector3 *valuePtr = &value)
     {
         *(BulletSharp.Math.Vector3 *)valuePtr = obj.HitNormalLocal;
     }
 }
        public override float AddSingleResult(LocalRayResult rayResult, bool normalInWorldSpace)
        {
            if (rayResult.CollisionObject == _me)
            {
                return(1.0f);
            }

            return(base.AddSingleResult(rayResult, normalInWorldSpace));
        }
Ejemplo n.º 3
0
 public override float AddSingleResult(LocalRayResult rayResult, bool normalInWorldSpace)
 {
     if (rayResult.LocalShapeInfo != null)
     {
         Success       = true;
         TriangleIndex = rayResult.LocalShapeInfo.TriangleIndex;
     }
     return(base.AddSingleResult(rayResult, normalInWorldSpace));
 }
Ejemplo n.º 4
0
 public override float AddSingleResult(LocalRayResult rayResult)
 {
     //caller already does the filter on the m_closestHitFraction
     //assert(rayResult.m_hitFraction <= m_closestHitFraction);
     ClosestHitFraction = rayResult.HitFraction;
     _collisionObject   = rayResult.CollisionObject;
     _hitNormalWorld    = Vector3.TransformNormal(rayResult.HitNormalLocal, _collisionObject.WorldTransform);
     MathHelper.SetInterpolate3(_rayFromWorld, _rayToWorld, rayResult.HitFraction, ref _hitPointWorld);
     return(rayResult.HitFraction);
 }
Ejemplo n.º 5
0
		public override float AddSingleResult(LocalRayResult r, bool b)
		{
			LocalShapeInfo	shapeInfo = new LocalShapeInfo();
			shapeInfo.m_shapePart = -1;
			shapeInfo.m_triangleIndex = m_i;
			if (r.m_localShapeInfo == null)
			{
				r.m_localShapeInfo = shapeInfo;
			}
			return m_userCallback.AddSingleResult(r, b);
		}
Ejemplo n.º 6
0
        public override float AddSingleResult(LocalRayResult rayResult, bool normalInWorldSpace)
        {
            var meshObject = rayResult.CollisionObject.CollisionShape as BvhTriangleMeshShape;

            if (meshObject == null)
            {
                return(base.AddSingleResult(rayResult, normalInWorldSpace));
            }
            ;

            var triangleBuffer = new TriangleBuffer( );

            meshObject.PerformRaycast(triangleBuffer, RayFromWorld, RayToWorld);

            var   rayFromWorld     = RayFromWorld;
            var   rayTarget        = RayToWorld - rayFromWorld;
            float closestFrontFace = 1f;

            for (int i = 0; i < triangleBuffer.NumTriangles; i++)
            {
                var triangle = triangleBuffer.GetTriangle(i);

                var edge1  = triangle.vertex1 - triangle.vertex0;
                var edge2  = triangle.vertex2 - triangle.vertex0;
                var normal = Vector3.Cross(edge1, edge2);

                var dot = _ccw ? Vector3.Dot(rayTarget, normal) : -Vector3.Dot(normal, rayTarget);
                if (dot > 0)
                {
                    continue;
                }

                float t;
                if (TriangleRayIntersection.Test(triangle.vertex0, triangle.vertex1, triangle.vertex2,
                                                 rayFromWorld, rayTarget, out t))
                {
                    closestFrontFace = closestFrontFace < t ? closestFrontFace : t;
                }
            }
            rayResult.HitFraction = closestFrontFace;
            if (closestFrontFace < ClosestHitFraction)
            {
                base.AddSingleResult(rayResult, normalInWorldSpace);
            }
            return(closestFrontFace);
        }
Ejemplo n.º 7
0
 public override double AddSingleResult(LocalRayResult rayResult, bool normalInWorldSpace)
 {
     CollisionObject = rayResult.CollisionObject;
     CollisionObjects.Add(rayResult.CollisionObject);
     if (normalInWorldSpace)
     {
         HitNormalWorld.Add(rayResult.HitNormalLocal);
     }
     else
     {
         // need to transform normal into worldspace
         HitNormalWorld.Add(Vector3.TransformCoordinate(rayResult.HitNormalLocal, CollisionObject.WorldTransform.Basis));
     }
     HitPointWorld.Add(Vector3.Lerp(RayFromWorld, RayToWorld, rayResult.HitFraction));
     HitFractions.Add(rayResult.HitFraction);
     return(ClosestHitFraction);
 }
Ejemplo n.º 8
0
        public override float AddSingleResult(LocalRayResult rayResult, bool normalInWorldSpace)
        {
            Debug.Assert(rayResult.HitFraction <= this.ClosestHitFraction, "invalid ray hit fraction");
            this.ClosestHitFraction = rayResult.HitFraction;
            this.CollisionObject    = rayResult.CollisionObject;
            if (normalInWorldSpace)
            {
                HitNormalWorld = rayResult.HitNormalLocal;
            }
            else
            {
                BulletSharp.Math.Matrix om;
                this.CollisionObject.GetWorldTransform(out om);
                //this.HitNormalWorld = om.Basis * rayResult.HitNormalLocal;
            }

            return(0f);
        }
Ejemplo n.º 9
0
        public override float AddSingleResult(LocalRayResult rayResult, bool normalInWorldSpace)
        {
            if (rayResult.HitFraction >= 0.0f && rayResult.HitFraction <= 1.0f)
            {
                var col = rayResult.CollisionObject;
                if (col == null)
                {
                    return(1.0f);
                }

                var uo = col.UserObject as CollisionObject;
                if (uo == null)
                {
                    return(1.0f);
                }

                var go = uo.tag as GameComponent;
                if (go == null)
                {
                    return(1.0f);
                }

                var obj = go.GameObject;
                if (obj == null)
                {
                    return(1.0f);
                }

                if (objects.Contains(obj))
                {
                    return(1.0f);
                }

                Results.Add(new RayTestResult()
                {
                    Rate       = rayResult.HitFraction,
                    Normal     = rayResult.HitNormalLocal,
                    GameObject = obj,
                });
                return(rayResult.HitFraction);
            }

            return(1.0f);
        }
Ejemplo n.º 10
0
        public override double AddSingleResult(LocalRayResult rayResult, bool normalInWorldSpace)
        {
            //caller already does the filter on the m_closestHitFraction
            Debug.Assert(rayResult.HitFraction <= ClosestHitFraction);

            ClosestHitFraction = rayResult.HitFraction;
            CollisionObject    = rayResult.CollisionObject;
            if (normalInWorldSpace)
            {
                HitNormalWorld = rayResult.HitNormalLocal;
            }
            else
            {
                // need to transform normal into worldspace
                HitNormalWorld = Vector3.TransformCoordinate(rayResult.HitNormalLocal, CollisionObject.WorldTransform.Basis);
            }
            HitPointWorld = Vector3.Lerp(RayFromWorld, RayToWorld, rayResult.HitFraction);
            return(rayResult.HitFraction);
        }
Ejemplo n.º 11
0
			public abstract float AddSingleResult(LocalRayResult rayResult);
Ejemplo n.º 12
0
        // rayTestSingle performs a raycast call and calls the resultCallback. It is used internally by rayTest.
        // In a future implementation, we consider moving the ray test as a virtual method in CollisionShape.
        // This allows more customization.
        public static void RayTestSingle(Matrix rayFromTrans, Matrix rayToTrans,
                                         CollisionObject collisionObject,
                                         CollisionShape collisionShape,
                                         Matrix colObjWorldTransform,
                                         RayResultCallback resultCallback)
        {
            SphereShape pointShape = new SphereShape(0.0f);

            if (collisionShape.IsConvex)
            {
                CastResult castResult = new CastResult();
                castResult.Fraction = 1f;                //??

                ConvexShape          convexShape   = collisionShape as ConvexShape;
                VoronoiSimplexSolver simplexSolver = new VoronoiSimplexSolver();
                SubsimplexConvexCast convexCaster  = new SubsimplexConvexCast(pointShape, convexShape, simplexSolver);
                //GjkConvexCast	convexCaster(&pointShape,convexShape,&simplexSolver);
                //ContinuousConvexCollision convexCaster(&pointShape,convexShape,&simplexSolver,0);

                if (convexCaster.CalcTimeOfImpact(rayFromTrans, rayToTrans, colObjWorldTransform, colObjWorldTransform, castResult))
                {
                    //add hit
                    if (castResult.Normal.LengthSquared() > 0.0001f)
                    {
                        castResult.Normal.Normalize();
                        if (castResult.Fraction < resultCallback.ClosestHitFraction)
                        {
                            CollisionWorld.LocalRayResult localRayResult = new LocalRayResult
                                                                           (
                                collisionObject,
                                new LocalShapeInfo(),
                                castResult.Normal,
                                castResult.Fraction
                                                                           );

                            resultCallback.AddSingleResult(localRayResult);
                        }
                    }
                }
                else
                {
                    if (collisionShape.IsConcave)
                    {
                        TriangleMeshShape triangleMesh = collisionShape as TriangleMeshShape;

                        Matrix worldTocollisionObject = MathHelper.InvertMatrix(colObjWorldTransform);

                        Vector3 rayFromLocal = Vector3.TransformNormal(rayFromTrans.Translation, worldTocollisionObject);
                        Vector3 rayToLocal   = Vector3.TransformNormal(rayToTrans.Translation, worldTocollisionObject);

                        BridgeTriangleRaycastCallback rcb = new BridgeTriangleRaycastCallback(rayFromLocal, rayToLocal, resultCallback, collisionObject, triangleMesh);
                        rcb.HitFraction = resultCallback.ClosestHitFraction;

                        Vector3 rayAabbMinLocal = rayFromLocal;
                        MathHelper.SetMin(ref rayAabbMinLocal, rayToLocal);
                        Vector3 rayAabbMaxLocal = rayFromLocal;
                        MathHelper.SetMax(ref rayAabbMaxLocal, rayToLocal);

                        triangleMesh.ProcessAllTriangles(rcb, rayAabbMinLocal, rayAabbMaxLocal);
                    }
                    else
                    {
                        //todo: use AABB tree or other BVH acceleration structure!
                        if (collisionShape.IsCompound)
                        {
                            CompoundShape compoundShape = collisionShape as CompoundShape;
                            for (int i = 0; i < compoundShape.ChildShapeCount; i++)
                            {
                                Matrix         childTrans          = compoundShape.GetChildTransform(i);
                                CollisionShape childCollisionShape = compoundShape.GetChildShape(i);
                                Matrix         childWorldTrans     = colObjWorldTransform * childTrans;
                                RayTestSingle(rayFromTrans, rayToTrans,
                                              collisionObject,
                                              childCollisionShape,
                                              childWorldTrans,
                                              resultCallback);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 13
0
 public static void SetHitNormalLocal(this LocalRayResult obj, OpenTK.Vector3 value)
 {
     SetHitNormalLocal(obj, ref value);
 }
Ejemplo n.º 14
0
 public static OpenTK.Vector3 GetHitNormalLocal(this LocalRayResult obj)
 {
     OpenTK.Vector3 value;
     GetHitNormalLocal(obj, out value);
     return(value);
 }
Ejemplo n.º 15
0
 public override double AddSingleResult(LocalRayResult rayResult, bool normalInWorldSpace)
Ejemplo n.º 16
0
			internal override double reportHit( ref btVector3 hitNormalLocal, double hitFraction, int partId, int triangleIndex )
			{
				LocalShapeInfo shapeInfo = new LocalShapeInfo();
				shapeInfo.m_shapePart = partId;
				shapeInfo.m_triangleIndex = triangleIndex;

				btVector3 hitNormalWorld; m_colObjWorldTransform.m_basis.Apply( ref hitNormalLocal, out hitNormalWorld );

				LocalRayResult rayResult = new LocalRayResult
									( m_collisionObject,
									  shapeInfo,
					hitNormalWorld,
					hitFraction );

				bool normalInWorldSpace = true;
				return m_resultCallback.addSingleResult( rayResult, normalInWorldSpace );
			}
Ejemplo n.º 17
0
 public override float AddSingleResult(LocalRayResult rayResult, bool normalInWorldSpace)
 {
     return(base.AddSingleResult(rayResult, normalInWorldSpace));
 }
Ejemplo n.º 18
0
			public override double addSingleResult( LocalRayResult rayResult, bool normalInWorldSpace )
			{
				//caller already does the filter on the m_closestHitFraction
				Debug.Assert( rayResult.m_hitFraction <= m_closestHitFraction );

				m_closestHitFraction = rayResult.m_hitFraction;
				m_collisionObject = rayResult.m_collisionObject;
				if( normalInWorldSpace )
				{
					m_hitNormalWorld = rayResult.m_hitNormalLocal;
				}
				else
				{
					///need to transform normal into worldspace
					m_collisionObject.m_worldTransform.m_basis.Apply( ref rayResult.m_hitNormalLocal, out m_hitNormalWorld );
					//m_hitNormalWorld = m_collisionObject.getWorldTransform().getBasis() * rayResult.m_hitNormalLocal;
				}
				btVector3.setInterpolate3( out m_hitPointWorld, ref m_rayFromWorld, ref m_rayToWorld, rayResult.m_hitFraction );
				return rayResult.m_hitFraction;
			}
Ejemplo n.º 19
0
            public override float AddSingleResult(LocalRayResult rayResult, bool normalInWorldSpace)
            {
                var collisionObject = rayResult.CollisionObject;

                if (collisionObject == null)
                {
                    return(base.AddSingleResult(rayResult, normalInWorldSpace));
                }

                var meshShape = collisionObject.CollisionShape as BvhTriangleMeshShape;

                if (meshShape == null)
                {
                    return(base.AddSingleResult(rayResult, normalInWorldSpace));
                }

                var meshObject = rayResult.CollisionObject.CollisionShape as BvhTriangleMeshShape;

                if (meshObject == null)
                {
                    return(base.AddSingleResult(rayResult, normalInWorldSpace));
                }

                var rayFromWorld = RayFromWorld;
                var rayDirection = RayToWorld - rayFromWorld;

                var triangleBuffer = new TriangleBuffer( );

                meshShape.PerformRaycast(triangleBuffer, RayFromWorld, RayToWorld);

                float closestFrontFace = 1000f;

                for (int i = 0; i < triangleBuffer.NumTriangles; i++)
                {
                    var triangle = triangleBuffer.GetTriangle(i);


                    var triangleIndex = triangle.triangleIndex * 3;
                    var info          = meshShape.MeshInterface as InfoTriangleIndexVertexArray;
                    var part          = info?.IndexedMeshArray[triangle.partId];
                    if (part == null)
                    {
                        continue;
                    }

                    var edge1  = triangle.vertex1 - triangle.vertex0;
                    var edge2  = triangle.vertex2 - triangle.vertex0;
                    var normal = Vector3.Cross(edge1, edge2);

                    var dot = Vector3.Dot(normal, rayDirection);
                    if (dot > 0)
                    {
                        continue;
                    }

                    float t;
                    if (TriangleRayIntersection.Test(triangle.vertex0, triangle.vertex1, triangle.vertex2,
                                                     rayFromWorld, rayDirection, out t))
                    {
                        if (!(closestFrontFace < t))
                        {
                            GLDebug.QueueTriangleDraw(ref triangle.vertex0, ref triangle.vertex1, ref triangle.vertex2);
                            closestFrontFace = t;
                        }
                    }
                    else
                    {
                        continue;
                    }

                    var bitangent = info.SurfaceInfo[triangle.partId].Bitangents[
                        part.TriangleIndices[triangleIndex]];
                    var tangent = Vector3.Cross(bitangent, normal);



                    HitSurfaceTangent   = tangent.Normalized();
                    HitSurfaceNormal    = normal.Normalized();
                    HitSurfaceBitangent = bitangent.Normalized();
                }
                rayResult.HitFraction = closestFrontFace;
                if (closestFrontFace < ClosestHitFraction)
                {
                    base.AddSingleResult(rayResult, normalInWorldSpace);
                }
                return(closestFrontFace);
            }
Ejemplo n.º 20
0
			public override double addSingleResult( LocalRayResult rayResult, bool normalInWorldSpace )
			{
				m_collisionObject = rayResult.m_collisionObject;
				m_collisionObjects.Add( rayResult.m_collisionObject );
				btVector3 hitNormalWorld;
				if( normalInWorldSpace )
				{
					hitNormalWorld = rayResult.m_hitNormalLocal;
				}
				else
				{
					///need to transform normal into worldspace
					m_collisionObject.m_worldTransform.m_basis.Apply( ref rayResult.m_hitNormalLocal, out hitNormalWorld );
					//hitNormalWorld = m_collisionObject.getWorldTransform().getBasis() * rayResult.m_hitNormalLocal;
				}
				m_hitNormalWorld.Add( ref hitNormalWorld );
				btVector3 hitPointWorld;
				btVector3.setInterpolate3( out hitPointWorld, ref m_rayFromWorld, ref m_rayToWorld, rayResult.m_hitFraction );
				m_hitPointWorld.Add( hitPointWorld );
				m_hitFractions.Add( rayResult.m_hitFraction );
				return m_closestHitFraction;
			}
Ejemplo n.º 21
0
		public static void rayTestSingleInternal( ref btTransform rayFromTrans, ref btTransform rayToTrans,
												btCollisionShape collisionShape,
												btCollisionObject collisionObject,
												ref btTransform useTransform,
												RayResultCallback resultCallback )
		{
			btSphereShape pointShape = BulletGlobals.SphereShapePool.Get();
			pointShape.Initialize( btScalar.BT_ZERO );
			pointShape.setMargin( 0f );
			btConvexShape castShape = pointShape;
			//btCollisionShape collisionShape = collisionObjectWrap.getCollisionShape();
			btTransform colObjWorldTransform = useTransform;// collisionObjectWrap.m_worldTransform;

			if( collisionShape.isConvex() )
			{
				//		CProfileSample sample = new CProfileSample("rayTestConvex");
				btConvexCast.CastResult castResult = new btConvexCast.CastResult();

				castResult.m_fraction = resultCallback.m_closestHitFraction;

				btConvexShape convexShape = (btConvexShape)collisionShape;
				btVoronoiSimplexSolver simplexSolver = BulletGlobals.VoronoiSimplexSolverPool.Get();
				//new btVoronoiSimplexSolver();
				btSubsimplexConvexCast subSimplexConvexCaster = BulletGlobals.SubSimplexConvexCastPool.Get();
				subSimplexConvexCaster.Initialize( castShape, convexShape, simplexSolver );

				btGjkConvexCast gjkConvexCaster = BulletGlobals.GjkConvexCastPool.Get();
				gjkConvexCaster.Initialize( castShape, convexShape, simplexSolver );
				BulletGlobals.SubSimplexConvexCastPool.Free( subSimplexConvexCaster );
				BulletGlobals.VoronoiSimplexSolverPool.Free( simplexSolver );

				//btContinuousConvexCollision convexCaster(castShape,convexShape,&simplexSolver,0);

				btConvexCast convexCasterPtr = null;
				//use kF_UseSubSimplexConvexCastRaytest by default
				if( ( resultCallback.m_flags & (uint)btTriangleRaycastCallback.EFlags.kF_UseGjkConvexCastRaytest ) != 0 )
					convexCasterPtr = gjkConvexCaster;
				else
					convexCasterPtr = subSimplexConvexCaster;

				btConvexCast convexCaster = convexCasterPtr;

				if( convexCaster.calcTimeOfImpact( ref rayFromTrans, ref rayToTrans
					, ref useTransform/*collisionObjectWrap.m_worldTransform*/
					, ref useTransform/*collisionObjectWrap.m_worldTransform*/
					, castResult ) )
				{
					//add hit
					if( castResult.m_normal.length2() > (double)( 0.0001 ) )
					{
						if( castResult.m_fraction < resultCallback.m_closestHitFraction )
						{
							//todo: figure out what this is about. When is rayFromTest.getBasis() not identity?
#if USE_SUBSIMPLEX_CONVEX_CAST
							//rotate normal into worldspace
							castResult.m_normal = rayFromTrans.getBasis()  castResult.m_normal;
#endif //USE_SUBSIMPLEX_CONVEX_CAST

							castResult.m_normal.normalize();
							LocalRayResult localRayResult = new LocalRayResult
								(
								collisionObject,
								null,
								castResult.m_normal,
								castResult.m_fraction
								);

							bool normalInWorldSpace = true;
							resultCallback.addSingleResult( localRayResult, normalInWorldSpace );

						}
					}
				}
				BulletGlobals.GjkConvexCastPool.Free( gjkConvexCaster );
			}
			else
			{
				if( collisionShape.isConcave() )
				{
					btTransform worldTocollisionObject; colObjWorldTransform.inverse( out worldTocollisionObject );
					btVector3 tmp;
					rayFromTrans.getOrigin( out tmp );
					btVector3 rayFromLocal; worldTocollisionObject.Apply( ref tmp, out rayFromLocal );
					rayToTrans.getOrigin( out tmp );
					btVector3 rayToLocal; worldTocollisionObject.Apply( ref tmp, out rayToLocal );

					//			CProfileSample sample = new CProfileSample("rayTestConcave");
#if SUPPORT_TRIANGLE_MESH
					if( collisionShape.getShapeType() == BroadphaseNativeTypes.TRIANGLE_MESH_SHAPE_PROXYTYPE )
					{
						///optimized version for btBvhTriangleMeshShape
						btBvhTriangleMeshShape triangleMesh = (btBvhTriangleMeshShape)collisionShape;

						BridgeTriangleRaycastCallback rcb( rayFromLocal, rayToLocal,&resultCallback, collisionObjectWrap.getCollisionObject(), triangleMesh, colObjWorldTransform);
						rcb.m_hitFraction = resultCallback.m_closestHitFraction;
						triangleMesh.performRaycast( &rcb, rayFromLocal, rayToLocal );
					}
					else
#endif
					{
						//generic (slower) case
						btConcaveShape concaveShape = (btConcaveShape)collisionShape;

						//ConvexCast::CastResult


						BridgeTriangleRaycastCallback rcb = new BridgeTriangleRaycastCallback( ref rayFromLocal, ref rayToLocal, resultCallback
							, collisionObject, concaveShape, ref colObjWorldTransform );
						rcb.m_hitFraction = resultCallback.m_closestHitFraction;

						btVector3 rayAabbMinLocal = rayFromLocal;
						rayAabbMinLocal.setMin( ref rayToLocal );
						btVector3 rayAabbMaxLocal = rayFromLocal;
						rayAabbMaxLocal.setMax( ref rayToLocal );

						concaveShape.processAllTriangles( rcb, ref rayAabbMinLocal, ref rayAabbMaxLocal );
					}
				}
				else
				{
					//			CProfileSample sample = new CProfileSample("rayTestCompound");
					if( collisionShape.isCompound() )
					{

						btCompoundShape compoundShape = (btCompoundShape)( collisionShape );
						btDbvt 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 )
						{
							btTransform tmp;
							colObjWorldTransform.inverseTimes( ref rayFromTrans, out tmp );
							btVector3 localRayFrom; tmp.getOrigin( out localRayFrom );
							colObjWorldTransform.inverseTimes( ref rayToTrans, out tmp );
							btVector3 localRayTo; tmp.getOrigin( out localRayTo );
							btDbvt.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 );
							}
						}
					}
				}
			}
			BulletGlobals.SphereShapePool.Free( pointShape );
		}
Ejemplo n.º 22
0
			public override double addSingleResult( LocalRayResult r, bool b )
			{
				LocalShapeInfo shapeInfo = new LocalShapeInfo();
				shapeInfo.m_shapePart = -1;
				shapeInfo.m_triangleIndex = m_i;
				if( r.m_localShapeInfo == null )
					r.m_localShapeInfo = shapeInfo;

				btScalar result = m_userCallback.addSingleResult( r, b );
				m_closestHitFraction = m_userCallback.m_closestHitFraction;
				return result;
			}
Ejemplo n.º 23
0
			public override float AddSingleResult(LocalRayResult rayResult)
			{
				//caller already does the filter on the m_closestHitFraction
				//assert(rayResult.m_hitFraction <= m_closestHitFraction);
				ClosestHitFraction = rayResult.HitFraction;
				_collisionObject = rayResult.CollisionObject;
				_hitNormalWorld = Vector3.TransformNormal(rayResult.HitNormalLocal, _collisionObject.WorldTransform);
				MathHelper.SetInterpolate3(_rayFromWorld, _rayToWorld, rayResult.HitFraction, ref _hitPointWorld);
				return rayResult.HitFraction;
			}
Ejemplo n.º 24
0
		// rayTestSingle performs a raycast call and calls the resultCallback. It is used internally by rayTest.
		// In a future implementation, we consider moving the ray test as a virtual method in CollisionShape.
		// This allows more customization.
		public static void RayTestSingle(Matrix rayFromTrans, Matrix rayToTrans,
						  CollisionObject collisionObject,
						  CollisionShape collisionShape,
						  Matrix colObjWorldTransform,
						  RayResultCallback resultCallback)
		{
			SphereShape pointShape=new SphereShape(0.0f);

			if (collisionShape.IsConvex)
			{
				CastResult castResult = new CastResult();
				castResult.Fraction = 1f;//??

				ConvexShape convexShape = collisionShape as ConvexShape;
				VoronoiSimplexSolver simplexSolver = new VoronoiSimplexSolver();
				SubsimplexConvexCast convexCaster = new SubsimplexConvexCast(pointShape, convexShape, simplexSolver);
				//GjkConvexCast	convexCaster(&pointShape,convexShape,&simplexSolver);
				//ContinuousConvexCollision convexCaster(&pointShape,convexShape,&simplexSolver,0);

				if (convexCaster.CalcTimeOfImpact(rayFromTrans, rayToTrans, colObjWorldTransform, colObjWorldTransform, castResult))
				{
					//add hit
					if (castResult.Normal.LengthSquared() > 0.0001f)
					{
						castResult.Normal.Normalize();
						if (castResult.Fraction < resultCallback.ClosestHitFraction)
						{

							CollisionWorld.LocalRayResult localRayResult = new LocalRayResult
								(
									collisionObject,
									new LocalShapeInfo(),
									castResult.Normal,
									castResult.Fraction
								);

							resultCallback.AddSingleResult(localRayResult);
						}
					}
				}
				else
				{
					if (collisionShape.IsConcave)
					{

						TriangleMeshShape triangleMesh = collisionShape as TriangleMeshShape;

						Matrix worldTocollisionObject = MathHelper.InvertMatrix(colObjWorldTransform);

						Vector3 rayFromLocal = Vector3.TransformNormal(rayFromTrans.Translation, worldTocollisionObject);
						Vector3 rayToLocal = Vector3.TransformNormal(rayToTrans.Translation, worldTocollisionObject);

						BridgeTriangleRaycastCallback rcb = new BridgeTriangleRaycastCallback(rayFromLocal, rayToLocal, resultCallback, collisionObject, triangleMesh);
						rcb.HitFraction = resultCallback.ClosestHitFraction;

						Vector3 rayAabbMinLocal = rayFromLocal;
						MathHelper.SetMin(ref rayAabbMinLocal, rayToLocal);
						Vector3 rayAabbMaxLocal = rayFromLocal;
						MathHelper.SetMax(ref rayAabbMaxLocal, rayToLocal);

						triangleMesh.ProcessAllTriangles(rcb, rayAabbMinLocal, rayAabbMaxLocal);
					}
					else
					{
						//todo: use AABB tree or other BVH acceleration structure!
						if (collisionShape.IsCompound)
						{
							CompoundShape compoundShape = collisionShape as CompoundShape;
							for (int i = 0; i < compoundShape.ChildShapeCount; i++)
							{
								Matrix childTrans = compoundShape.GetChildTransform(i);
								CollisionShape childCollisionShape = compoundShape.GetChildShape(i);
								Matrix childWorldTrans = colObjWorldTransform * childTrans;
								RayTestSingle(rayFromTrans, rayToTrans,
									collisionObject,
									childCollisionShape,
									childWorldTrans,
									resultCallback);
							}
						}
					}
				}
			}
		}
Ejemplo n.º 25
0
			public abstract double addSingleResult( LocalRayResult rayResult, bool normalInWorldSpace );
Ejemplo n.º 26
0
 public abstract float AddSingleResult(LocalRayResult rayResult);
Ejemplo n.º 27
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(LocalRayResult obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }