Calculates Euclidean distance between two points (sqrt(dx^2+dy^2), integer result, use for proximity checks).
Takes x1 (first point X coordinate, integer), y1 (first point Y coordinate, integer), x2 (second point X coordinate, integer), y2 (second point Y coordinate, integer).
Returns distance (integer pixels, rounded from sqrt, 0=same point).
2D Overlay
Parameters & Returns
Parameters
x1Int
y1Int
x2Int
y2Int
Returns
Int
Quick Summary
Calculates Euclidean distance between two points (sqrt(dx^2+dy^2), integer result, use for proximity checks).
Takes x1 (first point X coordinate, integer), y1 (first point Y coordinate, integer), x2 (second point X coordinate, integer), y2 (second point Y coordinate, integer).
Returns distance (integer pixels, rounded from sqrt, 0=same point).
Technical Exegesis...
Calculates Euclidean distance between two points (sqrt(dx^2+dy^2), integer result, use for proximity checks). Takes x1 (first point X coordinate, integer), y1 (first point Y coordinate, integer), x2 (second point X coordinate, integer), y2 (second point Y coordinate, integer). Returns distance (integer pixels, rounded from sqrt, 0=same point). Calculates delta (dx=x2-x1, dy=y2-y1), computes distance squared (dx^2+dy^2), applies square root (std::sqrt), casts to integer (truncates fractional part).
Calculates Euclidean distance between two points (sqrt(dx^2+dy^2), integer result, use for proximity checks). Takes x1 (first point X coordinate, integer), y1 (first point Y coordinate, integer), x2 (second point X coordinate, integer), y2 (second point Y coordinate, integer). Returns distance (integer pixels, rounded from sqrt, 0=same point). Calculates delta (dx=x2-x1, dy=y2-y1), computes distance squared (dx^2+dy^2), applies square root (std::sqrt), casts to integer (truncates fractional part). Use to measure separation (check if objects within range), implement range-based AI (enemy detection radius), calculate movement speed (distance per frame), or sort by proximity (nearest object selection). Distance formula: Euclidean distance = sqrt((x2-x1)^2 + (y2-y1)^2), always positive (distance magnitude, no direction), units in pixels (screen/world space coordinates), integer truncation (fractional pixels lost, use for approximate checks). Use cases: (1) Range check (dist=b2dGetDistance(playerX,playerY,enemyX,enemyY): If dist<100 Then inRange), (2) Proximity detection (If b2dGetDistance(x1,y1,x2,y2)<detectionRadius Then detected), (3) Circle collision alternative (If b2dGetDistance(x1,y1,x2,y2)<r1+r2 Then collision, slower than b2dIsCirclesColliding due to sqrt), (4) Nearest object (minDist=9999: For each obj: dist=b2dGetDistance(px,py,obj.x,obj.y): If dist<minDist Then minDist=dist: nearest=obj), (5) Movement speed (speed=b2dGetDistance(oldX,oldY,newX,newY)/elapsedTime pixels per second). Common patterns: in range = If b2dGetDistance(x1,y1,x2,y2)<radius Then within circle; compare squared = If (dx*dx+dy*dy)<(radius*radius) Then avoid sqrt for speed. Performance: cost moderate (sqrt operation ~10-20 CPU cycles, relatively slow), integer coordinates (no floating point conversions, fast deltas), prefer squared distance (avoid sqrt by comparing distance^2 vs radius^2, faster for range checks), typical speed (~0.001ms per call, can call thousands per frame). Optimization: squared distance comparison (If (dx*dx+dy*dy)<(r*r) Then collision avoids sqrt, 2-3x faster), use IsCirclesColliding (optimized version for circle collision, avoids sqrt), batch calculations (calculate once, reuse if positions unchanged). Coordinate system: screen space (typical use with pixel coordinates), world space (works with any coordinate system), no bounds (negative coordinates valid, can calculate distance to off-screen points). Precision: integer truncation (sqrt(50.9) returns 7 not 8, loses fractional pixels), sufficient for gameplay (pixel-level precision adequate for most collision/proximity), use squared for exact (squared distances preserve precision, compare squared values directly). Related: b2dIsCirclesColliding optimized circle collision (uses distance^2 comparison, avoids sqrt, faster for collision detection), b2dIsCircleBoxColliding circle-rectangle collision (specialized collision, more complex than distance), b2dIsBoxesColliding rectangle collision (AABB test, no distance calculation needed).