Ooops
October 24, 2021, 04:01:11

Author Topic: [bb] Smoothly moving object by Matt Merkulov [ 1+ years ago ]  (Read 551 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Smoothly moving object
Author : Matt Merkulov
Posted : 1+ years ago

Description : From article:<a href="http://blitzetcetera.org/index.php/Кубическая_интерполяция" target="_blank"> Cubic spline interpolation[/url] (rus)

Code :
Code: BlitzBasic
  1. ;Object in 2D smoothly passing thru random set of points visiting them in certain time (dual cubic spline interpolation) by Matt Merkulov
  2.  
  3. SeedRnd MilliSecs ()
  4.  
  5. Const q=10
  6.  
  7. ; As coordinates x and y are processed equally, files, where are created
  8. ; These coordinates and factors for functions on a current interval of time are stored(kept)
  9. Dim ptc#(q+2,1)
  10. Dim a#(1)
  11. Dim b#(1)
  12. Dim c#(1)
  13. Dim d#(1)
  14. ; A file for time in which the object should visit(attend) the set point
  15. Dim tim (q+2)
  16. ; A file of coordinates of object
  17. Dim oc#(1)
  18.  
  19. Graphics 800,600
  20. SetFont LoadFont ("Arial", 16)
  21.  
  22. x#=0
  23. y#=300
  24. Color 255,0,0
  25. For n=1 To q
  26.  x#=Rnd (50,750)
  27.  y#=Rnd (50,550)
  28.  ptc#(n, 0)=x#
  29.  ptc#(n, 1)=y#
  30.  tim (n)=t
  31.  Oval x#-4, y#-4,9,9
  32.  Text x#, y#+ 4, .001*t + "s", True
  33.  t=t+Rand (1000,3000)
  34. Next
  35.  
  36. ; Values of parameters of extreme points for maintenance of cyclicity are set
  37. For nn=0 To 1
  38.  ptc#(0, nn)=ptc#(q, nn)
  39.  ptc#(q+1, nn)=ptc#(1, nn)
  40.  ptc#(q+2, nn)=ptc#(2, nn)
  41. Next
  42. tim (q+1)=t
  43. tim (q+2)=t+tim (2)
  44. tim (0)=tim (q)-t
  45.  
  46. Color 255,255,255
  47. ; We keep a background
  48. i=CreateImage (800,600)
  49. GrabImage i, 0,0
  50. SetBuffer BackBuffer ()
  51.  
  52. ; time counter it is established(installed) outside an interval of a cycle, that
  53. ; To calculate factors for an initial interval of time, having passed(having taken place) through a condition
  54. t=t+1
  55. n=q+1
  56. Repeat
  57.  If t> tim (n+1) Then
  58.  n=n+1
  59.  ; If number of unit has fallen outside the limits a file - return on unit 1, zeroing
  60.  ; The counter of time
  61.  If n> q Then
  62.   n=1
  63.   ms=0
  64.   tbeg=MilliSecs ()
  65.  End If
  66.  For nn=0 To 1
  67.   d#(nn)=ptc#(n, nn)
  68.   c#(nn)=(ptc#(n+1, nn)-ptc#(n-1, nn)) / (tim (n+1)-tim (n-1))
  69.   dy2#=(ptc#(n+2, nn)-ptc#(n, nn)) / (tim (n+2)-tim (n))
  70.   x3#=tim (n+1)-tim (n)
  71.   xx3#=x3#*x3#
  72.   b#(nn)=(3*ptc#(n+1, nn)-dy2#*x3#-2*c#(nn)*x3#-3*d#(nn))/xx3#
  73.   a#(nn)=(dy2#-2*b#(nn)*x3#-c#(nn)) / (3*xx3#)
  74.  Next
  75.  End If
  76.  
  77.  ; Calculation of coordinates of object
  78.  For nn=0 To 1
  79.  v#=t-tim (n)
  80.  vv#=v#*v#
  81.  oc#(nn)=a#(nn)*vv#*v#+ b#(nn)*vv#+ c#(nn)*v#+ d#(nn)
  82.  Next
  83.  
  84.  ; Display of a background, object and current time
  85.  DrawBlock i, 0,0
  86.  Oval oc#(0)-9, oc#(1)-9,19,19
  87.  Text 0,0, " Time: " + (.001*t) + "s"
  88.  Flip
  89.  
  90.  t=MilliSecs ()-tbeg
  91. Until KeyHit (1)


Comments :


Naughty Alien(Posted 1+ years ago)

 nice..3D version will be fine to o


impixi(Posted 1+ years ago)

 Excellent.


impixi(Posted 1+ years ago)

 Here's a BlitzMax port + modification, if anybody is interested:
Code: [Select]

SuperStrict

Graphics 1024, 768

SeedRnd MilliSecs()

Local Paths:TPath[] = createPaths(3)

While Not KeyHit(KEY_ESCAPE)

If MouseHit(1) Or MouseHit(2)

Paths = Null
GCCollect

Paths = createPaths(3)

EndIf

renderPaths Paths

Wend

End

Function createPaths:TPath[](qty:Int = 1)

Local paths:TPath[] = New TPath[qty]

For Local n:Int = 0 To paths.length - 1

paths[n] = New TPath

paths[n].init Rand(3, 10)
paths[n].generateCoords

Next

Return paths

EndFunction

Function renderPaths(paths:TPath[])

Cls

SetColor 255, 255, 255
DrawText "Click mouse button for a new set of paths.", 10, 10

Local t:Int = MilliSecs()

For Local p:TPath = EachIn paths
p.process t
p.render
Next

Flip True

EndFunction


'********


Type TPath

Field Qty:Int

Field Coords:TCoord[]

Field CurrentPosition:TCoord
Field TimeNow:Int

'**** "Scratchpad" variables
Field a:TCoord
Field b:TCoord
Field c:TCoord
Field d:TCoord
Field n:Int
Field tbeg:Int
'*****

'**** For rendering only
Field red:Byte
Field green:Byte
Field blue:Byte
'****

Method init(q:Int = 5)

Qty = q

Coords = New TCoord[Qty + 3]

For Local n:Int = 0 To Coords.length - 1
Coords[n] = TCoord.create()
Next

CurrentPosition = New TCoord

a = New TCoord
b = New TCoord
c = New TCoord
d = New TCoord
n = Qty + 1

red = Rand(128, 255)
green = Rand(0, 255)
blue = Rand(0, 255)

EndMethod

Method generateCoords()

Local t:Int = 0

For Local n:Int = 1 To Qty
Coords[n].set Rnd(50, GraphicsWidth() - 50), Rnd(50, GraphicsHeight() - 50), t
t :+ Rand(1000, 3000)
Next

Coords[0].set Coords[Qty].X, Coords[Qty].Y, Coords[Qty].Time - t
Coords[Qty + 1].set Coords[1].X, Coords[1].Y, t
Coords[Qty + 2].set Coords[2].X, Coords[2].Y, Coords[2].Time + t

EndMethod

Method render()

SetColor red, green, blue
For Local n:Int = 1 To Qty
DrawOval Coords[n].X - 4, Coords[n].Y - 4, 9, 9
DrawText n, Coords[n].X, Coords[n].Y + 4
Next

DrawOval CurrentPosition.X - 9, CurrentPosition.Y - 9, 19, 19

EndMethod

Method process(t:Int)

TimeNow = t - tbeg

If TimeNow > Coords[n+1].Time

n :+ 1

If n > Qty
n = 1
  tbeg = t
  End If

d.set Coords[n].X, Coords[n].Y
c.set ((Coords[n+1].X - Coords[n-1].X) / (Coords[n+1].Time - Coords[n-1].Time)), ((Coords[n+1].Y - Coords[n-1].Y) / (Coords[n+1].Time - Coords[n-1].Time))

Local dy2:TCoord = New TCoord
dy2.set ((Coords[n+2].X - Coords[n].X) / (Coords[n+2].Time - Coords[n].Time)), ((Coords[n+2].Y - Coords[n].Y) / (Coords[n+2].Time - Coords[n].Time))

Local x3# = Coords[n+1].Time - Coords[n].Time
  Local xx3# = x3# * x3#

b.set ((3 * Coords[n+1].X - dy2.X * x3# - 2 * c.X * x3# - 3 * d.X) / xx3#), ((3 * Coords[n+1].Y - dy2.Y * x3# - 2 * c.Y * x3# - 3 * d.Y) / xx3#)
a.set ((dy2.X - 2 * b.X * x3# - c.X) / (3 * xx3#)), ((dy2.Y - 2 * b.Y * x3# - c.Y) / (3 * xx3#))

  End If

Local v# = TimeNow - Coords[n].time
Local vv# = v# * v#
CurrentPosition.set (a.X * vv# * v# + b.X * vv# + c.X * v# + d.X), (a.Y * vv# * v# + b.Y * vv# + c.Y * v# + d.Y)

EndMethod

EndType


Type TCoord

Field X:Float
Field Y:Float
Field Time:Int

Function create:TCoord(px:Float = 0.0, py:Float = 0.0, t:Int = 0.0)

Local c:TCoord = New TCoord

c.set px, py, t

Return c

EndFunction

Method set(px:Float = 0.0, py:Float = 0.0, t:Int = 0.0)

X = px
Y = py
Time = t

EndMethod

EndType



 

SimplePortal 2.3.6 © 2008-2014, SimplePortal