Calculate normal from RayHelpers GeomPointPair?

Topics: User Forum
Oct 8, 2009 at 8:55 AM

Hi,

Is there an easy way to get the normal from the GeomPointPair that is returned from RayHelper?

//AlfonsA

 

Coordinator
Oct 8, 2009 at 4:05 PM

What is it that you want the normal from? (the ray, geometry, other...)

Oct 10, 2009 at 7:34 AM
Edited Oct 10, 2009 at 7:37 AM

 

This is how I implemented it.

public float normalRadians; private LineBrush _rayLineBrush; private Color _rayLineColor = new Color(200, 0, 0, 150); private Vector2 leftRayStart = new Vector2(-20, -30); private Vector2 leftRayEnd = new Vector2(-20, 220); private Vector2 rightRayStart = new Vector2(20, -30); private Vector2 rightRayEnd = new Vector2(20, 220); public Vector2 leftLeg = Vector2.Zero; public Vector2 rightLeg = Vector2.Zero;

In Update()

Vector2 p1 = leftRayStart + Body.Position;
            Vector2 p2 = leftRayEnd + Body.Position;
            List<GeomPointPair> lg = RayHelper.LineSegmentAllGeomsIntersect(ref p1, ref p2, physicsSimulator, false);
            foreach (GeomPointPair gpp in lg)
             {
                if (!gpp.Geom.CollisionCategories.Equals(CollisionCategory.Cat7))        // any CollisionCategory.Cat1 objects will not be detected as a collision
                    {
                        Vector2 firstPoint = gpp.Points[0];
                        Vector2 lastPoint = gpp.Points[gpp.Points.Count - 1];
                        if (Calculator.DistanceBetweenPointAndPoint(ref p1, ref firstPoint) < Calculator.DistanceBetweenPointAndPoint(ref p1, ref lastPoint))
                        {
                            // closest point is = gpp.Points[0];
                            leftLeg = firstPoint;
                        }
                        else
                        {
                            // closest point is = gpp.Points[gpp.Points.Count - 1];         
                            leftLeg = lastPoint;
                        }
                 }
             }

            p1 = rightRayStart + Body.Position;
            p2 = rightRayEnd + Body.Position;

            lg = RayHelper.LineSegmentAllGeomsIntersect(ref p1, ref p2, physicsSimulator, false);
            foreach (GeomPointPair gpp in lg)
            {
                if (!gpp.Geom.CollisionCategories.Equals(CollisionCategory.Cat7))        // any CollisionCategory.Cat1 objects will not be detected as a collision
                {
                    Vector2 firstPoint = gpp.Points[0];
                    Vector2 lastPoint = gpp.Points[gpp.Points.Count - 1];
                    if (Calculator.DistanceBetweenPointAndPoint(ref p1, ref firstPoint) < Calculator.DistanceBetweenPointAndPoint(ref p1, ref lastPoint))
                    {
                        // closest point is = gpp.Points[0];
                        rightLeg = firstPoint;
                    }
                    else
                    {
                        // closest point is = gpp.Points[gpp.Points.Count - 1];         
                        rightLeg = lastPoint;
                    }
                }
            }

            Vector2 normal = Vector2.Subtract(rightLeg, leftLeg);
            normalRadians = (float)Math.Atan2(normal.X, normal.Y);
      

In Draw()

            _rayLineBrush.Draw(screenManager.SpriteBatch, screenManager.GetScreenPosition(characterBody.Position + rightRayStart),
                    screenManager.GetScreenPosition(characterBody.Position + rightRayEnd));
            _rayLineBrush.Draw(screenManager.SpriteBatch, screenManager.GetScreenPosition(characterBody.Position + leftRayStart),
                              screenManager.GetScreenPosition(characterBody.Position + leftRayEnd));


ScreenManager.SpriteBatch.DrawString(_spriteFont,
                                          "R",
                                          ScreenManager.Camera.GetScreenPosition(rightLeg), Color.Red);
                    ScreenManager.SpriteBatch.DrawString(_spriteFont,
                                          "L",
                                          ScreenManager.Camera.GetScreenPosition(leftLeg), Color.Red);