0
\$\begingroup\$

Wanting to know how to figure out if player's bottom is colliding with object so that I could allow the player to jump. I was trying to use OnCollision but was referred to ray-casting, I looked through the Testbed of Farseer to try and understand ray-casting better but am still lost.

I know what each return for the method does but how would I use it for player collision?

In this answer the person uses this function to find out if player is in the air. Is this the best way to find out if player is colliding with the ground?

In the testbed there is a fixture called f, two vectors call p and n, and a float called fr. What do these four things do?

Example:

case RayCastMode.Closest:
    bool hitClosest = false;
    World.RayCast((f, p, n, fr) =>
    {
        Body body = f.Body;
        if (body.UserData != null)
        {
              int index = (int)body.UserData;
              if (index == 0)
              {
                  // filter
                  return -1.0f;
              }
        }

        hitClosest = true;
        point = p;
        normal = n;
        return fr;
    }, point1, point2);

    if (hitClosest)
    {
         DebugView.BeginCustomDraw(ref GameInstance.Projection, ref GameInstance.View);
                    DebugView.DrawPoint(point, .5f, new Color(0.4f, 0.9f, 0.4f));

                    DebugView.DrawSegment(point1, point, new Color(0.8f, 0.8f, 0.8f));

                    Vector2 head = point + 0.5f * normal;
    DebugView.DrawSegment(point, head, new Color(0.9f, 0.9f, 0.4f));
    DebugView.EndCustomDraw();
    }
    else
    {
        DebugView.BeginCustomDraw(ref GameInstance.Projection, ref GameInstance.View);
        DebugView.DrawSegment(point1, point2, new Color(0.8f, 0.8f, 0.8f));
        DebugView.EndCustomDraw();
    }

break;

Also Were is World.RayCast Coming from?

Note

If some one has a better way of detecting collision I would love to hear it.

EDIT: 3/7/14

I found that World.RayCast is located in the testbed in the Farseer Physics XNA project, Collision Folder, Collision.cs. The RayCastInput and Output are located on lines 200, and the bool on 444.

So if there located in the engine then I should be able to call on the function when I make my own project with it.

I make a class called Ray.

I then add the following code:

using System;
using System.Collections.Generic;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using FarseerPhysics.Collision;

namespace FarseerPhysics.TestBed.Tests
{
    public class Ray
    {
        private float angle;

        public override void Update(GameTime gameTime)
        {
            const float l = 11.0f;
            Vector2 point1 = new Vector2(0.0f, 10.0f);
            Vector2 d = new Vector2(l * (float)Math.Cos(angle), l * (float)Math.Sin(angle));
            Vector2 point2 = point1 + d;

            Vector2 point = Vector2.Zero, normal = Vector2.Zero;

            World.RayCast((f, p, n, fr) =>
            {
                Body body = f.Body;
                if (body.UserData != null)
                {
                    int index = (int)body.UserData;
                    if (index == 0)
                    {
                        return -1.0f;
                    }
                }
                point = p;
                normal = n;
                return fr;
            }, point1, point2);
        }
    }
}

I create this little thing but World.RayCast has an error:

An object reference is required for the non-static field, method, or property 'FarseerPhysics.Dynamics.World.RayCast(System.Func<FarseerPhysics.Dynamics.Fixture,Microsoft.Xna.Framework.Vector2,Microsoft.Xna.Framework.Vector2,float,float>, Microsoft.Xna.Framework.Vector2, Microsoft.Xna.Framework.Vector2)'

What have I done wrong?

\$\endgroup\$
0
\$\begingroup\$

I've been looking at your code for ten minutes now and I believe I have found your mistake. The Farseer engine uses a Func<,,,> but you're trying to use a delegate. As far as I can tell a delegate cannot be automatically casted to a Func<,,,>.

Just to be on the safe side I've copy/pasted my own RayCasting code (right from a working application) so that you might even be able to figure it out if this wasn't the error.

Note that my code always returns the first collision (my own class) so you might need to modify it a bit.

public Collision RayCastFirst(Vector2 point_0, Vector2 point_1)
{
    Collision collision = null;
    Func<Fixture, Vector2, Vector2, float, float> get_first_callback = delegate(Fixture fixture, Vector2 point, Vector2 normal, float fraction)
    {
        collision = new Collision(fixture, point, normal);
        return 0;
    };

    // Summary:
    //     Ray-cast the world for all fixtures in the path of the ray. Your callback
    //     controls whether you get the closest point, any point, or n-points.  The
    //     ray-cast ignores shapes that contain the starting point.  Inside the callback:
    //     return -1: ignore this fixture and continue 
    //     return  0: terminate the ray cast
    //     return fraction: clip the ray to this point
    //     return 1:        don't clip the ray and continue
    world.RayCast(get_first_callback, point_0, point_1);
    return collision;
}
\$\endgroup\$
  • \$\begingroup\$ Thank you for the help, at the moment I can not say this is the correct answer I was looking for, the reason in that my team said that using farseer would take to much time and has gone to use a tile map to make it easier for them to make the game. \$\endgroup\$ – Ryan Foy Mar 17 '14 at 14:16
  • \$\begingroup\$ A tile map? So you didn't need physics at all then? \$\endgroup\$ – Roy T. Mar 17 '14 at 15:36
  • \$\begingroup\$ I was using the physics but then needed to jump over to a tile map because of the time constraints I would have used these things but again, time is running short so I can't do anything fancy. \$\endgroup\$ – Ryan Foy Mar 18 '14 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.