Reflected Raycast isssue

Hi i've been having some issues getting raycasting reflection working in my game. I have it working to raycast from a point to the border 10px from the edge around the screen. I then calculate the point for the reflected ray from the point on the border. I raycast the reflected ray from the border, but on some areas the raycast returns the exact same point. I can show in this picture where the reflected raycast returns the same point, Image. The area where a line is drawn (from the center of the screen) is where the reflected raycast works, and will draw properly to another point. The area where it is not drawn is where the reflected raycast returns the same point.

My code for the raycast callback is

Code: Select all

class RayCastAnyCallback : public b2RayCastCallback
{
public:
    RayCastAnyCallback()
    {
        m_hit = false;
    }
    float32 ReportFixture(  b2Fixture* fixture, const b2Vec2& point,
                          const b2Vec2& normal, float32 fraction)
    {

        b2Body* body = fixture->GetBody();
        void* userData = body->GetUserData();
        if(userData)
        {
            CCSprite *herp = (CCSprite*)userData;
            if (herp.tag != 666) //the border has sprite tag 666, so we filter everything but the border
            {
                // filter
                return -1.0f;
            }
        }

        m_hit = true;
        m_point = point;
        m_normal = normal;
        return 0.0f;
    }

    bool m_hit;
    b2Vec2 m_point;
    b2Vec2 m_normal;
    b2Body* m_bodySource;
};


my code for the raycasting is

Code: Select all

-(lineSegment)returnLineCollision:(b2Body*)rayBody p1:(b2Vec2)point1 p2:(b2Vec2)point2
{
    RayCastAnyCallback callback;
    if(point1 == b2Vec2_zero && point2 == b2Vec2_zero){
        b2Vec2 velocity = rayBody->GetLinearVelocity();
        float currentAngle = [self returnVelocityAngle:velocity];
        currentAngle = [self angleBetweenTwoPoints:initialTouch1 secondPoint:currentTouch1];
        point1 = rayBody->GetPosition();
        point2 = point1 + (100 * b2Vec2( sinf(currentAngle), cosf(currentAngle)));
    }
   world->RayCast(&callback, point1, point2);

    point2 = callback.m_point;
    b2Vec2 n = callback.m_normal;
    lineSegment returnVal;
    returnVal.x = point2.x;
    returnVal.y = point2.y;
    returnVal.nx = n.x;
    returnVal.ny = n.y;
    return returnVal;
}


and my code to find the reflected ray is

Code: Select all

-(b2Vec2)returnReflectedRay:(b2Vec2)p1 point2:(b2Vec2)p2 normal:(b2Vec2)n
{
    float a = p2.x - p1.x;
    float b = p2.y - p1.y;
    b2Vec2 v = b2Vec2(a, b);
    float32 dot = b2Dot(n, v);
    b2Vec2 r2 = 2.0f * dot * n;
    b2Vec2 r3 = v - r2;
    b2Vec2 reflectP1 = b2Vec2((r3.x*10.0f + p2.x),(r3.y*10.0f + p2.y));
    return reflectP1;
}


My question is why does the raycast only work sometimes?
Is that border made of edge shapes? If so I think the problem is that the second ray's starting point is sometimes outside the border, and sometimes inside it, due to floating point precision.
You could try making the border from polygons, or move the starting point for the 'returning' raycast to make sure it is inside the border.

Original thread: https://box2d.org/forum/viewtopic.php?t=7995