Phisics Difficulties
cbxCan 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 [url]http://www.vbgamer.com/files/[/url] 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. [code] 'Created by: X 'E-Mail: mailto:createdbyx@yahoo.com 'Web: http://www.createdbyx.com/ 'Date: March 30, 2003 '============================================= <ComClass(GameTimer.ClassId, GameTimer.InterfaceId, GameTimer.EventsId)> _ Public Class GameTimer Private mlngLastCapturedTime As Long #Region "COM GUIDs" ' These GUIDs provide the COM identity for this class ' and its COM interfaces. If you change them, existing ' clients will no longer be able to access the class. 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() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. 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 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer Friend WithEvents Timer1 As System.Windows.Forms.Timer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.components = New System.ComponentModel.Container() Me.Timer1 = New System.Windows.Forms.Timer(Me.components) ' 'Timer1 ' ' 'Form1 ' 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 'Me.SetStyle(ControlStyles.DoubleBuffer Or ControlStyles.UserMouse Or ControlStyles.AllPaintingInWmPaint, True) 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 TimeStep As Long Dim objPos As PointF Dim TimeStamp As Single Dim LastTimeStamp As Single Dim TimeStep As Single Dim Force As PointF mobjTimer.CaptureTime() ' mobjBall.Velocity = New PointF(0, -1) While Timer1.Enabled And Me.Created 'Work out how long it has been since the last 'frame 'TimeStep = mobjTimer.TicksToMilli(mobjTimer.GetCurrentTime - mobjTimer.LastCapturedTime) TimeStep = (mobjTimer.GetCurrentTime - mobjTimer.LastCapturedTime) 'LastTimeStamp = TimeStamp 'TimeStamp = Microsoft.VisualBasic.Timer 'TimeStep = (TimeStamp - LastTimeStamp) * 20 ' update ball With mobjBall '.Velocity = mobjBox.Velocity '.Velocity = New PointF() Force.X = .Mass * mobjGravity.X Force.Y = .Mass * mobjGravity.Y ' objPos = New PointF(.Velocity.X * TimeStep, _ ' .Velocity.Y * TimeStep) objPos = New PointF(.Velocity.X + ((Force.X / .Mass) * TimeStep), _ .Velocity.Y + ((Force.Y / .Mass) * TimeStep)) .Velocity = objPos ' update position .Position = New PointF(.Position.X + (.Velocity.X * TimeStep), .Position.Y + (.Velocity.Y * TimeStep)) ' keep in bounds 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 ' draw items With Me.CreateGraphics .Clear(Me.BackColor) ' draw cuircles .DrawEllipse(New Pen(Color.Black), New RectangleF(mobjBall.Position.X - mobjBall.Raduis, _ mobjBall.Position.Y - mobjBall.Raduis, _ mobjBall.Raduis * 2, _ mobjBall.Raduis * 2)) ' draw rects .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 [/code]
cbxNevermind I got it to work. ... at least for now. [:p]