Recurrence Relation for Projectile Simulation Project and Game based Learning

Huge Gap has been observed on study of projectile simulation models relating it to speed of camera or frame per seconds. The objective of this paper is to explore and investigate time driven simulation models to mimic projectile trajectory; with an intent to highlight importance of game programming on native platforms. The proposed projectile recurrence relation and extensive mathematical modeling based on Triangular Series is an innovative outcome of project and game based learning used in BSCS-514 Computer Graphics Course at Department of Computer Science (DCS) University of Karachi (UOK). Box2D Replica of Popular 2D Mobile Game Angry Bird has been created on desktop to have an in depth mathematical and programming insight of commercial physics engine and discrete event simulation. Analysis has also been performed to answer certain key questions for progressive projectile trajectory for e.g. (1) With What angle, projectile should be launched? (2) What is the maximum height it will reach? (3) How long it will take for landing? (4) What will be its velocity to reach a desired height? (4) Where it will hit? (5) How it will bounce? The above stated questions are important to answer so that projectile motion within engineering, Gaming and other CAD Applications can be taught and programmed correctly specially on native platforms like OpenGL. Besides reporting Numerical results, a successful projectile based game making has been compiled and reported to validate the significance of project based learning in classrooms and labs. Keywords—Projectile; game programming; simulation; angry birds; linear drag; trajectory; impulse


I. INTRODUCTION
Understanding projectiles in an interdisciplinary manner is exceedingly important at variety of levels to incorporate them in real life applications.Engineering applications involving projectile use deep and precise analytical equations which are difficult to grasp and requires an in depth domain knowledge [1][2] [3].Classical literature documented Physical class room experiments to study and build understanding about projectile models [4] [5].CAD and gaming applications requires the same background knowledge along with programming skill for simulation and visualization of projectile trajectory.In this paper, an attempt to unlock the projectile behavior of available game development platforms from simulation perspective on computers.Platforms are available to facilitate engineers and programmers for building these physical models without handling the pressure of complex analytical mathematics and statistics.As a senior computer science faculty, I strongly believe that understanding these mathematical foundation has played an immense important role in polishing ones" cognitive, engineering and programming skills [6] - [12].There exist three common models to simulate projectile in computer applications for gaming and simulation; they are: (1) No drag model (2) Linear drag model and (3) Quadratic drag model.In no drag model, the motion of projectile is mainly dependent on initial velocity and the angle of launch.On the other hand, both linear and quadratic drag model taken into account, the air resistance effect on projectile trajectory.Demonstration of projectile has been done through replica of famous angry bird game using Box2D [13] [14] and figure out that which model has been used in this physics engine?Rest of the paper is organized as follows: Section II and III discuss standard No drag and linear drag trajectory models respectively.Section IV till Section VII comprise of extensive derivations based on recurrence relation, triangular numbers and Frames Per Second Criteria (FPS).The derivations are inspired by Box2D and OpenGL experiments discussed in [13] - [17].Experiments on spread sheet and through game programming has been discussed in Section VIII which follows Results and Discussion described in Section IX.Conclusion and Future Work has been presented in the end of this paper.

II. NO DRAG TRAJECTORY MODEL
In general, the motion produced by a body which follows projectile trajectory reached a certain elevation and then allow to descending as a mirror of elevation.The vector of the motion can be divided into two components, "x": the horizontal component and "y": the vertical component of the motion.The force at the horizontal vector remains unchanged throughout the motion while the vertical forces applied at the birds frequently changes due to the effect of gravity and height of the bird.For computing the velocity of the birds during the flight time, following equation can be used.
The acceleration of the object, in both the components of motion remains constant throughout the flight time.3) will compute the height of the flight with respect to the ground.Range of the projectile launched is also important to calculate because this information will enable us to determine the landing position of the object.The mathematical model for computing total range of the bird is mentioned below Range of the bird depends on two independent variables in the projectile motion of a game, first is the adjusted angle and second is the adjusted velocity of the bird.The angles of the launch greatly affect the range of the bird due to the trigonometric function of Sine on the angle.It is computed that at any velocity the bird will achieve the maximum range value if the angle adjusted is 45 0 because of ( ) in Eq (4).When the value of is equal to 45 then becomes equal to 90 degrees and thus maximum range will be From simulation perspective, total time of flight is calculated by manipulation of Eq. ( 2) and Eq. ( 3) and finally putting h =0 to attain maximum range at ground.It is not difficult to have following equation for computation of total time of flight "T" in advance as follows Once total time of flight is calculated, computation of points (X, Y) on trajectory will become possible by dividing time T into small equal space intervals t +delT as follows Eq (7) and Eq (8) are used to determine instantaneous position of projectile given initial Velocity as "V" and initial angle "theta".It is important to note that Y(t) depends on X(t).

III. LINEAR DRAG TRAJECTORY MODEL
Linear impulse is defined as a linear force applied on anybody i.e.
. In many games, an external backward force is applied on body before launching it as projectile for e.g.drag of the bird in game "Angry bird" is applied through a slingshot controlled by user.This drag force will of course be decomposed into its components along x and y-axis.The area for dragging the object is specified with reference to the initial position of the projectile.The drag backward is restricted on x-axis; the same will be true for y-axis.Drag displacement will be directly proportional to drag force. .If represents the change in distance due to drag then d F   Eq (9) can be decomposed into two independent equations with respect to x and as follows: The objective is to find final velocity with the help of initial velocity so that we can determine the launch speed with which it moves along projectile trajectory.Assume fixed time incremental approach with = 1.Assume that the Forces will remain constant throughout the projectile motion.The body will then move with uniform acceleration on x-axis.The y-component of launch velocity will be affected by the acceleration due to gravity g.The expression for final velocity along x-axis can be formulated as follows Along y-axis; following simplified drag model has proposed to determine final velocity progressively gt ma F y y   (12) Aa per our assumption, the change in momentum of projectile can be compared with change in vertical forces as follows

IV. RECURRENCE RELATION FOR HEIGHT
Our recurrence relation is inspired by Box2D description on projectile [13 [14].To derive a general formula, that can be used to find the final velocity ( ) at any given point in time; each velocity assume to contain the sum of all the previous velocities.In a general way the velocities are in the arithmetic progression manner.If we use Eq (14) at each frame display in www.ijacsa.thesai.orggames, then the general expression for finding the height at any instant can be figure out by working from Eq (14) till Eq (17 If we use Eq (15) to find instant launch speed and we are given the initial height of the projectile then following equation can be used to find the height at any instance of time: This above equation can be further expanded into Eq (17) as we substitute final velocity from Eq (15).Here represents the initial height of the projectile at which it was launched Where i is the counter to track time instants while projectile is flying above the ground.

V. TIME OF FLIGHT AS TRIANGULAR SERIES
The total time for the flight of the projectile can be obtained by substituting final velocity as zero because the projectile will come to rest as it hits the ground, therefore where is used to represent instantaneous time of flight The sign of summation can be interchanged by 2 ) 1 (  n n because this series is the nth partial sum of the series of triangular numbers. 2

VI. RELATING VELOCITY AND HEIGHT
The derivation to find the velocity at the maximum height will follows from Eq (22) till Eq (25).Substituting time of flight from Eq (20) into Eq (15) of final velocity, we will get Eq ( 22) Eq (20) provides the total time of flight but maximim height will be achieved at half time of the total therefore Eq (21) will turn into Eq (23) as follows Substituting above fact into Eq (22), We have Eq (24) and Eq (25) respectively represent the relation which represent velocity of projectile with respect to displayed frame in game and illustrate frame animation.

VII. IMPULSE FORCE AND FINAL VELOCITY
The equation for the final velocity due to the impulse force will be considered as follows Where is the final velocity which is the consequence of the initial impulse, and is the initial velocity, is the acceleration provided to the body whereas represent the force of gravity applied on the motion of the body.This equation only tells us about the instantaneous velocity of a body at any given time.Indeed, apart from calculating the velocity, we are more interested in getting the current displacement at y-axis by the body with a given value of velocity.For e.g.What will be the distance covered by the body when and ?In this scenario, it can be said that every displacement is dependent upon the previous displacement that is why the following equation is given to fulfill our requirement.
The total time taken by the body to complete its trajectory where the velocity of the body becomes equal to zero.The equation for total time taken for the flight is as follows: 2 The equation for calculating the maximum height achieved by the body during the flight is derived by substituting EQ (29) into EQ (27) as follows ) 2

A. Spread Sheet Simulaiton of Pojectile Trajectory
The purpose of spread sheet simulation is to visualize and test our proposed linear impulse based recursive projectile model.We already discussed Linear Drag Model in Section III.The force along x-axis is assumed to be constant according to our simulation assumption.The required initial parameters are set as follows for demonstration purpose With above initial conditions we use Eq (11) and Eq (13) to update velocity along each axis respectively at each frame or display of projectile.For simplicity, the change in time between frame displays is taken to be unit which means that time instances are taken as a fraction of 60.Table 1 and Table 2 Presents Numerical Results of progression in velocity along each axis.From Table 1, it has been observed, that the projectile is with uniform acceleration along x-axis as the difference between two adjacent velocities remained a constant.1 and Table 2 that the generated impulsive force has enough large magnitude over a very small interval of time (fraction of a second) that it causes a significant change in the momentum which in turn lift the projectile up in the air in response to initial drag during its flight.To build simulation start with ; compute as described in Eq (11) TABLE I.

It has been observed from Table
SIMULATION SAMPLE EQ (11)

C. Projectile Simulation on Box 2D and OpenGL
Game Programming is an important and impressive tool to practice simulation.Box2D is used to simulate core projectile functionality whereas OpenGL "freeglut" library on windows is used for rendering and to interact with user through mouse and keyboard.Box2D modeled projectile as linear impulse force [13] [14].The 2D game pipeline consist of following minimal basic steps to experiment with projectile.( 1) Modeling ( 2) Animation (3) Collision Detection.Modeling of game world means setting up background and foreground.Background is indeed a flat polygon with height of 480 and width of 1920 pixels.The wide polygon is split into smaller polygon to be used as textured sheet.Fig. 1 demonstrate simple UV mapping on a rectangle to model background comprising of clouds, grassy land, and sun.So textured polygon made it convenient for a programmer to have nice background using glossy images.Fore ground entities Comprises of main characters such as Birds, Pigs, walls (Boxes) and slingshot.Game Characters are designed with simple polygons and we employ uv mapping to animate characters.These characters are represented by Bitmap images as shown in Fig. 2. As a matter of fact, all foreground objects are modelled at origin; later they are translated to specific position by using coordinate system transformation as illustrated in the for the Bird.
The land type and background in the game need to change as projectile launces and trying to hit the target at distance ahead of its current position.This effect is achieved by setting predefined multiple polygons shown in Fig. 3 as Polygon A, B and C. When the game starts, user will able to see the initial background but when it uses sling shot to launch projectile, the coordinates get changed and user will experience change of background at the output screen.These multiple lands or polygons will be visible to the user at its screen and the view will be updated at runtime.Foreground modeling is another important part of the game as they will change with their state on different triggers and events.All Game character (birds), pigs (enemy), boxes, hurdles and other bitmaps are counted as foreground elements.The foreground is divided into two section of the screen.The left portion of the foreground is dedicated for modeling the slingshot and the birds which are in the control of the user.This portion will enable the user to adjust the angle and the speed for launching the birds at a trajectory of projectile motion through the slingshot in order to hit the walls and to eliminate the pig.The other portion of the foreground consists of the defensive walls for the pigs.All the Birds in game are modeled by drawing circles and then applying bird texture on them.While Boxes and slingshots are modeled by applying texture on to the rectangles.When the game is initialized, all the characters and the objects in the game are drawn at the initial position of the screen, at (0, 0).The positions of these various models in this game are translated with respect to their functionality and behavior, as the initial bird is set near the slingshot elevated by default ready to be shot by the user, on the other hand other birds wait for their turn in a row wise manner.The pigs are modeled in between the walls or the boxes on the other side of the screen.www.ijacsa.thesai.orgPhysics played a vital role in animation of all characters.Following aspects need to be considered in order to model the desired animation.(1) In which direction it should be launched to meet the desired result?(2) How high it will go?(3) How long it will take to land on the floor?(4) Where it will hit the ground?(5) How far away from the ground?(6) Collision Detection.Slingshot is used in this game in order to set the launching direction and to make accurate shot at the pigs.The slingshot uses the birds for attacking pigs confined in a defensive wall.The slingshot is animated with the help of mouse events and buttons.Slingshot is stretched with the left button of the mouse for adjusting the force or the speed and the angle at which the bird will be shot.The right button is used to launch the bird at the pigs with the angles and the force adjusted earlier.Birds being shot through the slingshot are translated at an angle with an initial velocity and become projectile under the influence of gravity.The animation of the bird is handled according to trajectory model already described in detail from Eq (1) till Eq (13).Observe the arrangement of Woods in Fig 4 .Falling of wood Boxes is established under the acceleration due to gravity and is achieved by using Newton"s second equation of motion.
But, the initial velocity Therefore, the rate at which body is falling is: Another Noticeable construct is once again the application of parametric equation to generate points on the curve as already described in Eq (7) and Eq (8).After modeling and animation, the last aspect of a basic 2D game is to detect collision between entities.Principal of Separating Axis Theorem (SAT) states that: "If two convex objects are not penetrating, there exists an axis for which the projection of the objects will not overlap."To detect whether two objects are being collided, projection of their axis are calculated.If the distance between all the projections is equals to or less than zero, then it is said that the object has collided with each other.If projection of bird overlaps with projection of box on both horizontal and vertical, animation event is triggered.This concept has been extended and applied on every polygon as shown in Fig. 5.    Successful Simulation of extensive mathematical models has been demonstrated using both Spread Sheet and C++ programming by integrating Box2D and OpenGL.Fig. 6 shows the result of applying impulse force as agent at initial time of the motion, after which the velocity produced by this impulse and force of gravitation contributes for further movement of the projectile.In our scenario the impulse is being applied at the center of the body.The y-component of the force mimics the angle of launching the projectile towards the target while x-component is the linear force or speed with which the bird is supposed to launch.FPS is 60 frames per www.ijacsa.thesai.orgsecond and is the rate at which box2D updates or refreshes its View Window.Fig. 7 shows that it is possible that flight time will take form of triangular series and the projectile trajectory is successfully followed if triangular increment is used for simulation.The simulation of linear impulse in box2D and OpenGL requires two arguments: (1) An initial vector V 0 containing and component of velocity (2) A Point from where impulse has to be triggered.Y-component of final velocity is denoted with ( ) i.e. velocity at time instance t in future; g is 9.8 m/s, "F" represents y-component of force applied on mass "m".FPS is defined as the default frame per second value of box2D which is 60.Reciprocal of "FPS" provides us with time to render single frame which in turn plot the position of body.To mimic original game, projectile is fired and controlled through mouse pointer.To aim pigs at a certain angle; birds are controlled by click buttons of the mouse.Left button is used for adjusting speed value of the bird whereas the right button controls the angle of the shot being made.All the variable setup is shown in Fig. 8 for reader convenience.By using Mouse interactions with the system we developed multiple techniques to deal with the angle, at which the bird would fly, and magnitude of force, with which the bird would be fired, from the slingshot.The movement of mouse on the horizontal axis (xFactor) would modify the magnitude of force by some fraction.Similarly, the vertical displacement (yFactor) would intervened the angel of flight by some value as shown in the Fig. 9.In the end we have presented a comparison of OpenGL and Box2D simulation loop in Table 6.Linear Drag Simulation on Spread Sheet doesn"t exactly match with functionality available in Box 2D and hence It has been concluded that mathematical modeling and simulation is an essential ingredient of learning which cannot be excluded from class environment due to easy access of commercial engines and open source libraries.Instead it is very important to exercise elementary pipeline of engines as a class project.This will enhance one"s programming skills and also inculcate bottom up approach for building innovative software"s either on the top of existing one or by developing it from scratch.Future work will comprise of dimension analysis of drag model presented in this paper.The work is also extended by demonstration of projectile using Vulkun API instead of glut or free glut with intention of handling physics and rendering in separate threads for effective GPU utilization.

Fig. 3 .
Fig. 3. Wide Polygon Design to Map Levels in Game.

Fig. 4 .
Fig. 4. Animation to Show Fall of Woods or Boxes.

Fig. 6 .
Fig. 6.Simulation Result of EQ (17) Recurrence Relation of Projectile Height as a Function of Projectile Final Velocity and Frame Per Second.

Fig. 7 .
Fig. 7. Simulation Result of EQ (22) Where Flight Time Takes the form of Triangular Series and Mimics Projectile.

Fig. 8 .
Fig. 8. Simulation Settings of Projectile Linear Drag Model using Box2D and GLUT API.
Spread Sheet Simulation of Time as Triangular SeriesIt has been observed that current displacement of projectile can be seen as series of instantaneous velocities sum together where each instant can take the form of triangular number in series.The simulation sample of model described in Section VII is illustrated in Table3below.Some initial parameters for demonstration purpose are given following values 3478 www.ijacsa.thesai.orgB.

TABLE IV .
FINDING AXIS BY VECTOR SUBTRACTION

TABLE V .
COLLISION DETECTION BY OVERLAPPING TECHNIQUE for all Shapes in S for all Axes of S in A Projection p1 = s.project(A); Projection p2 = s2.project(A); // s2 is the second shape.if (!p1.overlap(p2)) return false; end for Axes end for S IX.RESULT AND DISCUSSION

TABLE VI .
COMPARISON OF GLUT AND BOX2D SIMULATION LOOP Assign the fixture to the body Step 3 Rendering of world"s Bodies For all bodies n in the world { Draw n } X. CONCLUSION AND FUTURE WORK Replica of popular angry bird game has been successfully implemented during BSCS-Computer Graphics class project at Department of computer Science, University of Karachi.Integration of physics engine Box2D and OpenGL on Visual Studio and windows platform has been successfully achieved.