Rotating A Vector?

I’m making a placement system which only allows you to place an object on valid grid squares, what I have works IF the object is not rotated.

In the following GIFs, the Blue Squares indicates the grid squares that are valid/exists (where the object wants to be places). The object in the GIFs requires 6 valid grid squares, any less & it wont move to that new position.

Working (Not Rotated): (Stops the object if it detects <6 valid grids)
https://gyazo.com/7c9a20b8778fc51ad3db83c9161465f9

Not Working (Rotated 90,180, or 270):
https://gyazo.com/2ffecc48671a5e8bf85539989e6889f4

An example of what I mean by “Rotating a Vector”

Everything is positioned based, I can’t use CFrame. I’ve looked at every post I could find relating to this topic & have found no solution.

Assuming that we will always have 90, 180, 270 etc (assuming clockwise).
90: negate just z value
180 degrees: negate x and z values
270: negate just x value

By ‘y’ do you mean ‘z’? regardless this didnt work

My code (before hand)

local start_position = Vector3.new( -- hitboxPosition = origin point (center)
	hitboxPosition.X - (hitbox.Size.X/2),
	0,
	hitboxPosition.Z - (hitbox.Size.Z/2)
)

for i = 1, math.round(hitbox.Size.X / self._grid) do -- for each grid it occupies
	local point = start_position + Vector3.new((self._grid * i) - (self._grid/2), 0, 0)

	for ii = 1, math.round(hitbox.Size.Z / self._grid) do -- for each grid it occupies
		local newPoint = point + Vector3.new(0, 0, (self._grid * ii) - (self._grid/2))
		
		local newVector = Vector3.new( -- grid cell point
			math.floor(newPoint.X * 1e+3) / 1e+3,
			0,
			math.floor(newPoint.Z * 1e+3) / 1e+3
		)

		table.insert(points, newVector)
	end

It the GIFs it might look like 1 model meaning 1 hitbox, but there are actually 2 hitboxes, I’m getting the grid cells for each hitbox. The entire item has a “master” hitbox which is how I’m moving the entire thing, the “sub” hitboxes need to rotate around the origin (center) of the “master” hitbox


Yes.

Assuming this is setting the X coordinate, you would just need to check the rotation angle, and change the sign of the new Vector3 accordingly.

Same thing here, change sign of new Vector3 according to rotation angle.

If your code works when the object is not rotated, you should make the vector relative to the rotation of the grid. You can either do this using object space or subtracting the rotation.

function clockwiseRotation(x, z)
return z, -x
end

Ex:

local x, z = 2, 1
local newx, newz = clockwiseRotation(x, z) -- 1, -2

Use it for each blue square to get it’s new position.

1 Like

Doing that results in the following: https://gyazo.com/8cbc8073d4801fc9c3a40321849506d5

I think it would be possible to use the Vector3.Cross method, but I’m no expert and I’m not sure if it would even work.

That’s because you are rotating their entire position.

Instead rotate their offset from the center blue square first.
After that you can apply the position of the entire model to all squares.
image

The blocks have an individual offset value from the center block (these are: (0,0), (1,0), (1,-1))
then the whole section has an offset value from the origin (which is (-3,3))

First rotate the blocks individual offset, then apply the sections offset (which in this case was (-3,3))