An exercise in vector math

oceangen.rbxl (24.0 KB)
My code generates a grid of tiles connected by nodes. The idea is to be able to move a node, create a plane with the two nodes it’s connected to and then move fourth node in the tile onto the new plane.

It works, but only when the grid is flat.

I’m almost positive that I’m making a mistake when I assign the Y value in the moveNode function in the OceanGenerator script.

Pre-move:

Post-move:

Here’s the code that handles the movement.

local function moveNode(node,position,ignore)
	local nodeTiles = node:getTiles()
	local oldPos = node:getPosition()
	
	node:setPosition(position)
	for i = 1, #nodeTiles do
		if(not (nodeTiles[i] == ignore)) then
			local connected,unconnected = getConnectedNodesOnTile(node,nodeTiles[i]) --connected refers to the connected nodes on the tile, unconnected refers to the unconnected
			local vec1,vec2 = connected[1]:getPosition()-position, connected[2]:getPosition()-position
			local planePos, vecOnCurrPlane= nodeTiles[i]:getPosition(), nodeTiles[i]:getPosition() -oldPos
			local normal = vec1:Cross(vec2)
			local vecToNew = position-nodeTiles[i]:getPosition()
			local angle = -math.abs(math.asin(vecToNew.Y/vecToNew.Magnitude))
			if(i==3) then angle = -angle end
			normal = normal/normal.Magnitude
			local y,x,z = solveY(normal,vecOnCurrPlane) +oldPos.Y,  planePos.X, planePos.Z 
			local newPos = Vector3.new(x,y,z)
			local tileCFrame = nodeTiles[i]:getCFrame()*CFrame.fromAxisAngle(connected[1]:getPosition()-connected[2]:getPosition(), angle)
			nodeTiles[i]:setCFrame(tileCFrame)
			local unconnectedPos = unconnected:getPosition()
			x,y,z = unconnectedPos.X,unconnectedPos.Y+ solveY(normal,unconnectedPos-nodeTiles[i]:getPosition()),unconnectedPos.Z
			unconnectedPos = Vector3.new(x,y,z)
			unconnected:setPosition(unconnectedPos)
			
		end
	end
end

Any and all suggestions are appreciated, organizational tips included.

1 Like

If I understand this correctly, the problem is that you cannot move the node up while keeping all the tiles as squares. You’ll have to make the tiles triangles, maybe by cutting each tile in half along the diagonal.

5 Likes

A triangular grid could be interesting, considering that you could form a plane using only the vertices of the triangle. But the idea here was to use the nodes already on the tile to form a plane and then move the remaining node onto the plane. Although, if I can’t make the square grid structure work, I’ll try triangles.