class_name Math static func get_chunk_from_pos(coord) -> Vector2i: return Vector2i( floori(coord.x / (Region.CHUNK_TILE_SIZE * Region.TILE_SIZE)), floori(coord.y / (Region.CHUNK_TILE_SIZE * Region.TILE_SIZE)) ) static func get_tile_from_pos(coord) -> Vector2i: return Vector2i( floori(coord.x / (Region.TILE_SIZE)), floori(coord.y / (Region.TILE_SIZE)), ) static func get_tiles_in_circle(center: Vector2,radius : float) -> Array[Vector2i]: var tiles : Array[Vector2i] = [] var margin = ceili(radius * 0.5) for x in range( floori((center.x - radius/2.) / Region.TILE_SIZE) - margin, ceili((center.x + radius/2.) / Region.TILE_SIZE) + margin, ): for y in range( floori((center.y - radius/2.) / Region.TILE_SIZE) - margin, ceili((center.y + radius/2.) / Region.TILE_SIZE) + margin, ): if is_tile_on_circle(Vector2i(x,y), center, radius): tiles.append(Vector2i(x,y)) return tiles static func is_tile_on_circle(tile_coord : Vector2i, circle_center: Vector2, circle_radius : float) -> bool: var absolute_tile_pos : Vector2 = tile_coord * Region.TILE_SIZE var tile_center = absolute_tile_pos + Vector2.ONE * Region.TILE_SIZE / 2 var tile_radius = roundf(Region.TILE_SIZE/2.) return pow(tile_center.x - circle_center.x, 2) + pow(tile_center.y - circle_center.y, 2) <= pow(tile_radius + circle_radius, 2) # Loop over tile corners to know if the area collide # var corners : Array[Vector2] = [] # for x in [0,1]: # for y in [0,1]: # corners.append( # absolute_tile_pos # + Vector2.RIGHT * x * Region.TILE_SIZE # + Vector2.DOWN * y * Region.TILE_SIZE # ) # # Check if segment touch area # for i in range(4): # var a = corners[i%4] # var b = corners[(i+1)%4] # if segment_intersect_circle(a,b,circle_center,circle_radius): # return true # return false # Stolen here https://stackoverflow.com/questions/1073336/circle-line-segment-collision-detection-algorithm static func segment_intersect_circle( a : Vector2, b : Vector2, c : Vector2, radius: float ) -> bool: var a_circle = c - a var b_a = b - a var proj_point = proj(a_circle,b_a) + a return proj_point.distance_to(c) < radius static func proj(a : Vector2,b : Vector2) -> Vector2: var k = a.dot(b) / b.dot(b) return Vector2(k * b.x, k * b.y)