     Home Search   What's New Index Books Links Q & A Newsletter Banners   Feedback Tip Jar      MSDN Visual Basic Community  Title Make a game where you shoot a cannon past black holes This example shows how to make a game where you shoot a cannon past black holes in Visual Basic 6. game, cannon, black holes, animation Puzzles and Games, Algorithms The BlackHole class represents a black hole. The main program stores an array of BlackHoles in a collection. When the cannon is fired, a cannon ball shoots out at a specified angle and velocity. At each clock tick, the program loops through the BlackHoles and calls each object's UpdateVelocity method. That method calculates the acceleration due to the BlackHole and applies it to the cannon ball's velocity vector. (F = m1 * m2/dist^2. Then acceleration = F/m1 = m2/dist^2.)

```' Apply our gravity to this bullet's velocity.
Public Sub UpdateVelocity(ByVal bullet_x As Single, ByVal _
bullet_y As Single, ByRef vx As Single, ByRef vy As _
Single)
Const FORCE_SCALE As Integer = 10 ' Keep it interesting.
Dim dx As Single
Dim dy  As Single
Dim dist As Single
Dim dist2 As Single
Dim f As Single

' Calculate the force.
dx = X - bullet_x
dy = Y - bullet_y
dist2 = dx * dx + dy * dy
f = FORCE_SCALE * Mass / dist2

' Calculate the unit vector from the bullet towards the
' black hole.
dist = Sqr(dist2)
dx = dx / dist
dy = dy / dist

' Apply the force components.
vx = vx + dx * f
vy = vy + dy * f
End Sub```

See the code for additional details such as how the BlackHoles draw themselves.  Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.  Updated 