using rays to find the first point that the ray hits.

Sep 10, 2009 at 8:12 PM
Edited Sep 10, 2009 at 9:42 PM

So im almost done getting everything to work but now i ran into a little bug.. It took me a while to realise what the bug actualy was.

I cut down the code so its easier to read. What i want to do is to retrieve the closest collision point directly below the character. I was hoping the the first ".Points" that is retrieved would be the one that the vector would hit first. I believe that this isn't the case due to the bug that im having(altho i could be wrong).. Is there another way of getting the top most collision point efficiently withought having to loop through all the points and find out which is closest by distance?

            float rayX = -RAYOFFSETX;
            for (int i = 0; i < MAXGROUNDRAYS; i++)
            {
                bool colDtct = false;
                groundRay[i] = new Vector2(-rayX, .4999f);
                Vector2 p1 = body_.Position + new Vector2(rayX, 0 );
                Vector2 p2 = body_.Position + new Vector2(rayX, -1.5f);
                List<GeomPointPair> lg = RayHelper.LineSegmentAllGeomsIntersect(ref p1, ref p2, Farseer.Physics, false);
                for (int j = 0; j < lg.Count && colDtct == false; j++)
                {
                    if (!lg[j].Geom.CollisionCategories.Equals(CollisionCategory.Cat1))
                    {
                        groundRay[i] = body_.Position - lg[j].Points[0];
                        deleteSprite.Position = new Vector3(lg[j].Points[0].X, lg[j].Points[0].Y, 1.1f);
                        colDtct = true;
                    }
                }
                //if (groundRay[i] = 
                rayX += RAYOFFSETX;
            }
  •                 bool colDtct = false;
  •                 Vector2 p1 = body_.Position + new Vector2(0, 0 );
  •                 Vector2 p2 = body_.Position + new Vector2(0, -1.5f);
  •                 List<GeomPointPair> lg = RayHelper.LineSegmentAllGeomsIntersect(ref p1, ref p2, Farseer.Physics, false);
  •                 for (int j = 0; j < lg.Count && colDtct == false; j++)
  •                 {
  •                     if (!lg[j].Geom.CollisionCategories.Equals(CollisionCategory.Cat1))
  •                     {
  •                         groundRay = body_.Position - lg[j].Points[0];
  •                         colDtct = true;
  •                     }
  •                 }

if you are wondering why the if statement is there "if (!lg[j].Geom.CollisionCategories.Equals(CollisionCategory.Cat1))". it is to prevent the ray from returning a value from the main player which so happens to be in collision category 1.

 

 

 

Sep 11, 2009 at 12:12 AM

btw i must add in that the rayhelper is KICK ASS!!!!!!  makes life a lot easier and games a  lot cooler!

Sep 11, 2009 at 1:44 PM

I realized that the closest ".Point" to the character will either be the first point (".Point[0]")  or the last one (".Point[Last]") which makes things a lot easier. Gonna try a test and will post my findings.

Sep 11, 2009 at 2:22 PM
Edited Sep 11, 2009 at 2:48 PM

(edit)--it turns out that the GeomPointPair that are listed for the bellow code are also not going to be listed in the array from nearest to p1 to furthest. So it looks like ill have to add a code check for that. If Theres an easier or better way that anyone knows how to achieve this, please share.

------------------------------------------

the following code is good if you want to return the closest point that your ray would hit.. There might be a more efficient method , but this worked great for me.

 

                     Calculator.DistanceBetweenPointAndPoint(p1, gpp.Points[gpp.Points.Count - 1]))

                        anchor2 = gpp.Points[0] - gpp.Geom.Body.Position;
                    else
                        anchor2 = gpp.Points[gpp.Points.Count - 1] - gpp.Geom.Body.Position;
                    body2_ = gpp.Geom.Body;
                    
                    break;
                }
       
  •             List<GeomPointPair> lg = RayHelper.LineSegmentAllGeomsIntersect(ref p1, ref p2, Farseer.Physics, false);
  •             foreach (GeomPointPair gpp in lg)
  •             {
  • if (!gpp.Geom.CollisionCategories.Equals(CollisionCategory.Cat1))        // any CollisionCategory.Cat1 objects will not be detected as a collision
  • {
  •                     if (Calculator.DistanceBetweenPointAndPoint(p1, gpp.Points[0]) < 
  •     Calculator.DistanceBetweenPointAndPoint(p1, gpp.Points[gpp.Points.Count - 1]))
  •                         // closest point is = gpp.Points[0];
  •                     else
  •                          // closest point is = gpp.Points[gpp.Points.Count - 1];         
  •                     break;
  •                 }
  •             }