prime31 / Nez

Nez is a free 2D focused framework that works with MonoGame and FNA
MIT License
1.76k stars 355 forks source link

RaycastHit distance returning incorrect value #62

Closed piedoom closed 8 years ago

piedoom commented 8 years ago

Hopefully I'm not going about this the wrong way, but I have a simple platformer game. The player uses raycasting to figure out where solid ground is.

           if (hit.collider != null)
            {
                velocity.Y = (hit.distance) * directionY;
                rayLength = hit.distance;

                collisions.below = directionY == 1;
                collisions.above = directionY == -1;
            }

Basically, the player will move to contact the block by setting the velocity to the remaining space.

However, when I inspect hit.distance, it doesn't seem to be the distance of the ray starting at the origin, but the distance from the top-left of the screen.

Hopefully I'm not just using this wrong/misunderstanding.

prime31 commented 8 years ago

What are the actual start and end values you are passing to the linecast method? Have you tried logging them to make sure they are what you expect?

piedoom commented 8 years ago

From what I understand, the linecast method is an absolute "point A" to "point B" draw - I have it formatted like this -

Physics.linecast(rayOrigin, rayOrigin + (Vector2.UnitY * directionY * rayLength));

Drawing the linecasts with Debug.drawLine gives the visual result I expect.

prime31 commented 8 years ago

I whipped up a little tester for this one:

var playerDude = scene.findEntity( "player-moon" );
if( playerDude != null )
{
    var start = playerDude.transform.position + new Vector2( 64f, 0f );
    var end = playerDude.transform.position + new Vector2( 256f, 0f );
    Debug.drawLine( start, end, Color.Black, 2f );
    var hit = Physics.linecast( start, end );
    if( hit.collider != null )
    {
        Debug.log( "ray HIT {0}, collider: {1}", hit, hit.collider.entity );
    }
}

Everything looks OK. Logs look like this:

Log: ray HIT [RaycastHit] fraction: 0.84375, distance: 162, normal: {X:-1 Y:0}, centroid: {X:0 Y:0}, point: {X:-64 Y:-29}, collider: [Entity: name: moon2, tag: 0, enabled: True, depth: 0]

And the debug view looks like this:

screen shot 2016-07-11 at 9 44 55 pm

Do you have a stripped down version that you can send over that reproduces the issue?

piedoom commented 8 years ago

On a closer look, it seems as though I'm making contact initially and correctly, but then completely passing through the object and getting erroneous measurements. I'm going to do a little more fiddling and see what I can come up with.

piedoom commented 8 years ago

I've created the following gist as a barebones example of the problem I'm having -

https://gist.github.com/piedoom/164969239e2b4bf047768cb9b27c2f54

Running this with debug render creates a few colliders and an entity that does a couple of raycasts. You can move the raycaster object by clicking down and dragging.

For some reason, the hit distance is correct for the raycast going to the right, but not correct for the raycast going downwards. I don't think this is my error since the Debug.drawLine functions appear to look correct.

Example output when 50px away in both directions: hit distance right: 50 hit distance bottom: 434.0069 (or some other odd number)


PS - I just wanted to commend you on some excellent work. I switched over to Monogame after getting frustrated with Unity's rather overcomplicated 2D system, and I'm glad I found this project. It's an excellent framework.

prime31 commented 8 years ago

Glad to hear you are liking Nez!

The repro helped identify the issue in not time. It also uncovered one other bug that is now fixed.

piedoom commented 8 years ago

Great to know! Thanks for the support.