Can anyone assist me in pointing out why this source code does not work? The full source code is availible if requested.
I am trying to create a few simple phisics interfaces/objects. But I can't quite seem to get it to work. I think it might be the timer that causing it to not function?
I have reviewed a number of docs on luckys vb , and gamedev sites but they only serve to annoy me. All I want is a very short phisics example of a ball being propelled into the air and falling back down aka gravity.
None of these web site have that, there too busy copy and pasting off of each other and bla bla bla-ing on and on. I can't focus when people just shove numbers infront of me and expect me to get it. I learn best by watching then doing. or in this case stepping through a simple source example.
I have downloaded the vb6 phisics demo from this site Phisics.zip
http://www.vbgamer.com/files/ by andy owen but I don't like that it is not based on actual phisics. IE: F = ma etc. etc. I could convert it to vb.net but like i said i don't like that it uses "Verlet Integration" as opposed to "Euler Integration" whatever the hell that means!
I don't what to know what the names for all this stuff is I just want to know how to do it. I don't know the proper names for alot of the stuff I program against but yet I can still do it!
Am I even on the right track or is my code critically flawed? Any help is appreciated.
<ComClass(GameTimer.ClassId, GameTimer.InterfaceId, GameTimer.EventsId)> _
Public Class GameTimer
Private mlngLastCapturedTime As Long
#Region "COM GUIDs"
Public Const ClassId As String = "FA355364-E0B2-45F0-B12D-2CF6B00C09A4"
Public Const InterfaceId As String = "83E1ACD8-31D8-4AB7-B3A9-8929F711241E"
Public Const EventsId As String = "A269A864-CB3E-4226-A642-E4F452316F78"
#End Region
Public Overridable Function GetCurrentTime() As Long
Return Now.Ticks
End Function
Public Overridable ReadOnly Property LastCapturedTime() As Long
Get
Return mlngLastCapturedTime
End Get
End Property
Public Overridable Sub CaptureTime()
mlngLastCapturedTime = Now.Ticks
End Sub
Public Function MilliToTicks(ByVal Milliseconds As Long) As Long
Return Milliseconds * 10000
End Function
Public Function TicksToMilli(ByVal Ticks As Long) As Long
Return Ticks \ 10000
End Function
Public Sub New()
MyBase.new()
mlngLastCapturedTime = Now.Ticks
End Sub
End Class
Public Interface IPhisics
Property Mass() As Single
Property Velocity() As PointF
End Interface
Public Class WorldObject
Private mobjPosition As PointF
Public Property Position() As PointF
Get
Return mobjPosition
End Get
Set(Byval Value As PointF)
mobjPosition = Value
End Set
End Property
End Class
Public Class CircleObject
Inherits WorldObject
Implements IPhisics
Private msngMass As Single
Private mobjVelocity As PointF
Private msngRadius As Single
Public Property Mass() As Single Implements Phisics_1.IPhisics.Mass
Get
Return msngMass
End Get
Set(ByVal Value As Single)
msngMass = Value
End Set
End Property
Public Property Velocity() As System.Drawing.PointF Implements Phisics_1.IPhisics.Velocity
Get
Return mobjVelocity
End Get
Set(ByVal Value As System.Drawing.PointF)
mobjVelocity = Value
End Set
End Property
Public Property Raduis() As Single
Get
Return msngRadius
End Get
Set(Byval Value As Single)
msngRadius = Value
End Set
End Property
End Class
Public Class RectangleObject
Inherits WorldObject
Implements IPhisics
Private msngMass As Single
Private mobjVelocity As PointF
Private mobjSize As SizeF
Public Property Mass() As Single Implements Phisics_1.IPhisics.Mass
Get
Return msngMass
End Get
Set(ByVal Value As Single)
msngMass = Value
End Set
End Property
Public Property Velocity() As System.Drawing.PointF Implements Phisics_1.IPhisics.Velocity
Get
Return mobjVelocity
End Get
Set(ByVal Value As System.Drawing.PointF)
mobjVelocity = Value
End Set
End Property
Public Property Size() As SizeF
Get
Return mobjSize
End Get
Set(Byval Value As SizeF)
mobjSize = Value
End Set
End Property
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Private mobjBox As RectangleObject
Private mobjBall As CircleObject
Private mobjGravity As PointF = New PointF(0, 0.5)
Private mobjTimer As GameToolsR2.GameTimer
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(544, 454)
Me.ForeColor = System.Drawing.Color.Black
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mobjBall = New CircleObject()
mobjBox = New RectangleObject()
mobjTimer = New GameToolsR2.GameTimer()
DoReset()
Me.Refresh()
End Sub
Private Sub DoReset()
With mobjBall
.Mass = 0.1
.Position = New PointF(CSng(Me.Size.Width) / 2, 150)
.Raduis = 50
.Velocity = New PointF(0, 0.1)
End With
With mobjBox
.Mass = 0
.Position = New PointF(CSng(Me.Size.Width) / 2, 50)
.Size = New SizeF(100, 25)
.Velocity = New PointF(0, 0)
End With
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
Static last As Point
Me.Text = "X: " & (last.X - e.X).ToString & " - " & "Y: " & (last.Y - e.Y).ToString
last.X = e.X
last.Y = e.Y
mobjBox.Velocity = New PointF(last.X - e.X, last.Y - e.Y)
mobjBox.Position = New PointF(e.X, e.Y)
DrawItems()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim objPos As PointF
Dim TimeStamp As Single
Dim LastTimeStamp As Single
Dim TimeStep As Single
Dim Force As PointF
mobjTimer.CaptureTime()
While Timer1.Enabled And Me.Created
TimeStep = (mobjTimer.GetCurrentTime - mobjTimer.LastCapturedTime)
With mobjBall
Force.X = .Mass * mobjGravity.X
Force.Y = .Mass * mobjGravity.Y
objPos = New PointF(.Velocity.X + ((Force.X / .Mass) * TimeStep), _
.Velocity.Y + ((Force.Y / .Mass) * TimeStep))
.Velocity = objPos
.Position = New PointF(.Position.X + (.Velocity.X * TimeStep), .Position.Y + (.Velocity.Y * TimeStep))
KeepObjsInBounds()
End With
DrawItems()
Application.DoEvents()
mobjTimer.CaptureTime()
End While
End Sub
Private Sub KeepObjsInBounds()
Dim objPos As PointF
With mobjBall
objPos = .Position
If objPos.X < .Raduis Then
objPos.X = .Raduis
mobjBall.Velocity = New PointF(0, mobjBall.Velocity.Y)
End If
If objPos.X > Me.ClientSize.Width - .Raduis Then
objPos.X = Me.ClientSize.Width - .Raduis
mobjBall.Velocity = New PointF(0, mobjBall.Velocity.Y)
End If
If objPos.Y < .Raduis Then
objPos.Y = .Raduis
mobjBall.Velocity = New PointF(mobjBall.Velocity.X, 0)
End If
If objPos.Y > Me.ClientSize.Height - .Raduis Then
objPos.Y = Me.ClientSize.Height - .Raduis
mobjBall.Velocity = New PointF(mobjBall.Velocity.X, 0)
End If
.Position = objPos
End With
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.R
Timer1.Stop()
DoReset()
Case Keys.Space
If Timer1.Enabled Then Timer1.Stop() Else Timer1.Start()
Case Keys.Escape
Me.Close()
End Select
End Sub
Private Sub DrawItems()
If Not Me.Visible Then Exit Sub
With Me.CreateGraphics
.Clear(Me.BackColor)
.DrawEllipse(New Pen(Color.Black), New RectangleF(mobjBall.Position.X - mobjBall.Raduis, _
mobjBall.Position.Y - mobjBall.Raduis, _
mobjBall.Raduis * 2, _
mobjBall.Raduis * 2))
.DrawRectangle(New Pen(Color.Blue), New Rectangle(mobjBox.Position.X - mobjBox.Size.Width / 2, _
mobjBox.Position.Y - mobjBox.Size.Height / 2, _
mobjBox.Size.Width, _
mobjBox.Size.Height))
End With
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If Timer1.Enabled Then Exit Sub
DrawItems()
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
DrawItems()
End Sub
End Class