2 Replies - 11671 Views - Last Post: 04 June 2010 - 12:46 AM Rate Topic: -----

#1 Recoil  Icon User is offline

  • D.I.C Regular

Reputation: 24
  • View blog
  • Posts: 292
  • Joined: 28-June 08

Dice animation

Posted 22 May 2010 - 12:18 PM

Searched, scoured, Googled, and nothing.

I'm trying to figure out how to have a dice animation for a program. I know how to randomize, as well flash the pictures in a picture box to have the appearance of rolling, but it doesn't appear to roll across the board, slowly landing on one of the sides. Currently I am trying to accomplish this with vb.net.

I have thought about making dice in 3ds max, then just having them drop each time in a window or box, but I haven't the slightest clue about returning the result of the top of the die to the program, let alone how to be able to import 3ds max files in vb.net, which is useless anyways if I can't return the result of the die side facing up.

I am hoping someone here has an idea of where to look, or a term I should be looking for as searching for "vb.net animated dice" "vb.net rolling dice" and others is turning up no help with this. Out of everything dealing with a game in vb.net I would figure that locating an example would be easy, but all I can come up with is examples for just returning random numbers, which I already know how to do.

This part of the program is pretty much eye-candy for the end user...it doesn't really affect the way anything is done, but it would be way better than just clicking a button and getting a number.

Is This A Good Question/Topic? 0
  • +

Replies To: Dice animation

#2 Recoil  Icon User is offline

  • D.I.C Regular

Reputation: 24
  • View blog
  • Posts: 292
  • Joined: 28-June 08

Re: Dice animation

Posted 23 May 2010 - 07:39 PM

Since all of this didn't come from me and I had to use external sources I'll go ahead and list them for others to reference.


Make your own breakout style game in VB.NET
Creating a fully-functional pong game.
Rotate an image in VB .NET using DrawImage
Redneck Slots

Here's the code that goes on Form1:
Imports System.Math

Public Class Form1

    Dim randNum1 As Integer
    Dim randNum2 As Integer
    Dim numPics As Integer = 6
    Dim count1 As Integer
    Dim pic1 As String
    Dim speed As Single = 20
    Dim rndInst As New Random()
    Dim xVel As Single = Math.Cos(rndInst.Next(5, 10)) * speed
    Dim yVel As Single = Math.Sin(rndInst.Next(5, 10)) * speed

    Private Sub tmrDie_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrDie.Tick
        'Start the random number generator

        'Assign a random number to a variable
        randNum1 = CInt(Int((numPics * Rnd()) + 1))

        'Show the picture for the random number
        Select Case randNum1
            Case 1
                pbxSixSided.Image = iLstSixDie.Images.Item(0)
            Case 2
                pbxSixSided.Image = iLstSixDie.Images.Item(1)
            Case 3
                pbxSixSided.Image = iLstSixDie.Images.Item(2)
            Case 4
                pbxSixSided.Image = iLstSixDie.Images.Item(3)
            Case 5
                pbxSixSided.Image = iLstSixDie.Images.Item(4)
            Case 6
                pbxSixSided.Image = iLstSixDie.Images.Item(5)
        End Select

        'Count 1 for every round
        count1 += 1

        'If the count equals the number of rotations,
        '   then stop the wheel, and reset the count
        If count1 = Val(txtRotations.Text) Then
            tmrDie.Enabled = False
            count1 = 0
        End If

        'Move the picture box.

        'Rotate the picture in the picture box.

    End Sub

    Private Sub btnRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRoll.Click
        tmrDie.Enabled = True
        tmrDie.Interval = Val(txtInterval.Text)
    End Sub

    Public Sub MovePic()
        ' Move the picturebox.
        pbxSixSided.Location = New Point(pbxSixSided.Location.X - xVel, pbxSixSided.Location.Y + yVel)

        ' Check for top.
        If pbxSixSided.Location.Y < 0 Then
            pbxSixSided.Location = New Point(pbxSixSided.Location.X, 0)
            yVel = -yVel
        End If

        ' Check for bottom.
        If pbxSixSided.Location.Y > Panel1.Bottom - (pbxSixSided.Height + 32) Then
            pbxSixSided.Location = New Point(pbxSixSided.Location.X, Panel1.Height - pbxSixSided.Size.Height)
            yVel = -yVel
        End If

        'Check for left.
        If pbxSixSided.Location.X < 0 Then
            pbxSixSided.Location = New Point(0, pbxSixSided.Location.Y)
            xVel = -xVel
        End If

        'Check for right.
        If pbxSixSided.Location.X + pbxSixSided.Width > Panel1.Width Then
            pbxSixSided.Location = New Point(Panel1.Width - pbxSixSided.Width, _
            xVel = -xVel
        End If
    End Sub

    Public Sub Rotate()
        'Copy the output bitmap from the source image.
        Dim startPic As New Bitmap(pbxSixSided.Image)

        'Make an array of points defining the image's corners.
        Dim wid As Single = startPic.Width
        Dim hgt As Single = startPic.Height
        Dim corners As Point() = { _
            New Point(0, 0), _
            New Point(wid, 0), _
            New Point(0, hgt), _
            New Point(wid, hgt)}

        'Translate to center the bounding box at the origin.
        Dim cx As Single = wid / 2
        Dim cy As Single = hgt / 2
        Dim i As Long
        For i = 0 To 3
            corners(i).X -= cx
            corners(i).Y -= cy
        Next i

        randNum2 = CInt(Int((360 * Rnd()) + 1))
        Dim theta As Single = Single.Parse(randNum2) * PI / 180.0

        Dim sin_theta As Single = Sin(theta)
        Dim cos_theta As Single = Cos(theta)
        Dim X As Single
        Dim Y As Single
        For i = 0 To 3
            X = corners(i).X
            Y = corners(i).Y
            corners(i).X = X * cos_theta + Y * sin_theta
            corners(i).Y = -X * sin_theta + Y * cos_theta
        Next i

        'Translate so X >= 0 and Y >=0 for all corners.
        Dim xmin As Single = corners(0).X
        Dim ymin As Single = corners(0).Y
        For i = 1 To 3
            If xmin > corners(i).X Then xmin = corners(i).X
            If ymin > corners(i).Y Then ymin = corners(i).Y
        Next i
        For i = 0 To 3
            corners(i).X -= xmin
            corners(i).Y -= ymin
        Next i

        'Create an output Bitmap and Graphics object.
        Dim finishPic As New Bitmap(CInt(-2 * xmin), CInt(-2 * ymin))
        Dim picOut As Graphics = Graphics.FromImage(finishPic)

        'Drop the last corner lest we confuse DrawImage, 
        'which expects an array of three corners.
        ReDim Preserve corners(2)

        'Draw the result onto the output image.
        picOut.DrawImage(startPic, corners)

        'Display the result.
        pbxSixSided.Image = finishPic
    End Sub

End Class

And you need the following controls:
PictureBox - pbxSixSided - 48 x 48
Panel1 - about 300 x 300
Button - btnRoll
Textbox - txtRotations
Textbox - txtInterval
ImageList - iLstSixDie
Timer - tmrDie

You will also need 6 die face images. I made mine in paint at 32 x 32. They go in order in the ImageList.

The only problem I am having is that on occasion the die will go to the top or the bottom, and just run across from side to side. It doesn't do it every time, but this is a minor issue and still returns the result. This might not be exactly like I was wanting, but is as close as I could come to it while looking half decent. I hope this helps someone else :)

Attached File  RollingDiceTest.zip (79.12K)
Number of downloads: 801
Was This Post Helpful? 0
  • +
  • -

#3 calvinthedestroyer  Icon User is offline

  • D.I.C Lover

Reputation: 166
  • View blog
  • Posts: 1,904
  • Joined: 13-October 07

Re: Dice animation

Posted 04 June 2010 - 12:46 AM

I was working on something similar but I decided to cheat and use animation.

But here is what I had if it might help you out:

I created this image from an animated gif of 4 dices being thrown into the air. The dice are naturally spinning as they fly through the air Instead of randomly rotating. I copied all of the dice on to this chart. The next step would be to plot all the the corners on the dice and then to list all those plots into an array or data set.

Here is the original gif by joebody (i don't know him, I just found this gif on the net)
Attached Image

I know that my post maybe different than what you are looking for but I hope that it helps you out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1