Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'nan means not a number
- Module Module1
- Public Structure vector
- Dim x As Double
- Dim y As Double
- Dim z As Double
- End Structure
- Public Structure objects ' these are all the variables needed for an indavidual object
- Dim position As vector
- Dim velocity As vector
- Dim mass As Double
- End Structure
- Sub Main()
- Const G As Double = -0.0000000000667384 ' newtons gravitational constant
- Dim bitrate As Int64 ' 1/bitrate = the time that acceloration is assumed for
- bitrate:
- Try
- Console.WriteLine("Please enter how many fractions of a second you want to asume the acceloration is constant for? 1/this number = the number of seconds the acceloration is constant for...")
- bitrate = Console.ReadLine()
- Catch
- Console.WriteLine("A user error occured, this is most likely becasue you did not enter a integer value!")
- GoTo bitrate
- End Try
- Dim NumberOfObjects As Integer ' the number of objects in the simulation
- NumberOfObjects:
- Try
- Console.WriteLine("How many objects?") ' getting the person to enter the number of objects
- NumberOfObjects = Console.ReadLine() ' retiving the number of objects
- Catch
- Console.WriteLine("A user error occured, this is most likely because the last entry was not an integer value!")
- GoTo NumberOfObjects
- End Try
- Dim runtime As Int64 ' this is the amount of runs the computer has currently run
- Dim runlength As Int64 'the total number of runs the computer will run
- Dim i As Integer ' a counter used mainly for the current object
- Dim n As Integer ' a counter used for the object acting on the current object
- Dim sumofaceloration As vector ' sum of the acceloration acting in one direction
- Dim objects(NumberOfObjects - 1) As objects ' definning the objects
- 'getting the input data
- Console.WriteLine("Please enter the following:")
- For i = 0 To NumberOfObjects - 1
- objects:
- Try
- Console.WriteLine("Object " & i + 1 & "'s x position...")
- objects(i).position.x = Console.ReadLine()
- Console.WriteLine("Object " & i + 1 & "'s y position...")
- objects(i).position.y = Console.ReadLine()
- Console.WriteLine("Object " & i + 1 & "'s z position...")
- objects(i).position.z = Console.ReadLine()
- Console.WriteLine("Object " & i + 1 & "'s x velocity...")
- objects(i).velocity.x = Console.ReadLine()
- Console.WriteLine("Object " & i + 1 & "'s y velocity...")
- objects(i).velocity.y = Console.ReadLine()
- Console.WriteLine("Object " & i + 1 & "'s z velocity...")
- objects(i).velocity.z = Console.ReadLine()
- Console.WriteLine("Object " & i + 1 & "'s mass...")
- objects(i).mass = Console.ReadLine()
- Catch
- Console.WriteLine("One or more of the numbers you entered for the last object where incorrect please try again!")
- GoTo objects
- End Try
- Next
- constinues:
- Try
- Console.WriteLine("How long do you want the simulation to go on for? The number you type in will be the number of seconds that the simulation is run for, this must be an integer...")
- runlength = Console.ReadLine() * bitrate
- Console.WriteLine("Total loops needed: " & runlength)
- Catch
- Console.WriteLine("The last number you entered contained an error! It should have been an integer value representing the number of seconds you would like to run the simulation for!")
- GoTo bitrate
- End Try
- 'running the simulation
- For runtime = 1 To runlength
- For i = 0 To NumberOfObjects - 1 ' calculate the movement of objects
- sumofaceloration.x = 0 ' making sure thar the last acceloration isn't put into this one
- sumofaceloration.y = 0
- sumofaceloration.z = 0
- For n = 0 To NumberOfObjects - 1 ' sumation of aceloration in x, over n except when x = n
- If n <> i Then ' to stop the program working out the force due to itself
- 'working out the acceloration caused by gravity
- sumofaceloration.x = sumofaceloration.x + (G * objects(n).mass * (objects(i).position.x - objects(n).position.x)) / ((objects(i).position.x - objects(n).position.x) ^ 2 + (objects(i).position.y - objects(n).position.y) ^ 2 + (objects(i).position.z - objects(n).position.z) ^ 2) ^ 1.5
- sumofaceloration.y = sumofaceloration.y + (G * objects(n).mass * (objects(i).position.y - objects(n).position.y)) / ((objects(i).position.x - objects(n).position.x) ^ 2 + (objects(i).position.y - objects(n).position.y) ^ 2 + (objects(i).position.z - objects(n).position.z) ^ 2) ^ 1.5
- sumofaceloration.z = sumofaceloration.z + (G * objects(n).mass * (objects(i).position.z - objects(n).position.z)) / (((objects(i).position.x - objects(n).position.x) ^ 2 + (objects(i).position.y - objects(n).position.y) ^ 2 + (objects(i).position.z - objects(n).position.z) ^ 2) * ((objects(i).position.x - objects(n).position.x) ^ 2 + (objects(i).position.x + objects(n).position.x) ^ 2) ^ 0.5)
- End If
- Next
- ' calcuating the new velocity
- objects(i).velocity.x = objects(i).velocity.x + sumofaceloration.x / bitrate
- objects(i).velocity.y = objects(i).velocity.y + sumofaceloration.y / bitrate
- objects(i).velocity.z = objects(i).velocity.z + sumofaceloration.z / bitrate
- Next
- For i = 0 To NumberOfObjects - 1 ' calculating the new position
- objects(i).position.x = objects(i).position.x + objects(i).velocity.x / bitrate
- objects(i).position.y = objects(i).position.y + objects(i).velocity.y / bitrate
- objects(i).position.z = objects(i).position.z + objects(i).velocity.z / bitrate
- Next
- Next
- 'printing the end result
- Console.WriteLine("Results:")
- For i = 0 To NumberOfObjects - 1
- Console.WriteLine(i + 1 & ":")
- Console.WriteLine("x position: " & objects(i).position.x)
- Console.WriteLine("y position: " & objects(i).position.y)
- Console.WriteLine("z position: " & objects(i).position.z)
- Console.WriteLine("x velocity: " & objects(i).velocity.x)
- Console.WriteLine("y velocity: " & objects(i).velocity.y)
- Console.WriteLine("z velocity: " & objects(i).velocity.z)
- Next
- GoTo constinues
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment