Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Rickwi22

Class based physics with VB

This topic is 5284 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is my first attempt at trying to do OO programming. Ive always been interested in phsyics so I thought I would try to learn by making a class for different types of object such as Circles and Squares and Lines and such and then have them interact correctly. So far this is what Ive got, but I think I am going about this the wrong way. Id appreciate any suggestions on how I can make it easier to work with and make more sense before I get to far into the project. I would also like to know if I am handling the classes corrrectly since I have never done this before, In the code for Form1
Const NumCircles As Integer = 3
Dim Circles(1 To NumCircles) As New CCircle
Dim Gravity As New Point

Private Sub Form_Load()
Gravity.Y = -2
Dim counter As Integer
While counter < NumCircles
counter = counter + 1
Circles(counter).LoadCircle counter * 600, 1000, 300
Wend
End Sub

Private Sub Timer1_Timer()
Cls
Dim counter As Integer
While counter < NumCircles
counter = counter + 1
If Circles(counter).CLoc.Y - Circles(counter).CRadius < 0 Then
Circles(counter).CLoc.Y = Circles(counter).CRadius
Dim Rebound As New Point
Rebound.Y = Circles(counter).CVel.Y * -1.5
Circles(counter).ApplyForce Rebound, 1
End If
If Circles(counter).CLoc.X - Circles(counter).CRadius < 0 Then
Circles(counter).CLoc.X = Circles(counter).CRadius
Dim LeftRebound As New Point
Rebound.X = Circles(counter).CVel.X * -1.5
Circles(counter).ApplyForce Rebound, 1
End If
If Circles(counter).CLoc.X - Circles(counter).CRadius > 5000 Then
Circles(counter).CLoc.X = Circles(counter).CRadius
Dim RightRebound As New Point
Rebound.Y = Circles(counter).CVel.X * -1.5
Circles(counter).ApplyForce Rebound, 1
End If
Circles(counter).ApplyForce Gravity, 1
Circles(counter).Update
Circles(counter).DrawCircle
Wend
End Sub
 
In the Class Module for Circle
Public CRadius As Integer
Public CLoc As New Point
Public CVel As New Point
Public CAccel As New Point
Public Sub DrawCircle()
Form1.Circle (CLoc.X, CLoc.Y), CRadius, RGB(0, 0, 0)
End Sub

Public Sub Update()
CVel.Add CAccel
Dim Canceller As New Point
Canceller.X = CAccel.X * -1
Canceller.Y = CAccel.Y * -1
CAccel.Add Canceller
CLoc.Add CVel
End Sub

Public Sub ApplyForce(Normal As Point, Magnitude As Integer)
CAccel.Add Normal
End Sub


Public Sub LoadCircle(X As Integer, Y As Integer, Radius As Integer)
CLoc.X = X
CLoc.Y = Y
CRadius = Radius
End Sub

 
In the class module for Point
Public X As Integer

Public Y As Integer

Public Sub Add(Point2 As Point)
X = X + Point2.X
Y = Y + Point2.Y
End Sub

 

Share this post


Link to post
Share on other sites
Advertisement
I didn''t review 100% of your code, but let me give you some pointers.

1. Never use public member variables in a class. The reason is that (it appears your using VB 6.0 not .Net) public variables within a Class are not thread safe. They are if you are using them in a module, depending on your class instancing. You should make them private and use Property Let/Set/Get (let is for non objects and Set is for objects)

That means if its say an integer you can use let myvar = whatever was passed in
If its an object then use Set myobject = whatever was passed in.

Not a requirement but you should utilize functions in cases where you want to validate that what you got back completed successfully you should use a function to return back a "result" boolean/integer whatever.

however, you also need error handling in your code.

Since Vb 6.0 doesn''t support structured error handling you need to use either on error resume next. This turns off error notification, but does allow you to check the err object.
On error goto 0, clears the error object(or better yet doesn''t set it with error information).
Or in your case, you may want to add an error handler to validate what is passed in such as

Public Function Boo(Byval iX as intege,r Byval iY as integer) as boolean

on error goto Boo_Error

if iX < somevalue or iY is < some value or iX > somevalue or iY > then some value then
return false
end if

myclass.x(remember make it private) = iX
myclass.y = iY

return true
exit function
Boo_Error:
return false

end function


Also know that variables etc in a form are only accessible to that form by default and even if public are only accessible through a direct call through the forms collection.

By utilizing a module you can create a better organized set of helper functions if needed.

To be honest... there are about 10000000000 things when it comes to programming esp OOP and I can''t explain them all here but I hope it helps a little.


Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!