Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

TB-Stickfigure

0.00/5 (No votes)
2 Oct 2009 1  
Combine graphic objects to create own scenes

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:

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.

 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: 

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here