' A program to demonstrate simple physics
' This program uses a technique verlet integration: http://en.wikipedia.org/wiki/Verlet-Stoermer_integration
' A system is built from particles (rendered as red round rings) and constraints (rendered as green or yellow lines)
' A stick constraint can be used to build a stable square. A square has four particles (one in each corner) and we connect
' these corners with 5 stick constraints like this:
' P1 - C1 - P2
' | \ |
' | \ |
' | \ |
' C4 C5 C2
' | \ |
' | \ |
' | \ |
' P4 -C3 - P3
' The constaints connects two particles and make sure that particles neither get closer or further away from eachother
' We need the C5 constrain to prevent the box from collapsing
' Using these simple building blocks one can build suprisingly complex system that are behaves in way that feels natural
' Subroutines to help drawing system
Sub draw_legend
If legend_name = "" Then
GraphicsWindow.BrushColor="Gray"
legend_name = Shapes.AddText("Left Mouse Button - Invert Gravity, Right Mouse Button - Quit")
EndIf
EndSub
Sub draw_box
If box_name = "" Then
GraphicsWindow.PenColor = "White"
GraphicsWindow.BrushColor = "Transparent"
box_name = Shapes.AddRectangle(box_max_x - box_min_x, box_max_y - box_min_y)
EndIf
Shapes.Move(box_name, box_min_x + center_x, box_min_y + center_y)
EndSub
Sub draw_particles
GraphicsWindow.PenColor = "Red"
GraphicsWindow.BrushColor = "Gray"
For i = 1 To particle_count
x = particle_x[i]
y = particle_y[i]
name = particle_name[i]
If name = "" Then
name = Shapes.AddEllipse(particle_visual_size, particle_visual_size)
particle_name[i] = name
EndIf
Shapes.Move(name, x + center_x - particle_visual_size / 2, y + center_y - particle_visual_size / 2)
EndFor
EndSub
Sub draw_constraints
For i = 1 To constraint_count
left = constraint_left[i]
right = constraint_right[i]
If left > 0 And left <= particle_count And right > 0 And right <= particle_count Then
left_x = particle_x[left]
left_y = particle_y[left]
If constraint_is_stick[i] <> 0 Then
GraphicsWindow.PenColor = "Yellow"
Else
GraphicsWindow.PenColor = "Green"
EndIf
' This way of drawing constraints seem overly complicated it turns out using
' Move/Zoom/Rotate is more effecient than clear the GraphicsWindow and drawing the lines anew
clength = constraint_length[i]
name = constraint_name[i]
If name = "" Then
name = Shapes.AddLine(0, 0, clength, 0)
constraint_name[i] = name
EndIf
x = left_x + center_x
y = left_y + center_y
w = right_x + center_x
u = right_y + center_y
' This makes sure the particles never leave the box
Sub make_sure_particles_is_inside_box
For i = 1 To particle_count
particle_x[i] = Math.Min(Math.Max (particle_x[i], box_min_x), box_max_x)
particle_y[i] = Math.Min(Math.Max (particle_y[i], box_min_y), box_max_y)
EndFor
EndSub
' This makes sure that if stick constraint or rope constraint is tense we reduce tension by moving the particles closer or further away
' from eachother
Sub reduce_the_tension_in_the_system
For i = 1 To constraint_count
left = constraint_left[i]
right = constraint_right[i]
If left > 0 And left <= particle_count And right > 0 And right <= particle_count Then