How do I create a wave using three positions?

I want to make a waving motion that goes back and forth. From point A to point B, but it starts in the middle of the two. How would I go about doing this?

1 Like

Do you have a reference for what you are trying to achieve?

Yes, Wave - Roblox This is a test model that works, but does not have the middle between a and b working and I dont know how. An this is the what I used to create this: Mechanical Wave Simulation

You can prob use a system kinda like this

local a = Vector3.new(0,0,0)
local b = Vector3.new(10,0,0)
local numOfParts = 50
local numOfWaves = 3
local amplitude = 1 --size of wave

local parts = {}
for i = 1,numOfParts do --simply setting up some parts
	local p = Instance.new("Part")
	p.Size = Vector3.new(0.1,0.1,0.1)
	p.Anchored = true
	p.Parent = workspace
	table.insert(parts,p)
end

function moveParts(offset) --change offset to make the wave move
	local dist = (b-a).Magnitude
	for x = 0,numOfParts-1 do
		local p = parts[x+1]
		local current_dist = dist * x/(numOfParts-1)
        --x/(numOfParts-1) is a number from 0 to 1 describing the progress through the loop
        --0.5 would mean its halfway through
        --dist  * 0.5 would mean halfway through the distance
		local inp = 2 * math.pi * numOfWaves * x/(numOfParts-1)
        --1 sine wave is  2 * math.pi studs long
        --2 sine waves are 2 * math.pi * 2 studs long
        --3 sine waves are 2 * math.pi * 3 studs long, etc
        --by multiplying the total length of all of the sine waves by our progress value, x/(numOfParts-1)
        --you can get the current position along the wave you should be at to get exactly the num of waves you need
		p.Position = (CFrame.new(a,b)*CFrame.new(0,math.sin(inp+offset),-current_dist)).p
	end
end

moveParts(0)

Heres two little videos showing what the different variables look like:


And what the finished product should look like in roblox:

image

In order to make the wave move, just call moveParts() in a loop with an offset value
To make it go back and forth you can do:

local rs = game:GetService("RunService")

local sizeOfBackAndForthMotion = 5
local speed = 1

local curr = 0
while true do
    local dt = rs.Heartbeat:Wait()
    curr = (curr + speed*dt)%(math.pi*2)

    moveParts(math.sin(curr)*sizeOfBackAndForthMotion)
end

If you want to move a and b while this is happening youll have to update the variables each time you run moveParts

2 Likes

Ok thank you I will mark as a solution as soon once I try.

1 Like