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


C# IVehicle.PredictFuturePosition方法代码示例

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


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

示例1: SteerToAvoid

        // xxx couldn't this be made more compact using localizePosition?
        /// <summary>
        /// Checks for intersection of the given spherical obstacle with a
        /// volume of "likely future vehicle positions": a cylinder along the
        /// current path, extending minTimeToCollision seconds along the
        /// forward axis from current position.
        ///
        /// If they intersect, a collision is imminent and this function returns
        /// a steering force pointing laterally away from the obstacle's center.
        ///
        /// Returns a zero vector if the obstacle is outside the cylinder
        /// </summary>
        /// <param name="v"></param>
        /// <param name="minTimeToCollision"></param>
        /// <returns></returns>
        public Vector3 SteerToAvoid(IVehicle v, float minTimeToCollision)
        {
            // Capsule x Sphere collision detection
            //http://www.altdev.co/2011/04/26/more-collision-detection-for-dummies/

            var capStart = v.Position;
            var capEnd = v.PredictFuturePosition(minTimeToCollision);

            var alongCap = capEnd - capStart;
            var capLength = alongCap.Length();

            //If the vehicle is going very slowly then simply test vehicle sphere against obstacle sphere
            if (capLength <= 0.05)
            {
                var distance = Vector3.Distance(Center, v.Position);
                if (distance < Radius + v.Radius)
                    return (v.Position - Center);
                return Vector3.Zero;
            }

            var capAxis = alongCap / capLength;

            //Project vector onto capsule axis
            var b = Utilities.Clamp(Vector3.Dot(Center - capStart, capAxis), 0, capLength);

            //Get point on axis (closest point to sphere center)
            var r = capStart + capAxis * b;

            //Get distance from circle center to closest point
            var dist = Vector3.Distance(Center, r);

            //Basic sphere sphere collision about the closest point
            var inCircle = dist < Radius + v.Radius;
            if (!inCircle)
                return Vector3.Zero;

            //avoidance vector calculation
            Vector3 avoidance = Vector3Helpers.PerpendicularComponent(v.Position - Center, v.Forward);
            //if no avoidance was calculated this is because the vehicle is moving exactly forward towards the sphere, add in some random sideways deflection
            if (avoidance == Vector3.Zero)
                avoidance = -v.Forward + v.Side * 0.01f * RandomHelpers.Random();

            avoidance = Vector3.Normalize(avoidance);
            avoidance *= v.MaxForce;
            avoidance += v.Forward * v.MaxForce * 0.75f;
            return avoidance;
		}
开发者ID:cupsster,项目名称:SharpSteer2,代码行数:62,代码来源:SphericalObstacle.cs


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