A little shape interpolation snippet

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

A little shape interpolation snippet

Postby Hezad » Sep 30, 2008 0:52

Hey :)

Well everything is in the title !

Nothing really serious, the code could be much better written and it's not particularly interesting, graphically speaking, but I just wanted to play with Interpolation.

( Interpolation discovery has been an illumination for me :) it's awesome all you can do with this simple concept ! )

Code: Select all

Const MAX_PNT = 20

Randomize timer

Type PntType
    as single x,y
End Type

Type ShapeType
    as integer NbPoints
   
    as PntType Center
   
    as PntType V
   
    as PntType Pnt0(MAX_PNT)
    as PntType Pnt(MAX_PNT)
   
End Type

Sub InterpolateShape(ByRef S1 as ShapeType, Byref S2 as ShapeType, ByVal t as single)
   
    Dim as integer dX,dY
   
    If S1.nbPoints<>S2.NbPoints then exit sub
   
    For i as integer = 1 to S1.NbPoints
       
        dX = S2.Pnt0(i).x - S1.Pnt0(i).x
        dY = S2.Pnt0(i).y - S1.Pnt0(i).y
       
        S1.Pnt(i).x = S1.Pnt0(i).x + t*dX
        S1.Pnt(i).y = S1.Pnt0(i).y + t*Dy
       
    next
   
End sub

Sub Clip(S as ShapeType)
   
    For i as integer = 1 to S.NbPoints
       
        If S.Center.x+S.pnt(i).x>320 or S.Center.x+S.pnt(i).x<0 then
            S.V.X*=-1
            S.Center.x+=S.v.x
        end if
       
        If S.Center.y+S.pnt(i).y>240 or S.Center.y+S.pnt(i).y<0 then
            S.V.Y*=-1
            S.Center.y+=S.v.y
        end if
       
    next

End sub

Sub RecallShape(S as ShapeType)
   
    For i as integer = 1 to S.NbPoints-1
       
        S.pnt(i) = S.pnt0(i)

    next

End Sub

Sub RenderShape(Byref S as ShapeType,t as single)
   
    For i as integer = 1 to S.NbPoints-1
       
        Line(S.Center.x+S.Pnt(i).x,S.Center.y+S.Pnt(i).y)-(S.Center.x+S.Pnt(i+1).x,S.Center.y+S.Pnt(i+1).y),rgb(t*250,100+150*t,200-100*t)

    next

    Line(S.Center.x+S.Pnt(S.NbPoints).x,S.Center.y+S.Pnt(S.NbPoints).y)-(S.Center.x+S.Pnt(1).x,S.Center.y+S.Pnt(1).y),rgb(t*250,100+150*t,200-100*t)

    Paint(S.Center.X,S.Center.Y),rgb(t*250,100+150*t,200-100*t)
End sub

Screenres 320,240,32,2

Dim as ShapeType Shape1, Shape2

Shape1.NbPoints = 8
Shape2.NbPoints = 8

Shape1.V=type(-.5+rnd/2,-.5+rnd/2)

Shape1.Center = type(150,150)
Shape2.Center = type(150,150)

Shape1.Pnt0(1) = type(-50,-50)
Shape1.Pnt0(2) = type(0,-50)
Shape1.Pnt0(3) = type(50,-50)
Shape1.Pnt0(4) = type(50,0)
Shape1.Pnt0(5) = type(50,50)
Shape1.Pnt0(6) = type(0,50)
Shape1.Pnt0(7) = type(-50,50)
Shape1.Pnt0(8) = type(-50,0)

For i as integer = 1 to 8
    Shape2.Pnt0(9-i) = Type(50*cos(i*(6.28/8)+3*3.14/2),50*sin(i*(6.28/8)-3*3.14/2))
next

dim as single t,sign=1

Do : screenlock : cls
   
    Shape1.Center.x+=Shape1.v.x
    Shape1.Center.y+=Shape1.v.y
   
    clip Shape1
   
    InterpolateShape(Shape1,Shape2,t)
       
    RenderShape Shape1,t
   
    t+=.0008*sign : if t>=1 or t<=0 then sign*=-1
   
    Screenunlock : sleep 1,1
Loop until multikey(&h01)
Nexinarus
Posts: 146
Joined: May 28, 2005 6:08
Location: Everywhere
Contact:

Postby Nexinarus » Sep 30, 2008 5:38

heh nice. You can also do 2d line or vector character animation, and ID software did their 3D animation in Quake by interpolating their 3d models vertices ;P .
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Postby 1000101 » Sep 30, 2008 13:09

One of the screen savers that came with Windows since 95 (or was it not until 98?) was the "3D FlowerBox" which did interpolation a simple cube to make different shapes.
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Postby Hezad » Sep 30, 2008 18:26

They come from the future. They stole my code. I saw them !


ID software did their 3D animation in Quake by interpolating their 3d models vertices


'didn't know that, cool :D
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Postby TheMG » Oct 07, 2008 18:19

I'm gonna try that interpolation idea (on models), sounds useful!
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Postby Hezad » Oct 07, 2008 21:18

'glad it can be useful :) I hope to see soon your 3D shape interpolation !
TheMG
Posts: 376
Joined: Feb 08, 2006 16:58

Postby TheMG » Oct 07, 2008 21:21

You wont see it here... I don't program games in FB, I use a RAD game development engine (aptly named Game Maker) which makes it much quicker to develop 3d stuff.
Veggiet
Posts: 156
Joined: Apr 17, 2006 19:41

Postby Veggiet » Oct 11, 2008 13:40

cool demo.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 2 guests