Skip to content

Commit

Permalink
Merge pull request #254 from stevehalliwell/add-remap
Browse files Browse the repository at this point in the history
Add remap
  • Loading branch information
stevehalliwell authored Jun 16, 2024
2 parents 7a5d601 + b04861a commit 16a8ca7
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 12 deletions.
72 changes: 72 additions & 0 deletions ulox/ulox.core.tests/MathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,5 +274,77 @@ public void Clamp_When0And1And2_ShouldBe1()

Assert.AreEqual("", testEngine.InterpreterResult);
}

[Test]
public void Remap_WhenExpected_ShouldBe2()
{
testEngine.Run(@"
var res = Math.Remap(0.5, 0, 1, 1, 3);
expect res == 2;
");

Assert.AreEqual("", testEngine.InterpreterResult);
}

[Test]
public void Remap_When0And1And2_ShouldBe1()
{
testEngine.Run(@"
var res = Math.Remap(0.5, 0, 1, 1, 3);
expect res == 2;
");

Assert.AreEqual("", testEngine.InterpreterResult);
}

[Test]
public void RandUnitCircle_WhenCalled_ShouldBeLessThan1Radius()
{
testEngine.Run(@"
var (x,y) = Math.RandUnitCircle();
var res = Math.Sqrt(x*x + y*y);
expect res < 2;
");

Assert.AreEqual("", testEngine.InterpreterResult);
}

[Test]
public void MoveTowards_When1to2and3_ShouldBe2()
{
testEngine.Run(@"
var res = Math.MoveTowards(1,2,3);
expect res == 2;
");

Assert.AreEqual("", testEngine.InterpreterResult);
}

[Test]
public void Lerp_When1to2andQuarter_ShouldBe1andQuarter()
{
testEngine.Run(@"
var res = Math.Lerp(1,2,0.25);
expect res == 1.25;
");

Assert.AreEqual("", testEngine.InterpreterResult);
}

[Test]
public void Dampen_When1and2_ShouldBeCloser()
{
testEngine.Run(@"
var a = 1;
var b = 2;
var decay = Math.CalcDampenHalflife(1,0.1);
var c = Math.Dampen(a,b,decay,0.1);
expect
c > a,
c < b;
");

Assert.AreEqual("", testEngine.InterpreterResult);
}
}
}
38 changes: 26 additions & 12 deletions ulox/ulox.core/Package/Runtime/Library/MathStdLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ internal static InstanceInternal MakeMathInstance()
(nameof(MoveTowards), Value.New(MoveTowards, 1, 3)),
(nameof(Lerp), Value.New(Lerp, 1, 3)),
(nameof(Dampen), Value.New(Dampen, 1, 4)),
(nameof(CalcDampenHalflife), Value.New(CalcDampenHalflife, 1, 2))
(nameof(CalcDampenHalflife), Value.New(CalcDampenHalflife, 1, 2)),
(nameof(Remap), Value.New(Remap, 1, 5))
);

diLibInst.Freeze();
Expand Down Expand Up @@ -260,25 +261,25 @@ private static NativeCallResult Clamp(Vm vm)

private static NativeCallResult MoveTowards(Vm vm)
{
var arg1 = vm.GetArg(1).val.asDouble;
var arg2 = vm.GetArg(2).val.asDouble;
var arg3 = vm.GetArg(3).val.asDouble;
var delta = arg2 - arg1;
var result = arg2;
if (Math.Abs(delta) > arg3)
var from = vm.GetArg(1).val.asDouble;
var to = vm.GetArg(2).val.asDouble;
var step = vm.GetArg(3).val.asDouble;
var delta = to - from;
var result = to;
if (Math.Abs(delta) > step)
{
result = arg1 + Math.Sign(delta) * arg3;
result = from + Math.Sign(delta) * step;
}
vm.SetNativeReturn(0, Value.New(result));
return NativeCallResult.SuccessfulExpression;
}

private static NativeCallResult Lerp(Vm vm)
{
var arg1 = vm.GetArg(1).val.asDouble;
var arg2 = vm.GetArg(2).val.asDouble;
var arg3 = vm.GetArg(3).val.asDouble;
var result = arg1 + (arg2 - arg1) * arg3;
var a = vm.GetArg(1).val.asDouble;
var b = vm.GetArg(2).val.asDouble;
var p = vm.GetArg(3).val.asDouble;
var result = a + (b - a) * p;
vm.SetNativeReturn(0, Value.New(result));
return NativeCallResult.SuccessfulExpression;
}
Expand All @@ -305,5 +306,18 @@ public static NativeCallResult CalcDampenHalflife(Vm vm)
vm.SetNativeReturn(0, Value.New(result));
return NativeCallResult.SuccessfulExpression;
}

public static NativeCallResult Remap(Vm vm)
{
var value = vm.GetArg(1).val.asDouble;
var from1 = vm.GetArg(2).val.asDouble;
var to1 = vm.GetArg(3).val.asDouble;
var from2 = vm.GetArg(4).val.asDouble;
var to2 = vm.GetArg(5).val.asDouble;

var result = from2 + (value - from1) * (to2 - from2) / (to1 - from1);
vm.SetNativeReturn(0, Value.New(result));
return NativeCallResult.SuccessfulExpression;
}
}
}

0 comments on commit 16a8ca7

Please sign in to comment.