Documente Academic
Documente Profesional
Documente Cultură
Overview of Cannonball
Cannonball
Computer graphics coordinate
system
Using a timer
Ballistics
Drag and drop
Coordinates and
Positioning
Visual Basic
controls are
positioned
through Top and
Left properties
Width and Height
properties
determine the
size
Coordinates for lines
Lines are
positioned in
terms of x1, y1,
x2, y2 values
X1,Y1 is one point
X2,Y2 is the
second point
But notice Y2 is
less than Y1?
Coordinate system
Computer systems are
backwards on Y axis
The Left/X values
increase moving to
the right
The Top/Y values
increase moving
DOWN the screen
Therefore when you
use code to move
things you need to
know this!
Timer control
Has very few
properties
Interval must be non-
zero for timer to work
Enabled = true turns
on the timer, enabled
= false turns it of
Can use multiple
timers
Managing timers
Timer appears in
design view, but not
when the game is
running
Interval is in
milliseconds -- If a
timer named timFalling
has Enabled set to True
and Interval set to 500
then timFalling_Timer
will happen every
second
Ballistics
Try out Cannonball if
you havent already
Notice the ball takes
an arc like path
This is natural
motion of object
subject to gravity
The calculation of the
motion of an object
subject to gravity is
call ballistics
Simulation has two parts
Ball is moving both horizontally
(across) and vertically (up and
down)
Our simulation uses two formulas to
the ball in both directions and take
gravity into account
Horizontal motion continues with no
acceleration (no change)
Vertical motion is changed by gravity
Physics & computer graphics
requires: when cannonball is shot
out of cannon at an angle, your
code must resolve the initial
velocity vector into horizontal and
vertical components (see p. 86)
Velocity is defined as speed and
direction
Timer event and ballistics
Timer event carries out the
animation
At each occurrence of the timer
event, a new position for the ball is
calculated and the cannonballs
positional properties are updated
These calculations apply formulas
from ballistics
Animating the cannonball
Step 1: resolving the vectors, i.e.
determining initial horizontal and
vertical speed (based on angle of
cannon and speed from scrollbar)
Step 2: repositioning the
cannonball over time
What Events?
Step 1: FIRE! Click
event
Step 2: Timer
event
Resolving the vectors
Angle
Angle (traditional name is theta)
vx = v * Cos (Theta)
vy = v * Sin (Theta)
Acceleration (vertical)
let g=acceleration
New_velocity = g * time + old_velocity
Average velocity = .5 * g*time+old_velocity
New_position = .
5*g*time*time+old_velocity*time+old_position
Horizontal and vertical
velocities (VB code)
X2,Y2 is endpoint of cannon, TT is
elapsed time
xx, yy is new (recalculated) position
Horizontal velocity (vx) is constant:
xx = vx * TT + X2