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?