Page 1 of 2

Maths formula for the maths guru's

Posted: Wed Jul 21, 2021 4:55 am
by togFox
Hello maths experts - in my current project I have modelled a rewards system where delivering x% of the goal will deliver y% of the reward but not on a linear scale. I've done a graph in excel and included the data for the graph and I'm hoping you brainy people can help me determine the equation so that:

when a player delivers x% of the required outcome (x-axis), the game will provide y% (y-axis) of the total available reward.

The required curve is NOT the blue line. Please see the red line and the data that creates it. It is intentionally curved so that 'payoff' happens about 65% (doesn't have to be exact).

The formula you see is the MS Excel trend line (ignore the error - I couldn't remove that one) but it really means nothing to me.

Image

Re: Maths formula for the maths guru's

Posted: Wed Jul 21, 2021 6:28 am
by ReFreezed
If it's not important that it's a smooth curve, you could instead simply imagine a straight line between the given points and use linear interpolation to determine the payout. That way you can easily have any arbitrary shape of the "curve". Also, minimal math involved!

Code: Select all

local function lerp(v1, v2, t)
	return v1 + (v2-v1)*t
end

local function getPayout(payouts, delivered)
	local iFloat = delivered * #payouts
	local iLow   = math.floor(iFloat)
	local iHigh  = math.ceil(iFloat)
	return lerp(payouts[iLow], payouts[iHigh], iFloat%1)
end

local payouts = {[0]=0, .02, .05, .12, .25, .40, .57, .75, .90, .98, 1}

function love.draw()
	local w, h = love.graphics.getDimensions()
	love.graphics.line(0, h, w, 0)

	for x = 0, w do
		-- 'delivered' and 'payout' are values between 0 and 1.
		local delivered = x / w
		local payout    = getPayout(payouts, delivered)
		love.graphics.points(x, (1-payout)*h)
	end
end
curve.png
curve.png (1.14 KiB) Viewed 6929 times

Re: Maths formula for the maths guru's

Posted: Wed Jul 21, 2021 7:18 am
by darkfrei
Not sure, but pretty similar:
https://www.desmos.com/calculator/5rl0vfpada

Re: Maths formula for the maths guru's

Posted: Wed Jul 21, 2021 11:57 am
by togFox
Thanks DF.

No, a smooth curve is not necessary. I just thought a single formula = 1 line of code. I'll adapt what you've suggested. Thanks.

Re: Maths formula for the maths guru's

Posted: Wed Jul 21, 2021 2:59 pm
by milon
Here's another option that accomplishes roughly the same thing using a logistic function instead.
https://www.desmos.com/calculator/rbkoh0hjwc

To do e^x in Lua, use math.exp(x) (I had to look that up, LOL)

Re: Maths formula for the maths guru's

Posted: Wed Jul 21, 2021 6:01 pm
by zorg
The wanted curve looks like two sinusoidal curves, connected at the x = 60% point. it could probably be implemented by using such easing functions and adjusting the range and domain of them (scaling the input & output).

Re: Maths formula for the maths guru's

Posted: Wed Jul 21, 2021 9:14 pm
by darkfrei
zorg wrote: Wed Jul 21, 2021 6:01 pm The wanted curve looks like two sinusoidal curves, connected at the x = 60% point.
Two connected sine curves is just one cosine curve :)

Re: Maths formula for the maths guru's

Posted: Thu Jul 22, 2021 5:16 am
by zorg
darkfrei wrote: Wed Jul 21, 2021 9:14 pm
zorg wrote: Wed Jul 21, 2021 6:01 pm The wanted curve looks like two sinusoidal curves, connected at the x = 60% point.
Two connected sine curves is just one cosine curve :)
Feel free to demonstrate to me how you skew the two parts of it differently :3

Re: Maths formula for the maths guru's

Posted: Thu Jul 22, 2021 7:38 am
by darkfrei
zorg wrote: Thu Jul 22, 2021 5:16 am
darkfrei wrote: Wed Jul 21, 2021 9:14 pm
zorg wrote: Wed Jul 21, 2021 6:01 pm The wanted curve looks like two sinusoidal curves, connected at the x = 60% point.
Two connected sine curves is just one cosine curve :)
Feel free to demonstrate to me how you skew the two parts of it differently :3
It's more interesting your solution, how to connect two lines at (0,0), (1,1) and between each other at (x1, y1), smooth and nicely.

Re: Maths formula for the maths guru's

Posted: Thu Jul 22, 2021 1:57 pm
by milon
zorg wrote: Wed Jul 21, 2021 6:01 pm The wanted curve looks like two sinusoidal curves, connected at the x = 60% point. it could probably be implemented by using such easing functions and adjusting the range and domain of them (scaling the input & output).
I'm curious what you're getting at, Zorg. Are you talking about 2 separate sin (or cos) curves and using an if/else scenario to decide which to use? Or do you have a meaningful way to combine them into 1 function? I'd love to see the algorithm you're thinking of. (And I'm pretty sure we're also way beyond what togFox looking for, haha! :3 )