i have this code : function BuildingController:HighlightCells(gridX, gridY, width, height)
self:ClearHighlightedCells()
local map = currentSelected.Map -- assuming map is a 2D array like {{'O', 'X', 'O'}, {'O', 'O', 'O'}} local buildX, buildY = 0, 0 -- Find the center (buildX, buildY) based on 'X' for y, yMap in pairs(map) do for x, mark in pairs(yMap) do if mark == "X" then print("SET DA OFFSET") print(x - 1) print(y - 1) buildX = x - 1 buildY = y- 1 end end end -- Highlight cells with applied rotation for y, yMap in pairs(map) do for x, mark in pairs(yMap) do -- Calculate the offset from the center local offsetFromCenterX = x - buildX local offsetFromCenterY = y - buildY print(mark) print(offsetFromCenterX) print(offsetFromCenterY) -- Apply rotation transformation local rotatedX, rotatedY if currentRotation == 90 then rotatedX = offsetFromCenterY rotatedY = -offsetFromCenterX + 2 elseif currentRotation == 180 then rotatedX = offsetFromCenterX rotatedY = -offsetFromCenterY + 3 elseif currentRotation == 270 then rotatedX = -offsetFromCenterY + 2 rotatedY = offsetFromCenterX else -- Default: no rotation (0 degrees) rotatedX = offsetFromCenterX rotatedY = offsetFromCenterY end local holder = rotatedX rotatedX = rotatedY rotatedY = holder -- Calculate the final cell position on the grid local cellX = gridX + rotatedX local cellY = gridY + rotatedY print(rotatedX) print(rotatedY) print(cellX) print(cellY) -- Ensure the cell is within grid bounds and highlight if cellX >= 1 and cellX <= self.Settings.GRID_WIDTH and cellY >= 1 and cellY <= self.Settings.GRID_HEIGHT then table.insert(highlightedCells, {x = cellX, y = cellY}) grid[cellX][cellY].part.SurfaceGui.ImageLabel.ImageColor3 = Color3.fromRGB(255, 255, 0) end end end
end
and the map is this: Map = {
{'O', 'O', 'O'},
{'O', 'X', 'O'},
{'O', 'O', 'O'},
{'O', 'O', 'O'},
}
}
but on the grid building its all offset and wonky and rotation wont work
Assistant
It sounds like you're encountering issues with cell offsets and rotation logic in your HighlightCells function. Let's break down the potential problems and suggest solutions to ensure your grid highlights correctly, regardless of rotation.
1. Iterating with pairs Instead of ipairs
Issue:
In Lua, pairs iterates over all key-value pairs in a table but does not guarantee order, which can lead to unexpected behavior when dealing with arrays (like your map). This unordered traversal can cause inconsistencies in how you process rows and columns.
Solution:
Use ipairs instead of pairs when iterating over the map. ipairs ensures that you traverse the table in order, which is crucial for maintaining correct grid positions.
lua
-- Replace 'pairs' with 'ipairs' for ordered traversal
for y, yMap in ipairs(map) do
for x, mark in ipairs(yMap) do
-- Your existing logic
end
end
2. Incorrect Calculation of buildX and buildY (Center Point)
Issue:
You're setting buildX and buildY based on the position of 'X' in the map by subtracting 1. This approach assumes that 'X' represents the center, but depending on your map size, this might not align correctly, especially with different map dimensions.
Solution:
Calculate the center of the map based on its dimensions rather than relying on the position of 'X'. This ensures that transformations like rotation are relative to the true center of the map.
lua
-- Calculate map dimensions
local mapHeight = #map
local mapWidth = #map[1]
-- Compute the center
local buildX = math.floor(mapWidth / 2) + 1
local buildY = math.floor(mapHeight / 2) + 1
3. Flawed Rotation Logic
Issue:
Hardcoded Offsets: Adding constants like +2 or +3 after rotation transformations can lead to incorrect positioning, especially if the map size changes.
Swapping rotatedX and rotatedY Unconditionally: The following lines swap rotatedX and rotatedY regardless of the rotation, which disrupts the intended rotation logic.
Solution:
Implement a standard rotation matrix based on the desired rotation angle. Here's how you can apply rotation transformations more systematically:
lua
-- Function to rotate a point around the center
local function rotatePoint(x, y, angle)
local rad = math.rad(angle)
local cosA = math.cos(rad)
local sinA = math.sin(rad)
local rotatedX = x * cosA - y * sinA
local rotatedY = x * sinA + y * cosA
return math.round(rotatedX), math.round(rotatedY) -- Rounding to handle integer grid positions
end
Integrate this function into your HighlightCells:
lua
function BuildingController:HighlightCells(gridX, gridY, width, height)
self:ClearHighlightedCells()
local map = currentSelected.Map
local mapHeight = #map
local mapWidth = #map[1]
-- Calculate the center of the map
local buildX = math.floor(mapWidth / 2) + 1
local buildY = math.floor(mapHeight / 2) + 1
-- Highlight cells with applied rotation
for y, yMap in ipairs(map) do
for x, mark in ipairs(yMap) do
if mark == "O" or mark == "X" then
-- Calculate the offset from the center
local offsetX = x - buildX
local offsetY = y - buildY
-- Apply rotation
local rotatedX, rotatedY = rotatePoint(offsetX, offsetY, currentRotation)
-- Calculate the final cell position on the grid
local cellX = gridX + rotatedX
local cellY = gridY + rotatedY
print(mark, rotatedX, rotatedY, cellX, cellY)
-- Ensure the cell is within grid bounds and highlight
if cellX >= 1 and cellX <= self.Settings.GRID_WIDTH and
cellY >= 1 and cellY <= self.Settings.GRID_HEIGHT then
table.insert(highlightedCells, {x = cellX, y = cellY})
grid[cellX][cellY].part.SurfaceGui.ImageLabel.ImageColor3 = Color3.fromRGB(255, 255, 0)
end
end
end
end
end
-- Helper function to round numbers
function math.round(num)
return math.floor(num + 0.5)
end
4. Ensuring Consistent Grid Coordinates
Issue:
Depending on how your grid is set up, the Y-axis might be inverted (common in many graphics systems where Y increases downward). This inversion affects how rotations should be applied.
Solution:
Adjust the rotation formula to account for your grid's Y-axis orientation. If your Y-axis increases downward, you might need to invert the Y-coordinate during rotation.
5. Additional Recommendations
Validate currentRotation: Ensure that currentRotation is one of the expected values (0, 90, 180, 270). You might also want to handle values outside these ranges or normalize them.
Handle Map Bounds: If your map isn't perfectly centered or has varying dimensions, ensure that rotations don't cause cells to go out of expected bounds.
Optimize Highlighting: If you're highlighting cells frequently, consider optimizing how you update the ImageColor3 property to reduce performance overhead.
Final Thoughts
By addressing the iteration method, refining your center calculations, and implementing a standard rotation approach, your HighlightCells function should correctly highlight cells on the grid with accurate offsets and rotations. Always test with different map sizes and rotation angles to ensure robustness.
If you continue to experience issues, consider adding more debug print statements to trace the values of offsetX, offsetY, rotatedX, and rotatedY to pinpoint where the logic might be deviating from expectations.
Example Implementation Incorporating Suggestions:
lua
function BuildingController:HighlightCells(gridX, gridY, width, height)
self:ClearHighlightedCells()
local map = currentSelected.Map
local mapHeight = #map
local mapWidth = #map[1]
-- Calculate the center of the map
local buildX = math.floor(mapWidth / 2) + 1
local buildY = math.floor(mapHeight / 2) + 1
-- Function to rotate a point around the center
local function rotatePoint(x, y, angle)
local rad = math.rad(angle)
local cosA = math.cos(rad)
local sinA = math.sin(rad)
local rotatedX = x * cosA - y * sinA
local rotatedY = x * sinA + y * cosA
return math.round(rotatedX), math.round(rotatedY)
end
for y, yMap in ipairs(map) do
for x, mark in ipairs(yMap) do
if mark == "O" or mark == "X" then
local offsetX = x - buildX
local offsetY = y - buildY
local rotatedX, rotatedY = rotatePoint(offsetX, offsetY, currentRotation)
local cellX = gridX + rotatedX
local cellY = gridY + rotatedY
print(mark, rotatedX, rotatedY, cellX, cellY)
if cellX >= 1 and cellX <= self.Settings.GRID_WIDTH and
cellY >= 1 and cellY <= self.Settings.GRID_HEIGHT then
table.insert(highlightedCells, {x = cellX, y = cellY})
grid[cellX][cellY].part.SurfaceGui.ImageLabel.ImageColor3 = Color3.fromRGB(255, 255, 0)
end
end
end
end
end
function math.round(num)
return math.floor(num + 0.5)
end
This implementation ensures that:
Traversal is Ordered: Using ipairs guarantees that rows and columns are processed in sequence.
Center is Accurately Calculated: The center is dynamically computed based on map dimensions.
Rotation is Systematic: A standard rotation matrix is applied, making the rotation logic clear and scalable.
Swapping is Eliminated: The unnecessary swapping of rotatedX and rotatedY is removed, preventing unintended offset issues.
Feel free to adjust the rotatePoint function or the center calculation if your grid's coordinate system differs. Testing with different map sizes and rotation angles will help ensure the robustness of your implementation.