December 04, 2020, 11:29:57 AM

Author Topic: [bb] Curve#() - Apply a curve to a value based on a minimum and maxiumum and curve amplitude by Streaksy [ 1+ years ago ]  (Read 422 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Curve#() - Apply a curve to a value based on a minimum and maxiumum and curve amplitude
Author : Streaksy
Posted : 1+ years ago

Description : This is kinda hard to explain but there's been many times I've needed this... for smoothly flowing movements etc.

Syntax: Curve#(val#,min#,max#,typ=3,amp#=1)

val#    - point beteen minimum and maximum
min#   - minimum value (floor) which acts as point A
max#  - maximum value (floor) which acts as point B
typ      - curve type (1=smooth in, 2=smooth out, 3=smooth both)
amp#  - curve amplituted.  best left as default (1)

I hope that makes sense...

My second contribution to the code archives.  Hope it's useful to someone.  The code's kinda rough, as usual.


Code :
Code: BlitzBasic
  1. ;EXAMPLE
  2. Graphics 1024,768,32,2
  3. SetBuffer BackBuffer()
  4.  
  5. MoveMouse 300,0
  6. typ=3
  7. Repeat
  8. Cls
  9.         If KeyHit(2) Then typ=1
  10.         If KeyHit(3) Then typ=2
  11.         If KeyHit(4) Then typ=3
  12.         Color 255,255,255
  13.         Text 0,0,"1-Smooth out"
  14.         Text 0,20,"2-Smooth in"
  15.         Text 0,40,"3-Smooth in and out"
  16.                 For x=1 To GraphicsWidth()
  17.                 y=(x*GraphicsHeight())/GraphicsWidth()
  18.                 y2=curve(y,0,GraphicsHeight(),typ,(Float(MouseX())/300))
  19.                 y=GraphicsHeight()-y
  20.                 y2=GraphicsHeight()-y2
  21.                 y=y/2+(GraphicsHeight()*.25)
  22.                 y2=y2/2+(GraphicsHeight()*.25)
  23.                 Color 0,0,255
  24.                 Plot x,y
  25.                 Color 255,0,0
  26.                 Plot x,y2
  27.                 Next
  28. Flip
  29. Until KeyHit(1)
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. ;value, value min, value max, curve type (1=smooth out, 2=smooth in, 3=smooth both (default)), curve amplitute
  37. Function Curve#(val#,min#,max#,typ=3,amp#=1)
  38. val=val-min
  39. max=max-min
  40. If amp<>1 Then olval#=val
  41. tween#=((val/max)*90)
  42. If typ=<1 Then cos1#=Cos(tween-90):val=cos1*max                                 ;smooth out
  43. If typ=2 Then cos1#=1-Cos((tween)):val=cos1*max                                 ;smooth in
  44. If typ=3 Then cos1#=Cos(tween-90)*Sin(tween):val=cos1*max               ;smooth in and out
  45. If amp<>1 Then dif#=olval-val:val#=olval-(dif*amp) ;amplify
  46. Return val+min
  47. End Function


Comments :


Serpent(Posted 1+ years ago)

 Good code for smooth movement.  I'm assuming that in your example you've effectively created a diagram for time (x axis) versus distance moved (y axis).  The code works very well.  One thing that from what I can see you would have to look out for when using this code is that the amplitude parameter isn't too high.  From what I can see in this example, it results in a bounce effect which wouldn't be what people want most of the time.  Everything works out smoothly anyway - good job.


Streaksy(Posted 1+ years ago)

 thanks serpent.  yeh... the amplitude is more intended for values between 0 and 1.  I can't think of a reason someone would want higher, but the options there ;D


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal