How to calculate Rotation from 4 points Y axis

I have part on water surface, and i have 4 points in Y axis and i want to rotate part depends on how tilted the points are.



local function getRotation(Model:Model)
    local size = Model:GetExtentsSize()
    local center = Model:GetPivot().Position
    
    local WaterHight = 4 -- Offset
    local WaterOffset = calcWaterHeightOffset(center.X,center.Z) -- Y hight

    center = Vector3.new(center.X,WaterHight+WaterOffset,center.Z) -- Y=  Offset + WaterWaveHeight
    
    local halfSize = size / 2
    local corner1 = center + Vector3.new(-halfSize.X, 0, -halfSize.Z)
    local corner2 = center + Vector3.new(halfSize.X, 0, -halfSize.Z)
    local corner3 = center + Vector3.new(halfSize.X, 0, halfSize.Z)
    local corner4 = center + Vector3.new(-halfSize.X, 0, halfSize.Z)

end
2 Likes

You can use this formula to get the upvector and orient to it.

((p1-p2):Cross(p3-p2)).Unit

The fourth point is unnecessary but you can average the result to make it more fitting to the surface.

5 Likes