Click here to Skip to main content
15,885,214 members
Articles / Programming Languages / Visual Basic 10

TB-Stickfigure

Rate me:
Please Sign up or sign in to vote.
2.81/5 (10 votes)
2 Oct 2009CPOL1 min read 27.7K   545   11   6
Combine graphic objects to create own scenes
Image 1

Introduction

This article was made for testing how to simply create realism looking objects by line painting.

A problem I would to solve in VB.NET is the animation performance. Because of keeping vector datas, it should be easy to create the animation with good performance, or sending the data to any 3D software like Truespace or Maya.

Background

The main idea was to make animations driven by external programs, music, joybad controller, etc. 

How will your music visualization look if your own objects will dance for Bass drum, melody and hihats? That was my question.

This is a good document for keeping the proportions.

Using the Code

A main view should be taken to the clsStickStructure.vb Class. It save the properties of any point. It also saves if the point will be in reference to any other point. So it is possible to make the animation with simple kinematics.

While moving the mouse, the selection has to be fixed to a grid to create models faster:

VB.NET
If MoveIndex > -1 Then
    If e.Button = Windows.Forms.MouseButtons.Left _
	Then Call MoveObjectToPosition(MoveIndex, New Point(e.X, e.Y), False)
    If e.Button = Windows.Forms.MouseButtons.Right _
	Then Call MoveObjectToPosition(MoveIndex, New Point(e.X, e.Y), True)
End If 

To make the reference to the points, it is necessary to keep an object for each point.

VB.NET
Private Sub MoveObjectToPosition(ByVal ObjectIndex As Integer, ByVal Pos As PointF, _
   ByVal MoveFullObject As Boolean)
       Dim X, Y As Integer
       Dim DiffX, DiffY As Integer
       'Dim item2 As StickStructure
       Dim i As Integer

       X = CInt(Pos.X / GridSize) * GridSize
       Y = CInt(Pos.Y / GridSize) * GridSize

       X = X - MittX
       Y = Y - MittY
       If Stick.Points IsNot Nothing _
       AndAlso UBound(Stick.Points)>= MoveIndex Then
           If LastPoint.Pos2Active = True Then
               DiffX = Stick.Points(MoveIndex).Pos2Relativ.X - X
               DiffY = Stick.Points(MoveIndex).Pos2Relativ.Y - Y
               Stick.Points(MoveIndex).Pos2Relativ = New PointF(X, Y)

               If MoveFullObject = True Then
                   Stick.Points(MoveIndex).Pos1Relativ = _
           New PointF(Stick.Points(MoveIndex).Pos1Relativ.X - DiffX, _
           Stick.Points(MoveIndex).Pos1Relativ.Y - DiffY)

                   If Stick.Points(MoveIndex).Pos1LinkToIndexToP1 > -1 _
            Then Stick.Points(Stick.Points(MoveIndex).Pos1LinkToIndexToP1)._
           Pos1Relativ = Stick.Points(MoveIndex).Pos1Relativ
                   If Stick.Points(MoveIndex).Pos1LinkToIndexToP2 > -1 _
            Then Stick.Points(Stick.Points(MoveIndex).Pos1LinkToIndexToP2)._
           Pos2Relativ = Stick.Points(MoveIndex).Pos1Relativ
               End If

           Else
               DiffX = Stick.Points(MoveIndex).Pos1Relativ.X - X
               DiffY = Stick.Points(MoveIndex).Pos1Relativ.Y - Y
               Stick.Points(MoveIndex).Pos1Relativ = New PointF(X, Y)

               If MoveFullObject = True Then
                   Stick.Points(MoveIndex).Pos2Relativ.X = _
           Stick.Points(MoveIndex).Pos2Relativ.X - DiffX
                   Stick.Points(MoveIndex).Pos2Relativ.Y = _
           Stick.Points(MoveIndex).Pos2Relativ.Y - DiffY

                   If Stick.Points(MoveIndex).Pos2LinkToIndexToP1 > -1 _
           Then Stick.Points(Stick.Points(MoveIndex)._
           Pos2LinkToIndexToP1).Pos1Relativ = _
           Stick.Points(MoveIndex).Pos1Relativ
                   If Stick.Points(MoveIndex).Pos2LinkToIndexToP2 > -1 _
           Then Stick.Points(Stick.Points(MoveIndex)._
           Pos2LinkToIndexToP2).Pos2Relativ = _
           Stick.Points(MoveIndex).Pos1Relativ
               End If
           End If

           For i = 0 To Stick.Points.Length - 1
               If i <> MoveIndex Then
                   If Stick.Points(i).Pos1LinkToIndexToP1 = _
           Stick.Points(MoveIndex).Id Then Stick.Points(i).Pos1Relativ = _
           Stick.Points(MoveIndex).Pos1Relativ
                   If Stick.Points(i).Pos1LinkToIndexToP2 = _
           Stick.Points(MoveIndex).Id Then Stick.Points(i).Pos1Relativ = _
           Stick.Points(MoveIndex).Pos2Relativ
                   If Stick.Points(i).Pos2LinkToIndexToP1 = _
           Stick.Points(MoveIndex).Id Then Stick.Points(i).Pos2Relativ = _
           Stick.Points(MoveIndex).Pos1Relativ
                   If Stick.Points(i).Pos2LinkToIndexToP2 = _
           Stick.Points(MoveIndex).Id Then Stick.Points(i).Pos2Relativ = _
           Stick.Points(MoveIndex).Pos2Relativ
               End If
           Next
       End If
   End Sub

   Private Sub PaintMousePosition(ByVal Pos As PointF)
       If Me.pic.Image Is Nothing Then Exit Sub
       Dim i As Integer = 5
       Pos.X = CInt(Pos.X / Me.GridSize) * Me.GridSize
       Pos.Y = CInt(Pos.Y / Me.GridSize) * Me.GridSize
       Dim P As New Pen(Color.FromArgb(100, 90, 90, 90), 1)
       Dim PBlue As New Pen(Color.FromArgb(90, 90, 90, 255), 1)
       PBlue.DashStyle = Drawing2D.DashStyle.DashDotDot

       Using gr As Graphics = Graphics.FromImage(Me.pic.Image)
           'Cross
           gr.DrawLine(P, CInt(Pos.X - i), CInt(Pos.Y - i), CInt(Pos.X + i), _
           CInt(Pos.Y + i))
           gr.DrawLine(P, CInt(Pos.X + i), CInt(Pos.Y - i), CInt(Pos.X - i), _
           CInt(Pos.Y + i))

           'Horizontal line
           gr.DrawLine(PBlue, 0, Pos.Y, Me.pic.Image.Width, Pos.Y)

           'Vertical line
           gr.DrawLine(PBlue, Pos.X, 0, Pos.X, pic.Image.Height)

       End Using
   End Sub

To select the next point object, the following function will help: 

VB.NET
Private Function GetNextFreeItemIdNumber()
        Dim i As Integer
        Dim res As Integer = 1
        Dim S As clsStickStructure.StickPoint

        If Stick.Points Is Nothing OrElse Stick.Points.Length = 0 Then Return 1

        For i = 0 To Stick.Points.Length - 1
            S = Stick.GeItemById(Stick.Points(i).Id)
            If S.Equals(Nothing) Then
                Exit For
            Else
                res = S.Id + 1
            End If
        Next
        Return res
    End Function

Points of Interest

I think VB.NET is very easy to use for making such an tool. And after having the stick figure objects as data, it could be very quick for any animation.   

History

  • 2nd October, 2009: Initial post

The tool is prepared to fix the angles in the project. But I had no time to include it until today. Hopefully someone has fun working on the project. The animation control would be a nice tool for the future too.  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Gun Gun Febrianza19-Nov-14 20:55
Gun Gun Febrianza19-Nov-14 20:55 
GeneralIt looks cool, but... Pin
Shane Story5-Oct-09 6:09
Shane Story5-Oct-09 6:09 
GeneralMy Vote of 4 Pin
sam.hill2-Oct-09 18:30
sam.hill2-Oct-09 18:30 
GeneralRe: My Vote of 4 Pin
Dave Kreskowiak11-Oct-09 6:49
mveDave Kreskowiak11-Oct-09 6:49 
GeneralMy vote of 1 Pin
gbraux2-Oct-09 11:00
gbraux2-Oct-09 11:00 
GeneralMy vote of 1 Pin
Dave Kreskowiak2-Oct-09 10:18
mveDave Kreskowiak2-Oct-09 10:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.