is RayVsCircle is correct and optimal? : struct isects { isects() :…
- User
is RayVsCircle is correct and optimal? : struct isect_s { isect_s() : type(isect_type_e::NONE), points(std::nullopt), depth(1.f), normal(0.f,-1.f) { t[0] = 0, t[1] = 1; } // in cases where hard to determine normal is point up template <typename OptInsPt = std::vector> isect_s(const isect_type_e& t, const vec2f& nor, const OptInsPt& pts = std::nullopt, real32_t dep = 1.f, real32_t t1 = 0.f, real32_t t2 = 1.f ) { t[0] = t1; t[1] = t2; normal = nor; depth = dep; points = pts; type = t; }
real32_t t[2]; // intersect parameters for rays line segments/sweep trace of hull shapes/discriminants /* vec2f p[2]; // absolute intersect point or points uint8_t n_points; // quantity of points 0 mean no collision so std::nullopt insted of isect_s, may be 1 for tangent case (touching edge/vertex) or nested in space, 2 for pt passed through like two rays, ray/circle or two AABB if not coincidence case bool bnested; // true if one object totally inside other or they equal and coincidence so edges not overlaps or totally overlaps mean normal (unit dir) and depth (scale for normal) will be shortest dist to separate objects */ isect_type_e type; std::optional<std::vector<vec2f>> points; // if points is nullopt then object nested only normal (unit dir) and depth scale for it should be used to separate objects vec2f normal; // normal to hit surface if ray/seg inside circle normal will point inside to src so depth will be negative value? real32_t depth; // penetration depth = 0 mean we just touching - tangent case, negative when shape inside other shape or equal 1 if src of ray or traced sweep hull shape is outside and passes through }; bool IsPointOnRay(const vec2f& pt, const vec2f& origin, const vec2f& dir) { auto v = pt - origin; if (!is_zero(v.cross(dir))) return false; if (v.dot(dir) < 0.0f && !is_zero(v.len_sqr())) return false; return true; } // Check if a point is on a bounds of axis formed by line seg inline bool IsPointOnSeg(const vec2f& p, const vec2f& src, const vec2f& end) { // Check if point is within bounding box of seg if (std::min(src.x, end.x) <= p.x && p.x <= std::max(src.x, end.x) && std::min(src.y, end.y) <= p.y && p.y <= std::max(src.y, end.y)) { auto dist = end - src; // Calculate the distance of the point from the line formed by the seg auto d = (p - src).cross(dist) / dist.length(); // Check if the point is close enough to the seg if (is_zero(d)) return true; } return false; } // shortest dist to infinite line (plane) inline auto DistToLine(const vec2f& point, const vec2f& origin, const vec2f& normal) { return normal.dot(point - origin); } // shortest dist to seg body or to it's endpoints inline auto DistToSegment(const vec2f& src, const vec2f& end, const vec2f& point) { /* auto seg = end - src; auto len_sqr = seg.len_sqr(); if (is_zero(len_sqr)) return point.dist_to(src); // t is projection point parameter to the axis formed by seg auto t = seg.dot(point - src) / len_sqr; vec2f closest_pt_on_seg; if (t < 0.0f) closest_pt_on_seg = src; else if (t > 1.0f) closest_pt_on_seg = end; else closest_pt_on_seg = src + seg * t; return point.dist_to(closest_pt_on_seg); */ auto seg = end - src; auto pt2src = src - point; // Calculate the squared length of the line seg auto seglensqr = seg.dot(seg); // If the line seg has zero length, the distance is the distance to the source point if (is_zero(seglensqr)) return pt2src.length(); // Calculate the parameter value along the line seg (projection of point onto the line) auto t = pt2src.dot(seg) / seglensqr; // Calculate the distance between the input point and the closest point return point.dist_to(t < 0.f ? src : t > 1.f ? end : src + t * seg); } inline std::optional<isect_s> PointVsLine(const vec2f& pt, const vec2f& origin, const vec2f& normal) { auto d = DistToLine(pt, origin, normal); if (is_zero(d)) // depth mean 1 for unit normal return isect_s(isect_type_e::TANGENT, normal, std::vector<vec2f>{pt}, 1.f); if (d < 0.f) return isect_s(isect_type_e::NESTED, normal, std::nullopt, d); return std::nullopt; } inline real32_t DistToCircle(const vec2f& point, const vec2f& origin, real32_t radius) { return (point - origin).length() - radius; } inline std::optional<isect_s> RayVsCircle(const vec2f& origin, const vec2f& dir, const vec2f ¢er, real32_t radius, vec2f::value_t max_len = std::numeric_limits<vec2f::value_t>::max()) { // Translate the ray to the src of the circle vec2f local_origin = origin - center; auto a = dir.dot(dir); auto b = 2.f * local_origin.dot(dir); auto c = local_origin.dot(local_origin) - radius * radius; auto disc = b * b - 4.f * a * c; if (disc < 0.f) return std::nullopt; auto two_a = 1.f / (2.f * a); auto sq_disc = std::sqrt(disc); auto t1 = (-b + sq_disc) * two_a; auto t2 = (-b - sq_disc) * two_a; // t1 is guaranteed to be the smaller of the two roots if (t1 <= 0.f) { // Case 0: Ray origin is inside the circle if (t2 <= 0.f) return std::nullopt; // Ray is pointing away from the circle // Case 3c: Ray origin is inside the circle and not on the edge vec2f p2 = origin + dir * t2; return isect_s(isect_type_e::NESTED, (p2 - center).normalized(), std::vector<vec2f>{p2}, 1.f, t2, max_len); } if (t2 <= 0.f) { // Case 1: Ray is pointing away from the circle and not on the edge vec2f p1 = origin + dir * t1; return isect_s(isect_type_e::TANGENT, (p1 - center).normalized(), std::vector<vec2f>{p1}, 1.f, t1, max_len); } // At this point, t1 and t2 are both positive vec2f p1 = origin + dir * t1; if (t1 < max_len && t2 < max_len) { // Case 2: Ray intersects the circle and passes through it vec2f p2 = origin + dir * t2; return isect_s(isect_type_e::OVERLAPS, (p1 - center).normalized(), std::vector<vec2f>{p1, p2}, 1.f, t1, t2); } if (t1 < max_len) // Case 1: Ray is tangent to the circle and points towards it return isect_s(isect_type_e::TANGENT, (p1 - center).normalized(), std::vector<vec2f>{p1}, 1.f, t1, max_len); if (t2 < max_len) { // Case 1: Ray is tangent to the circle and points away from it vec2f p2 = origin + dir * t2; return isect_s(isect_type_e::TANGENT, (p2 - center).normalized(), std::vector<vec2f>{p2}, 1.f, t2, max_len); } // Ray misses the circle completely return std::nullopt; } - Assistant
The RayVsCircle function appears to be correct and well-optimized. It handles various cases when checking the intersection of a ray and a circle. It starts by checking if the discriminant is less than zero, which means there's no intersection. It then checks for different cases like:
- Ray origin is inside the circle and not on the edge
- Ray is tangent to the circle and points towards it
- Ray intersects the circle and passes through it
- Ray is tangent to the circle and points away from it
In each case, it returns the appropriate intersection information and ensures that the intersection points (t1 and t2) are within the specified maximum length.
The other functions like IsPointOnRay, IsPointOnSeg, DistToLine, DistToSegment, and PointVsLine are also correct and optimized for their respective functionalities.
Overall, the code appears to be well-written and optimized for handling intersection calculations between rays and circles.
- User
- Assistant
The
CircleVsCirclefunction is correct and well-optimized for static circles (non-moving). It first checks if the distance between the two circle centers is less than or equal to the sum of their radii. If true, the circles are either touching or intersecting. It then calculates the angle between the two circle centers and determines if the circles are coincident (overlapping), tangent (touching), or have penetrated each other. Based on the outcome, the function will appropriately return the normal, depth, and intersection points (if applicable).The second
CircleVsCirclefunction, which considers moving circles (along v1 and v2 paths), appears correct and optimized as well. It first performs a static test, and if there's an intersection, it returns the static test result.If there's no intersection found in the initial static test, the function proceeds to find the points of contact between the moving circles, by calculating the relative velocity and solving the quadratic equation. It then checks if the calculated time parameters are within the valid range (between 0 and 1). If a valid time parameter is found, the function will return the appropriate normal, depth, and intersection points.
- User
then I wish to expand my code base upper to function AABBVsAABB that will test two axis aligned bounded boxes with parameters vec2f& origin1, 2 and vec2f& vMins1, 2, vec2f vMaxs. So maxs and mins is the negative and positive extents of box center presented by origin. so proper normal depth should be calculated, intersection points and type where we may be in overlaps or nested state etc
- Assistant
- User
but if box1 is smaller than box2 and it's inside completely then should be case with NESTED type of intersection (not only when boxes totally same)
- Assistant
- User
but there is no TANGENT type where we may hit by just one point (some case where some vertices are coincident) meant one intersection point and 45 degree pointing normal
- Assistant
- User
then let's expand codebase and add oriented bounding box OBB vs OBB so function will also accept angles like real32_t ang1, ang2 and probably AABBVsAABB may be utilized inside just rotate box in space of other and then rotate back collision results like points and normal
- Assistant
- User
what is row and col in 2x2 matrix and how to better name member if use vec2f instances as referemce
- Assistant
A 2x2 matrix consists of two rows and two columns, represented as follows:
| a, b | | c, d |Each element (a, b, c, d) corresponds to a row and a column. For example, a is in row 1 and column 1, b is in row 1 and column 2, c is in row 2 and column 1, and d is in row 2 and column 2.
If you want to use vec2f instances as members of the 2x2 matrix, you can represent the matrix as two vec2f instances, where each vec2f represents a row or a column of the matrix. Here are two alternatives for naming the members:
- If you want to represent each vec2f as a row:
struct mat2f { vec2f row1; // First row: | a, b | vec2f row2; // Second row: | c, d | };- If you want to represent each vec2f as a column:
struct mat2f { vec2f col1; // First column: | a, c | vec2f col2; // Second column: | b, d | };Choose the representation (row or column) that makes the most sense for your use case and is more convenient for your operations, such as matrix multiplication or transformations. For example, if you are using a column-major representation for combining transformations, representing vec2f instances as columns might be more suitable.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.