Presentation on theme: "Visual Basic: ballistics Review on timers, animation Resolve vectors Homework: Catch up on projects. Read chapter 6. Try cannonball."— Presentation transcript:
Visual Basic: ballistics Review on timers, animation Resolve vectors Homework: Catch up on projects. Read chapter 6. Try cannonball.
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
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.
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.
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.
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
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)
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
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
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.
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.