## Maths formula for the maths guru's

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
togFox
Party member
Posts: 265
Joined: Sat Jan 30, 2021 9:46 am

### Maths formula for the maths guru's

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.

ReFreezed
Party member
Posts: 272
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

### Re: Maths formula for the maths guru's

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 (1.14 KiB) Viewed 2263 times
Tools: Hot Particles, LuaPreprocess, (more) Games: Momento Temporis: Light from the Deep, Energize!
"If each mistake being made is a new one, then progress is being made."
darkfrei
Party member
Posts: 330
Joined: Sat Feb 08, 2020 11:09 pm

### Re: Maths formula for the maths guru's

Not sure, but pretty similar:
Attachments
2021-07-21T09_18_20-Desmos _ Grafik-Rechner.png (64.36 KiB) Viewed 2255 times
togFox
Party member
Posts: 265
Joined: Sat Jan 30, 2021 9:46 am

### Re: Maths formula for the maths guru's

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.
milon
Party member
Posts: 158
Joined: Thu Jan 18, 2018 9:14 pm

### Re: Maths formula for the maths guru's

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)
Attachments
Screenshot_2021-07-21_11-00-40.png (30.51 KiB) Viewed 2199 times
zorg
Party member
Posts: 3115
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Maths formula for the maths guru's

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).
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
darkfrei
Party member
Posts: 330
Joined: Sat Feb 08, 2020 11:09 pm

### Re: Maths formula for the maths guru's

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
zorg
Party member
Posts: 3115
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Maths formula for the maths guru's

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
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
darkfrei
Party member
Posts: 330
Joined: Sat Feb 08, 2020 11:09 pm

### Re: Maths formula for the maths guru's

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
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.
milon
Party member
Posts: 158
Joined: Thu Jan 18, 2018 9:14 pm

### Re: Maths formula for the maths guru's

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! )

### Who is online

Users browsing this forum: No registered users and 15 guests