Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static Bool Intersection(const Line2D& lineA, const Line2D& lineB, Float2& result)
- {
- // Compute the slopes
- Float2 slopeA(lineA.End - lineA.Start), slopeB(lineB.End - lineB.Start);
- // Calculate the intersection offsets
- Float offsetA = (-slopeA.y * (lineA.Start.x - lineB.Start.x) + slopeA.x * (lineA.Start.y - lineB.Start.y)) / (-slopeB.x * slopeA.y + slopeA.x * slopeB.y);
- Float offsetB = (slopeB.x * (lineA.Start.y - lineB.Start.y) - slopeB.y * (lineA.Start.x - lineB.Start.x)) / (-slopeB.x * slopeA.y + slopeA.x * slopeB.y);
- // Find intersection point
- if(offsetA >= 0 && offsetA <= 1 && offsetB >= 0 && offsetB <= 1)
- {
- // Assign result and return
- result = lineA.Start + slopeA * offsetA;
- return true;
- }
- // No intersection found
- return false;
- }
- static Bool Intersection(const Ray2D& ray, const AABB2D& aabb, Float2& result)
- {
- // The ray as a line
- Line2D line;
- line.Start = ray.Position;
- line.End = ray.Position + ray.Direction * (Length(aabb.Position - ray.Position) + Length(aabb.HalfSize * 2));
- // AABB sides
- Line2D top(aabb.Position + aabb.HalfSize, aabb.Position + aabb.HalfSize * Float2(-1, 1));
- Line2D left(aabb.Position - aabb.HalfSize, aabb.Position + aabb.HalfSize * Float2(1, -1));
- Line2D down(aabb.Position - aabb.HalfSize, aabb.Position + aabb.HalfSize * Float2(1, -1));
- Line2D right(aabb.Position - aabb.HalfSize, aabb.Position + aabb.HalfSize * Float2(1, -1));
- Float2 out;
- // Check the four sides and return if one hits ¯\_(ツ)_/¯
- if(Intersection(line, top, out)) { result = out; return true; }
- if(Intersection(line, left, out)) { result = out; return true; }
- if(Intersection(line, down, out)) { result = out; return true; }
- if(Intersection(line, right, out)) { result = out; return true; }
- // No intersection
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement