Advertisement

HLSL Minimap!

Started by April 03, 2018 05:21 PM
1 comment, last by MJP 6 years, 10 months ago

I am working on making a shader driven minimap for a game. I have almost completed this task but I am stuck on one part. I need to make sure that when I track something (on the minimap) and it goes beyond the extent of my viewport (perimeter of minimap) it sticks to the edge of the viewport and does not continue past it. 

This is relatively straight forward for a circle minimap, however, mine is a rectangle. Not sure how to work out the math on this one. 

Any ideas??

So assuming your rectangle isn't rotated, then your minimap bounds essentially form an axis-aligned bounding box (AABB). Now if you have an item on your minimap, you can compute a vector (ray) starting at the minimap center that points towards the item. You then basically need to intersect that ray with the AABB to determine where to put the item if it goes past the bounds of the map. If you google for "ray AABB intersection", you should be able to find some articles that explain how to compute the intersection point. Here's the code that I've used in the past:
 


float IntersectRayBox2D(float2 rayOrg, float2 dir, float2 bbmin, float2 bbmax)
{
    float2 invDir = rcp(dir);
    float2 d0 = (bbmin - rayOrg) * invDir;
    float2 d1 = (bbmax - rayOrg) * invDir;

    float2 v0 = min(d0, d1);
    float2 v1 = max(d0, d1);

    float tmin = max(v0.x, v0.y);
    float tmax = min(v1.x, v1.y);

    return tmin >= 0.0f ? tmin : tmax;
}

It will return a "t" value such that "rayOrg + dir * t" will give you the point of intersection.

This topic is closed to new replies.

Advertisement