About Monkey 2 › Forums › Monkey 2 Code Library › [Math] Lerp Interpolations (Lerp, LerpDuration, LerpSpeed)
This topic contains 0 replies, has 1 voice, and was last updated by
cocon 1 year, 10 months ago.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
June 17, 2017 at 7:30 am #8790
One very important function is Lerp, this will allow you to transition from the source value to the target value using a more clean and predicable way, instead of changing it directly. For more advanced uses you might use a Tweening system instead, but in more simpler uses this will be it.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174#Import "<mojo>"Using mojo..Using std.collections ' ListUsing std.random ' RndUsing std.graphics ' ColorUsing std.time ' MillisecsUsing std.geom ' Vec2f'________________________________________________________________#rem monkeydoc Performs a linear interpolation between two values based on blending value.@param a The original value.@param b The target value.@param blend The blending percentage factor (normalized between 0.0 and 1.0).#endFunction Lerp:Float(a:Float, b:Float, blend:Float)Return blend * (b - a) + aEnd#rem monkeydoc Performs a linear interpolation between two values based on a time duration.@param a The original value.@param b The target value.@param startTime The time at when the interpolation started in milliseconds.@param duration The time at when the interpolation will end in milliseconds.#endFunction LerpDuration:Float(a:Float, b:Float, startTime:Int, duration:Int)Local elapsed := Cast<Float>(Millisecs()) - startTimeLocal progress := Cast<Float>(elapsed) / durationIf elapsed >= duration Return bReturn Lerp(a, b, progress)End#rem monkeydoc Performs a linear interpolation between two values based on a speed value.@param a The original value.@param b The target value.@param speed The speed factor at which the interpolation occurs.#endFunction LerpSpeed:Float(a:Float, b:Float, speed:Float)Local diff := b - aspeed = Abs(speed)If Abs(diff) < speed Then Return bReturn a + speed * Sgn(diff)End'________________________________________________________________Class MovableObject' The position of the movable object.Field Position := New Vec2f' The new position of the movable object.Field PositionNew := New Vec2f' The starting position of the movable object, at the time new position is updated.Field PositionStart := New Vec2fField TimeStart :Int' DecorationField DecorColor:ColorField DecorText:StringField DecorSize:Float' DecorationMethod New(text:String, x:Int, y:Int, s:Int, c:Color)Position = New Vec2f(x, y)PositionNew = New Vec2f(x, y)DecorText = textDecorSize = sDecorColor = cEndMethod SetNewPosition(x:Float, y:Float)PositionStart.X = Position.XPositionStart.Y = Position.YPositionNew.X = xPositionNew.Y = yTimeStart = Millisecs()EndMethod UpdateSmooth()Position.X = Lerp(Position.X, PositionNew.X, 0.05)Position.Y = Lerp(Position.Y, PositionNew.Y, 0.05)EndMethod UpdateDuration(duration:Int = 2000)Position.X = LerpDuration(PositionStart.X, PositionNew.X, TimeStart, duration)Position.Y = LerpDuration(PositionStart.Y, PositionNew.Y, TimeStart, duration)EndMethod UpdateSpeed(speed:Float = 1.0)Position.X = LerpSpeed(Position.X, PositionNew.X, speed)Position.Y = LerpSpeed(Position.Y, PositionNew.Y, speed)EndMethod Draw(canvas:Canvas)canvas.Color = Color.Whitecanvas.DrawText(DecorText, Position.X, Position.Y, -1, 0.5)canvas.Color = DecorColorcanvas.DrawRect(Position.X - (DecorSize/2), Position.Y - (DecorSize/2), DecorSize, DecorSize)EndProperty GetStringPosition:String()Return Position.X + " " + Position.YEndProperty GetStringPositionNew:String()Return PositionNew.X + " " + PositionNew.YEndEndClass MyWindow Extends WindowField objects:List<MovableObject>Field obSmooth:MovableObjectField obDuration:MovableObjectField obSpeed:MovableObjectMethod New()' Create the objectsobSmooth = New MovableObject("Smooth", Width/2, Height/2, 50, New Color(0.9, 0.9, 0.0, 0.8))obDuration = New MovableObject("Duration", Width/2, Height/2, 40, New Color(0.9, 0.0, 0.0, 0.8))obSpeed = New MovableObject("Speed", Width/2, Height/2, 30, New Color(0.0, 0.0, 0.9, 0.8))' Add them to a listobjects = New List<MovableObject>objects.AddLast(obSmooth)objects.AddLast(obDuration)objects.AddLast(obSpeed)EndMethod OnRender(canvas:Canvas) Override' Update each of the movable objects in it's specific way.obSmooth.UpdateSmooth()obDuration.UpdateDuration()obSpeed.UpdateSpeed()App.RequestRender()' Draw the objectscanvas.Clear(Color.Black)For Local o := Eachin objectso.Draw(canvas)End' Draw messagescanvas.Color = Color.Whitecanvas.DrawText("Press Left Mouse To Set Positions", 15, 15*1, 0, 0.5)canvas.DrawText("Press Left Mouse To Set Random Positions", 15, 15*2, 0, 0.5)EndMethod OnMouseEvent( event:MouseEvent ) OverrideIf event.Type = EventType.MouseDown' When there's a left mouse click set the new position of the objects.If event.Button = MouseButton.LeftFor Local o := Eachin objectso.SetNewPosition(event.Location.X, event.Location.Y)End' When there's a right click set random positions.Elseif event.Button = MouseButton.RightFor Local o := Eachin objectso.SetNewPosition(Rnd(Width), Rnd(Height))EndEndEndEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()End -
AuthorPosts
Viewing 1 post (of 1 total)
You must be logged in to reply to this topic.