Visual Basic: ballistics

Presentation on theme: "Visual Basic: ballistics"— Presentation transcript:

Visual Basic: ballistics
Review on timers, animation Resolve vectors Homework: Catch up on projects. Read chapter 6. Try cannonball. Recall bouncing ball demonstrations. Complete (memory), hangman.

Coordinate system Most Visual Basic controls are positioned in terms of Top and Left. Lines are positioned in terms of x1, y1, x2, y2 values. As in most computer systems, the Left/X values increase moving to the right the Top/Y values increase moving DOWN the screen The screen is two dimensional, that is, it takes two numbers to specify a position.

Timer control Properties: Interval and Enabled.
If Enabled is True, the timer event happens if Interval is > 0 Interval is set in milliseconds If a timer named timFalling has Enabled set to True and Interval set to 500 then timFalling_Timer will happen every ½ second. Actually half a second is fairly slow for animation. You can use the values suggested in the chapter, but you should also experiment.

Sub timFalling_Timer shpBall.Top = shpBall.Top + shpBall.Height This causes the ball to drop its whole height every ‘interval’. shpBall.Top = shpBall.Top + delta * v_speed Assume delta represents the elapsed time and v_speed represents a speed, this causes the ball to drop an amount equal to time * speed. shpBall.Top = originalTop + T * v_speed This positions the ball each time based on a calculation from originalTop (presumably the starting position), T is the elapsed time. The text makes suggestions for cannonball, but in general, you need to decide if it is best to calculate changes or positions from starting positions.

Bouncing ball Recall that in the timer event, the object was moved horizontally (Left) and vertically (Top) Also, check was made if object struck sides of the form. Speed in each dimension was constant. Timer event had the code checking for going past the sides.

Ballistics Simulation of motion must 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 Velocity is defined as speed and direction This is simplification. There is such a thing as air resistance.

Horizontal scroll bar for setting the velocity leaving the cannon.
Command button captioned FIRE! fires the cannon.

Calculating the vectors
Angle vx = v * Cos (Theta) vy = v * Sin (Theta) Where v is velocity coming out of cannon, theta is the angle. Angle (traditional name is theta) This is called ‘resolving the vectors’. The trig functions return the horizontal and vertical component of the vector. Think of the horizontal and vertical vectors as adding up to the original vector.

Equations of motion Constant velocity
Distance traveled = velocity * time New_position = velocity*time + old_position Acceleration (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 (Independent of Visual Basic), these are the so-called equations of motion. Distance = speed times time. Change in velocity = acceleration times time. So new velocity is the formula shown. New position is average velocity times time. Average velocity (assuming constant acceleration) is one half the final velocity.

Horizontal and vertical velocities
(switching to VB names for variables): X2,Y2 is endpoint of cannon Initial horizontal velocity remains the same: sngXX = sngVx * sngTT + X2 Vertical velocity changes: sngYY = .5 * g *(sngTT * sngTT) - sngVy * sngTT + Y2 This is a restatement of the equations of motion as they apply to the horizontal (constant velocity) and the vertical (constant acceleration) in VB using variable names used in the text. Y2 is the vertical component of the tip of the cannon. The last line of code also takes into account the fact that the vertical goes up moving down the screen.

Overview of cannonball
Note: read chapter 6! Command button with caption FIRE! will calculate initial horizontal and vertical velocity from the scroll bar for the speed and the angle of the line representing the cannon Timer event will increment variable for time and apply the equations of motion to the cannonball (a shape control object) Timer event also does calculation to determine if cannonball has hit the ground or hit the target. Mouse events used to drag cannon tip and target. Horizontal scroll bar used for speed. Finish old projects first. I want to see them this lab session or next week.

Staged implementation
Cannonball moves through the air. No checks to stop it! Stop execution by clicking on stop button on toolbar. Check for hitting ground or hitting target. Implement event handlers for changing speed, moving tip of cannon. Staged implementation is highly recommended for all but the smallest of projects. This is how we divided the implementation: how I really worked on it, not just saying it for you to do. The professional way is to divide the work. It goes faster!