Example #1
0
        /// <summary>
        /// Returns all overlapping objects of a given ray with the physics world, the ray does NOT stop at solid hit but will report all overlaps and solid hits on the ray
        /// </summary>
        /// <param name="ray">Ray to test against</param>
        /// <param name="length">Maximum distance to test</param>
        /// <param name="group">Collision Group to use for the ray anything above "NoSolver" is ignored</param>
        /// <param name="outResults">All overlaps on the ray</param>
        /// <returns>True if any object was hit</returns>
        public bool RayOverlap(ref Ray ray, float length, CollisionGroup group, List <SRaycastResult> outResults)
        {
            List <RayCastResult> physicsResults = new List <RayCastResult>();

            if (m_physicSpace.RayCast(ray.ToBepu(), length, GetOverlapFilter(group), physicsResults))
            {
                foreach (RayCastResult physicsResult in physicsResults)
                {
                    if (physicsResult.HitObject.Tag is CEntity gameEntity)
                    {
                        CollisionRule  rule       = GetCollisionRuleWithGroup(physicsResult.HitObject, group);
                        SRaycastResult gameResult = new SRaycastResult()
                        {
                            HitEntity   = gameEntity,
                            Location    = physicsResult.HitData.Location.ToSharp(),
                            Normal      = physicsResult.HitData.Normal.ToSharp(),
                            Distance    = physicsResult.HitData.T,
                            bIsSolidHit = rule <= CollisionRule.Normal
                        };
                        outResults.Add(gameResult);
                    }
                }
                return(outResults.Count > 0);
            }

            return(false);
        }
Example #2
0
        /// <summary>
        /// Returns all overlapping objects of a given ray with the physics world, as no rules are given to this function all hits will be reported as non solid
        /// </summary>
        /// <param name="ray">Ray to test against</param>
        /// <param name="length">Maximum distance to test</param>
        /// <param name="outResults">All overlaps on the ray</param>
        /// <returns>True if any object was hit</returns>
        public bool RayOverlap(ref Ray ray, float length, List <SRaycastResult> outResults)
        {
            List <RayCastResult> physicsResults = new List <RayCastResult>();

            if (m_physicSpace.RayCast(ray.ToBepu(), length, physicsResults))
            {
                foreach (RayCastResult physicsResult in physicsResults)
                {
                    if (physicsResult.HitObject.Tag is CEntity gameEntity)
                    {
                        SRaycastResult gameResult = new SRaycastResult()
                        {
                            HitEntity   = gameEntity,
                            Location    = physicsResult.HitData.Location.ToSharp(),
                            Normal      = physicsResult.HitData.Normal.ToSharp(),
                            Distance    = physicsResult.HitData.T,
                            bIsSolidHit = false
                        };
                        outResults.Add(gameResult);
                    }
                }
                return(outResults.Count > 0);
            }

            return(false);
        }
Example #3
0
        public static bool Test(SharpDX.Ray ray, Vector3 a, Vector3 b, Vector3 c, out float u, out float v, out float w)
        {
            Vector3 ab = b - a;
            Vector3 ac = c - a;
            Vector3 qp = ray.Position - (ray.Position + ray.Direction);

            // Compute triangle normal. Can be precalculated or cached if
            // intersecting multiple segments against the same triangle
            Vector3 n = Vector3.Cross(ab, ac);

            // Compute denominator d. If d <= 0, segment is parallel to or points
            // away from triangle, so exit early
            float d = Vector3.Dot(qp, n);

            if (d <= 0.0f)
            {
                u = 0; v = 0; w = 0;
                return(false);
            }
            // Compute intersection t value of pq with plane of triangle. A ray
            // intersects iff 0 <= t. Segment intersects iff 0 <= t <= 1. Delay
            // dividing by d until intersection has been found to pierce triangle
            Vector3 ap = ray.Position - a;
            float   t  = Vector3.Dot(ap, n);

            if (t < 0.0f)
            {
                u = 0; v = 0; w = 0;
                return(false);
            }

            // Compute barycentric coordinate components and test if within bounds
            Vector3 e = Vector3.Cross(qp, ap);

            v = Vector3.Dot(ac, e);
            if (v < 0.0f || v > d)
            {
                u = 0; w = 0;
                return(false);
            }
            w = -Vector3.Dot(ab, e);
            if (w < 0.0f || v + w > d)
            {
                u = 0;
                return(false);
            }
            // Segment/ray intersects triangle. Perform delayed division and
            // compute the last barycentric coordinate component
            float ood = 1.0f / d;

            t *= ood;
            v *= ood;
            w *= ood;
            u  = 1.0f - v - w;
            return(true);
        }
Example #4
0
        public static bool Test(BoundingSphere s0, BoundingSphere s1, Vector3 v0, Vector3 v1, out float t)
        {
            // expand sphere s1 by the radius of s0
            s1.Radius += s0.Radius;

            // subtract movement of s1 from both s0 and s1, making s1 stationary
            Vector3 v = v0 - v1;

            // can now test directed segment s = s0.Center + v*t, v = (v0 - v1)/||v0 - v1|| against the expanded sphere for intersection
            float vlen = v.Length();
            SharpDX.Ray s = new SharpDX.Ray(s0.Center, v / vlen);
            if (s1.sSphere.Intersects(ref s, out t))
                return t <= vlen;

            return false;
        }
Example #5
0
        private void OnInputEvent(ReadOnlyCollection <SInputButtonEvent> buttonevents, string textinput)
        {
            foreach (var buttonEvent in buttonevents)
            {
                if (buttonEvent.button == EInputButton.MouseMiddleButton && buttonEvent.buttonEvent == EButtonEvent.Pressed)
                {
                    CViewManager viewManager = m_gameWorld.ViewManager;
                    int          mouseAbsX   = System.Windows.Forms.Cursor.Position.X - (int)viewManager.ScreenLeft;
                    int          mouseAbsY   = System.Windows.Forms.Cursor.Position.Y - (int)viewManager.ScreenTop;

                    if (mouseAbsX < 0 || mouseAbsY < 0)
                    {
                        return;
                    }

                    viewManager.GetViewInfo(out SSceneViewInfo viewInfo);
                    Ray pickRay = Ray.GetPickRay(mouseAbsX, mouseAbsY, new ViewportF(0, 0, viewManager.ScreenWidth, viewManager.ScreenHeight), viewInfo.ViewMatrix * viewInfo.ProjectionMatrix);
                    if (m_physicSpace.RayCast(pickRay.ToBepu(), 9999.0f, out RayCastResult result))
                    {
                        float fieldRadius     = 5.0f;
                        float impulseStrength = 20.0f;
                        List <BroadPhaseEntry> queryResults = new List <BroadPhaseEntry>();
                        BoundingSphere         bs           = new BoundingSphere(result.HitData.Location, fieldRadius);
                        m_physicSpace.BroadPhase.QueryAccelerator.GetEntries(bs, queryResults);
                        foreach (var entry in queryResults)
                        {
                            var entityCollision = entry as EntityCollidable;
                            if (entityCollision != null)
                            {
                                var e = entityCollision.Entity;
                                if (e.IsDynamic)
                                {
                                    Vector3 toEntity = e.Position - result.HitData.Location;
                                    float   length   = toEntity.Length();
                                    float   strength = impulseStrength / length;
                                    toEntity.Y = 1.0f;
                                    toEntity.Normalize();
                                    e.ApplyImpulse(e.Position, toEntity * strength);
                                }
                            }
                        }
                    }
                }
            }
        }
Example #6
0
        /// <summary>
        /// Returns the first hit of a given ray tested against the physics world, does filtering based on the given function
        /// </summary>
        /// <param name="ray">Ray to test against</param>
        /// <param name="length">Maximum distance to test</param>
        /// <param name="filter">Filter to check if an object is valid to hit</param>
        /// <param name="outHitResult">The hit that was found if any</param>
        /// <returns>True if any object was hit</returns>
        public bool Raycast(ref Ray ray, float length, Func <BroadPhaseEntry, bool> filter, ref SRaycastResult outHitResult)
        {
            if (m_physicSpace.RayCast(ray.ToBepu(), length, filter, out RayCastResult result))
            {
                if (result.HitObject.Tag is CEntity gameEntity)
                {
                    outHitResult.HitEntity = gameEntity;
                    outHitResult.Location  = result.HitData.Location.ToSharp();
                    outHitResult.Normal    = result.HitData.Normal.ToSharp();
                    outHitResult.Distance  = result.HitData.T;
                    return(true);
                }

                return(false);
            }

            return(false);
        }
Example #7
0
        public static bool Test(BoundingSphere s0, BoundingSphere s1, Vector3 v0, Vector3 v1, out float t)
        {
            // expand sphere s1 by the radius of s0
            s1.Radius += s0.Radius;

            // subtract movement of s1 from both s0 and s1, making s1 stationary
            Vector3 v = v0 - v1;

            // can now test directed segment s = s0.Center + v*t, v = (v0 - v1)/||v0 - v1|| against the expanded sphere for intersection
            float vlen = v.Length();

            SharpDX.Ray s = new SharpDX.Ray(s0.Center, v / vlen);
            if (s1.sSphere.Intersects(ref s, out t))
            {
                return(t <= vlen);
            }

            return(false);
        }
Example #8
0
        public static void Render(SharpDX.Direct3D11.DeviceContext context)
        {
            //we apply wvp here to update z
            var origin    = (Origin * WVP).TranslationVector;
            var direction = (Direction * WVP).TranslationVector;
            var ray       = new SharpDX.Ray(origin, direction);


            //if ray is inside panel, show
            if (AdjustmentPanelModel.CheckBounds(ref ray, out var z))
            {
                var k = Direction.TranslationVector.Length();
                vertices[5] = -z * k;
                shader.Apply(context);
                context.UpdateSubresource(vertices, vertexBuffer);
                context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;
                context.InputAssembler.SetVertexBuffers(0, vertexBufferBinding);
                context.Draw(2, 0);
            }
        }
Example #9
0
 public SLine(Vector3 p, Vector3 dir)
 {
     point = p;
     rp = new SharpDX.Ray(p, dir);
     rn = new SharpDX.Ray(p, -dir);
 }
Example #10
0
 public SLine(SharpDX.Ray ray)
 {
     point = ray.Position;
     rp = ray;
     rn = new SharpDX.Ray(ray.Position, -ray.Direction);
 }
Example #11
0
 public Ray(SharpDX.Ray ray)
 {
     this.sray = ray;
 }
Example #12
0
 /// <summary>
 /// Determines if there is an intersection between the current object and a <see cref="SharpDX.Ray"/>.
 /// </summary>
 /// <param name="ray">The ray to test.</param>
 /// <param name="distance">When the method completes, contains the distance of the intersection,
 /// or 0 if there was no intersection.</param>
 /// <returns>Whether the two objects intersected.</returns>
 public bool Intersects(ref Ray ray, out float distance)
 {
     return(Collision.RayIntersectsBox(ref ray, ref this, out distance));
 }
Example #13
0
 /// <summary>
 /// Returns the first hit of a given ray tested against the physics world, does filtering based on the given CollisionGroup only solid collisions will be reported
 /// </summary>
 /// <param name="ray">Ray to test against</param>
 /// <param name="length">Maximum distance to test</param>
 /// <param name="group">Collision Group to use for the ray, anything above "Normal" is ignored</param>
 /// <param name="outHitResult">The hit that was found if any</param>
 /// <returns>True if any object was hit</returns>
 public bool Raycast(ref Ray ray, float length, CollisionGroup group, ref SRaycastResult outHitResult)
 {
     return(Raycast(ref ray, length, GetSolidFilter(group), ref outHitResult));
 }
Example #14
0
        /// <summary>
        /// Returns the first hit of a given ray tested against the physics world, does filtering based on the given CollisionRules only solid collisions will be reported
        /// </summary>
        /// <param name="ray">Ray to test against</param>
        /// <param name="length">Maximum distance to test</param>
        /// <param name="raycastRules">Collision Rules to use for the ray, anything above "Normal" is ignored</param>
        /// <param name="outHitResult">The hit that was found if any</param>
        /// <returns>True if any object was hit</returns>
        public bool Raycast(ref Ray ray, float length, CollisionRules raycastRules, ref SRaycastResult outHitResult)
        {
            SimpleCollisionRulesOwner rulesOwner = new SimpleCollisionRulesOwner(raycastRules);

            return(Raycast(ref ray, length, GetSolidFilter(rulesOwner), ref outHitResult));
        }
Example #15
0
 /// <summary>
 /// Determines if there is an intersection between the current object and a <see cref="SharpDX.Ray"/>.
 /// </summary>
 /// <param name="ray">The ray to test.</param>
 /// <param name="point">When the method completes, contains the point of intersection,
 /// or <see cref="SharpDX.Vector3.Zero"/> if there was no intersection.</param>
 /// <returns>Whether the two objects intersected.</returns>
 public bool Intersects(ref Ray ray, out Vector3 point)
 {
     return(Collision.RayIntersectsBox(ref ray, ref this, out point));
 }
Example #16
0
 public Line(SharpDX.Ray ray)
 {
     this.Point = ray.Position;
     this.rp = ray;
     this.rn = new SharpDX.Ray(ray.Position, -ray.Direction);
 }
Example #17
0
 public Ray(SharpDX.Ray ray)
 {
     this.sray = ray;
 }
Example #18
0
 public Line(Vector3 point, Vector3 direction)
 {
     this.Point = point;
     this.rp = new SharpDX.Ray(point, direction);
     this.rn = new SharpDX.Ray(point, -direction);
 }
Example #19
0
 public SLine(Vector3 p, Vector3 dir)
 {
     point = p;
     rp    = new SharpDX.Ray(p, dir);
     rn    = new SharpDX.Ray(p, -dir);
 }
Example #20
0
 public Ray(Vector3 origin, Vector3 direction)
 {
     Origin = origin;
     Direction = direction;
     _sharpDx = new SharpDX.Ray(origin, direction);
 }
Example #21
0
 public Line(SharpDX.Ray ray)
 {
     this.Point = ray.Position;
     this.rp    = ray;
     this.rn    = new SharpDX.Ray(ray.Position, -ray.Direction);
 }
Example #22
0
 public Ray(Vector3 point, Vector3 direction)
 {
     this.sray = new SharpDX.Ray(point, direction);
 }
Example #23
0
 public SLine(SharpDX.Ray ray)
 {
     point = ray.Position;
     rp    = ray;
     rn    = new SharpDX.Ray(ray.Position, -ray.Direction);
 }