当前位置: 首页>>代码示例>>C#>>正文


C# Vector.DotProduct方法代码示例

本文整理汇总了C#中Engine.Vector.DotProduct方法的典型用法代码示例。如果您正苦于以下问题:C# Vector.DotProduct方法的具体用法?C# Vector.DotProduct怎么用?C# Vector.DotProduct使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Engine.Vector的用法示例。


在下文中一共展示了Vector.DotProduct方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: VectorToDirection

        // Take a normalized vector and return the direction it's facing.
        // There's a quite of lot of operations here I'm sure it could be made more efficent.
        private Direction VectorToDirection(Vector direction)
        {
            Vector up = new Vector(0, 1, 0);
            Vector down = new Vector(0, -1, 0);
            Vector left = new Vector(-1, 0, 0);
            Vector right = new Vector(1, 0, 0);

            double upDiff = Math.Acos(direction.DotProduct(up));
            double downDiff = Math.Acos(direction.DotProduct(down));
            double leftDiff = Math.Acos(direction.DotProduct(left));
            double rightDiff = Math.Acos(direction.DotProduct(right));

            double smallest = Math.Min(Math.Min(upDiff, downDiff), Math.Min(leftDiff, rightDiff));

            // yes there's a precidence if they're the same value, it doesn't matter
            if (smallest == upDiff)
            {
                return Direction.Up;
            }
            else if (smallest == downDiff)
            {
                return Direction.Down;
            }
            else if (smallest == leftDiff)
            {
                return Direction.Left;
            }
            else
            {
                return Direction.Right;
            }
        }
开发者ID:iq110,项目名称:csharpgameprogramming,代码行数:34,代码来源:PlayerCharacter.cs

示例2: testAxis

        /// <summary>
        /// Test one axis with SAT.
        /// Updates result if there is a "better" hit
        /// </summary>
        private static void testAxis(Projection prj1, Projection prj2, Vector relativeVelocity, Vector axis, CollisionResult result, double remainingFrameFraction, int axisOwner)
        {
            bool isIntersecting = false, willIntersect = false;
            double t = 0; //Collision time

            //Positive distance means we don't have an overlap. Negative means we have an overlap.
            double distance = prj1.GetDistance(prj2);

            #if DEBUG_COLLISION_OBJECT_POLYGON || DEBUG_COLLISION_OBJECT_OBJECT
            Log.Write("\tTesting axis " + axis + ", relative vel: " + relativeVelocity, Log.DEBUG);
            Log.Write("\tProjection 1: " + prj1 + " Projection 2: " + prj2, Log.DEBUG);
            Log.Write("\tDistance " + distance, Log.DEBUG);
            #endif

            //Already intersecting?
            if (distance < 0)
            {
                isIntersecting = true;
                #if DEBUG_COLLISION_OBJECT_POLYGON || DEBUG_COLLISION_OBJECT_OBJECT
                Log.Write("\tIntersecting.", Log.DEBUG);
                #endif
            }
            else
            {
                //Calculate velocity component in direction of axis
                double velAxis = axis.DotProduct(relativeVelocity);

                //if (velAxis < Constants.MinDouble) velAxis = 0;

                #if DEBUG_COLLISION_OBJECT_POLYGON || DEBUG_COLLISION_OBJECT_OBJECT
                Log.Write("\tNot intersecting. Velocity along axis: " + velAxis, Log.DEBUG);
                #endif

                //If projection of polygon 2 is to the right of polygon 1, AND we have a positive velocity along the axis
                //OR projection of polygon 1 is to the left of polygon 2 AND we have a negative velocity along axis
                //then we might have a collision in the future. If not, the objects are either moving in separate directions
                //or they are staying still.
                if ((velAxis > 0 && prj2.Min >= prj1.Max) || (velAxis < 0 && prj1.Min >= prj2.Max))
                {
                    //If the axis belongs to object 1, and it's facing the opposite direction of the velocity,
                    //then ignore it because it can't collide. Also, if the axis belongs to object 2,
                    //and the axis faces the same direction as the velocity, also ignore it.
                    #if DEBUG_COLLISION_OBJECT_POLYGON
                    Log.Write("\tAxis dot Velocity: " + axis.DotProduct(relativeVelocity) * (axisOwner == 0 ? -1 : 1) + " Axis: " + axis, Log.DEBUG);
                    #endif

                    //Ignore this test if the axis faces the wrong way
                    if (axis.DotProduct(relativeVelocity) * (axisOwner == 0 ? -1 : 1) > Constants.MinDouble)
                    {
                        #if DEBUG_COLLISION_OBJECT_POLYGON || DEBUG_COLLISION_OBJECT_OBJECT
                        Log.Write("\tIgnoring test because the edge faces the wrong way. Dot: " + axis.DotProduct(relativeVelocity) * (axisOwner == 0 ? -1 : 1) + "Owner: " + axisOwner);
                        #endif
                        distance = double.NegativeInfinity;
                        isIntersecting = true;
                    }
                    else
                    {
                        t = distance / Math.Abs(velAxis);

                        if (t < remainingFrameFraction)
                            willIntersect = true;

                        #if DEBUG_COLLISION_OBJECT_POLYGON || DEBUG_COLLISION_OBJECT_OBJECT
                        Log.Write("\tCollision time: " + t, Log.DEBUG);
                        #endif
                    }
                }
                #if DEBUG_COLLISION_OBJECT_POLYGON || DEBUG_COLLISION_OBJECT_OBJECT
                else
                {
                    Log.Write("\tMoving the wrong way. No collision.", Log.DEBUG);
                }
                #endif
            }

            //Find the "best" guess of HOW the objects collides.
            //That is, what direction, and what normal was intersected first.
            if ((!result.isIntersecting && !result.hasIntersected) || //If the result intersection flags are both false, this is the first test.
                (result.isIntersecting && (willIntersect || (isIntersecting && result.distance < distance))) || //Previous result was an overlapping one, while the latest result indicate o1 and o2 will collide in the future instead,
                (result.hasIntersected && willIntersect && t > result.collisionTime)) //Previous result was that o1 and o2 collides in the future, but this result indicates that they collide later.
            {
                result.isIntersecting = isIntersecting;
                result.hasIntersected = willIntersect;
                result.collisionTime = t;
                result.distance = distance;
                result.hitNormal = axis;

                #if DEBUG_COLLISION_OBJECT_POLYGON || DEBUG_COLLISION_OBJECT_OBJECT
                Log.Write("\tNew best axis", Log.DEBUG);
                #endif
            }
            //No intersection now or in the future.
            else if (!isIntersecting && !willIntersect)
            {
                result.hasIntersected = false;
                result.isIntersecting = false;
//.........这里部分代码省略.........
开发者ID:hallgeirl,项目名称:Hiage,代码行数:101,代码来源:CollisionManager.cs

示例3: ProjectLine

 private static Projection ProjectLine(Vector p1, Vector p2, Vector axis)
 {
     return new Projection(Math.Min(axis.DotProduct(p1), axis.DotProduct(p2)), Math.Max(axis.DotProduct(p1), axis.DotProduct(p2)), axis);
 }
开发者ID:hallgeirl,项目名称:Hiage,代码行数:4,代码来源:CollisionManager.cs


注:本文中的Engine.Vector.DotProduct方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。