Presentation on theme: "Visual Basic: ballistics"— Presentation transcript:
1 Visual Basic: ballistics Review on timers, animationResolve vectorsHomework: Catch up on projects. Read chapter 6. Try cannonball.Recall bouncing ball demonstrations. Complete (memory), hangman.
2 Coordinate systemMost 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 rightthe Top/Y values increase moving DOWN the screenThe screen is two dimensional, that is, it takes two numbers to specify a position.
3 Timer control Properties: Interval and Enabled. If Enabled is True, the timer event happens if Interval is > 0Interval is set in millisecondsIf 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.
4 Sub timFalling_TimershpBall.Top = shpBall.Top + shpBall.HeightThis causes the ball to drop its whole height every ‘interval’.shpBall.Top = shpBall.Top + delta * v_speedAssume 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_speedThis 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.
5 Bouncing ballRecall 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.
6 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 componentsVelocity is defined as speed and directionThis is simplification. There is such a thing as air resistance.
7 Horizontal scroll bar for setting the velocity leaving the cannon. Command button captioned FIRE! fires the cannon.
8 Calculating the vectors Anglevx = 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 thehorizontal and vertical vectors as adding up to the original vector.
9 Equations of motion Constant velocity Distance traveled = velocity * timeNew_position = velocity*time + old_positionAcceleration (let g=acceleration)New_velocity = g * time + old_velocityAverage velocity = .5 * g*time+old_velocityNew_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.
10 Horizontal and vertical velocities (switching to VB names for variables):X2,Y2 is endpoint of cannonInitial horizontal velocity remains the same:sngXX = sngVx * sngTT + X2Vertical velocity changes:sngYY = .5 * g *(sngTT * sngTT) - sngVy * sngTT + Y2This 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.
11 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 cannonTimer 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.
12 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!