[2.0.59] math2d.bounding_box.contains_point does not use box orientation

Things that we don't consider worth fixing at this moment.
robot256
Smart Inserter
Smart Inserter
Posts: 1230
Joined: Sun Mar 17, 2019 1:52 am
Contact:

[2.0.59] math2d.bounding_box.contains_point does not use box orientation

Post by robot256 »

The very convenient helper function function math2d.bounding_box.contains_point(box, point) always assumes box.orientation=0. When orientation is nonzero and the box is not square, it does not accurately tell you whether a given point is inside a given bounding box. The is true of all the bounding_box functions, and is misleading because without orientation included, the left_top and right_bottom of entity.bounding_box are simply incorrect.

For contains_point(), the simplest solution is to rotate the point around the center of the box, and then do the comparison to the north-facing box coordinates, rather than attempt to rotate the box itself.

Here is a solution that works for me given the existing library functions:

local function rotated_bounding_box_contains_point(box, point)
local box_center = math2d.bounding_box.get_centre(box)
local box_angle = box.orientation * 360
local point_relative = math2d.position.subtract(point, box_center)
local rotated_point_relative = math2d.position.rotate_vector(point_relative, -box_angle)
local rotated_point = math2d.position.add(rotated_point_relative, box_center)
return math2d.bounding_box.contains_point(box, rotated_point)
end

It would be great if the library can be updated so the functions all use the full specification of BoundingBox, or if additional functions can be added that do so.
My mods: Multiple Unit Train Control, RGB Pipes, Shipping Containers, Rocket Log, Smart Artillery Wagons.
Maintainer of Auto Deconstruct, Cargo Ships, Vehicle Wagon, Honk, Shortwave.
Rseding91
Factorio Staff
Factorio Staff
Posts: 15834
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [2.0.59] math2d.bounding_box.contains_point does not use box orientation

Post by Rseding91 »

Thanks for the report however I don't see this changing. This function was added 7 years ago for some campaign tests that never stuck and got left behind because "why not".

Replicating the entirety of the bounding box logic into the lua math helpers just isn't something any of us want to do/maintain as far as I know.
If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Won't fix.”