Skip to content

Instantly share code, notes, and snippets.

@Rseding91
Last active April 17, 2019 21:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rseding91/d120007d0fe19dc1e6bca1fab8ceaec7 to your computer and use it in GitHub Desktop.
Save Rseding91/d120007d0fe19dc1e6bca1fab8ceaec7 to your computer and use it in GitHub Desktop.
Factorio Rail bounding box logic
std::pair<BoundingBox, BoundingBox> Rail::calculateBoundingBoxes(MapPosition position,
Direction direction,
RailBendingType bendingType,
const BoundingBox& collisionBoundingBox,
const BoundingBox* secondaryCollisionBoundingBox)
{
std::pair<BoundingBox, BoundingBox> boundingBoxes;
if (bendingType == RailBendingType::Straight)
{
if (direction.isDiagonal())
{
boundingBoxes.first = BoundingBox(BoundingBox(-0.65, -0.7, 0.65, 0.7), position);
Vector movingVector(Math::sqrt_2 / 2, direction);
boundingBoxes.first.orientation = direction.left90();
boundingBoxes.first += movingVector;
}
else
{
boundingBoxes.first = BoundingBox(collisionBoundingBox, position);
if (direction == Direction::East)
boundingBoxes.first.orientation = direction;
else
assert(direction == Direction::North /* Non-diagonal rail directions should be always "normalized" to North or East.*/);
}
}
else
{
Direction primaryDirection = Rail::primaryTurnRailDirection(direction);
boundingBoxes.first = BoundingBox(BoundingBox(collisionBoundingBox, primaryDirection), position);
boundingBoxes.second = BoundingBox(*secondaryCollisionBoundingBox, position);
boundingBoxes.second.orientation += RealOrientation(primaryDirection);
Direction trueDirection = direction;
if (!trueDirection.isDiagonal())
--trueDirection;
Vector movingVector(Math::sqrt_2, trueDirection.opposite());
boundingBoxes.first += movingVector;
boundingBoxes.first -= Vector(1.29, primaryDirection);
boundingBoxes.second += movingVector;
boundingBoxes.second -= Vector(1.3, primaryDirection);
RealOrientation secondaryOrientation(direction);
if (direction.isDiagonal())
boundingBoxes.second.orientation += 0.09;
else
{
boundingBoxes.second.orientation -= 0.09;
secondaryOrientation -= 0.125;
}
boundingBoxes.second += Vector(1.5, primaryDirection);
boundingBoxes.second += Vector(1.85, secondaryOrientation);
}
return boundingBoxes;
}
Direction Rail::primaryTurnRailDirection(Direction orientation)
{
if (!orientation.isDiagonal())
return orientation;
return orientation.left45();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment