Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Understanding how to fix this bounce function, and make up for the differing end positions?

I'm currently in the process of creating a 3D Connect Four Game. Basically, I need to make the pegs "bounce" when they fall.

I have the basics for this. Here's a link to a gif I made of the script working.

However, there's a problem.

Here's a link to the problem.

It looks like the bouncing isn't changing. It goes below the other pegs when they stack too high. I somehow need to account for this.

Here's the function,

local y = 2.75
local mag = 6


local start = peg.CFrame
local finish = CFrame.new(start.x,((y)+value*mag)-mag,start.z)
-- value equals the peg position. If there were 0 pegs in a column, then value would equal 1. Goes up to 6. 

count = 0

local function lurp(s,f,num)
	count = count+1

	for i = 0,1,num do
		peg.CFrame = s:lerp(f,i)
		wait()
	end

	if count > 4 then-- Number of lerps. num/2 = bounces
		return
	elseif count%2 == 0 then-- Down
		lurp(CFrame.new(f.x, f.y, f.z),CFrame.new(s.x, s.y, s.z),0.12);
	else-- Up
		lurp(CFrame.new(f.x, f.y, f.z),CFrame.new(s.x, s.y/2, s.z),0.12);
	end
end

lurp(start,finish, 0.08);

The lurp function lerps the peg in a way thay would mimic a bounce. The down part on line 22 seems to be the problem area from what I could tell.

Useful info

Because this is a math function, it might be worth knowing some of the maths involved.

2.75 studs is the bottom of the board or the y position of the last peg hole.

6 studs is the distance between each hole vertical wise, which is all we need to worry about.

I'm not sure if this is helpful. If you have any questions feel free to ask me.

Thank you

1 answer

What I was really asking for was the equation to a bounce easing function. Luckily, I found one that works awesomely.

Here it is.

local outBounce = function(ratio)
	if ratio < 1/2.75 then
		return 7.5625*ratio*ratio
	elseif ratio < 2/2.75 then
		ratio = ratio - 1.5/2.75
		return 7.5625*ratio*ratio+0.75
	elseif ratio < 2.5/2.75 then
		ratio= ratio - 2.25/2.75
		return 7.5625*ratio*ratio+0.9375
	else
		ratio = ratio - 2.625/2.75
		return 7.5625*ratio*ratio+0.984375
	end
end

This causes a perfect effect for what I'm going for. To use this, you would just give it the i in a for loop with a lerp function. Example,

local start = peg.CFrame-- Old CFrame
local finish = peg.CFrame*CFrame.new(20,0,0)-- New CFrame

for i = 0,1,0.03 do
	local x = outBounce(i)
	peg.CFrame = start:lerp(finish,x)
	wait()
end

Here's an AWESOME link to tons of easing function in Lua.

Thank you to all who tried to help.

This discussion is preserved as it was. New answers, comments, and votes are closed.