1 Simulation Essentials Overview World System of Coordinates Device System of Coordinates Transformation Equations Projectile Motion Equations Randomization,

Slides:



Advertisements
Similar presentations
Range, Height and Time.
Advertisements

Projectiles Two Dimensional Motion Courtesy darren/web/range/range.html Courtesy Physics Lessons.com.
7-2 Projectile Motion. Independence of Motion in 2-D Projectile is an object that has been given an intial thrust (ignore air resistance)  Football,
Modular Programming With Functions
IS660Z Programming Games Using Visual Basic Overview of Cannonball.
Visual Basic: ballistics
1© Manhattan Press (H.K.) Ltd. Monkey and hunter experiment Body projected horizontally under gravity Body projected horizontally under gravity Body projected.
Ideal Projectile Motion
Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
 Monday, 10/28/02, Slide #1 CS106 Introduction to CS1 Monday, 10/28/02  QUESTIONS on HW 03??  Today: Generating random numbers  Reading & exercises:
Projectile Motion: Physics of Baseball September 22, 2010
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
1 Lab Session-9 CSIT-121 Fall 2003 w Random Number Generation w Designing a Game.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
1 Getting Started: C-Revisions and Introduction to Graphics Next: Simulation Essentials, Memory Handling.
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
SACE Stage 2 Physics Motion in 2 Dimensions.
Chapter 1 Computing Tools Analytic and Algorithmic Solutions Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Functions Why we use functions C library functions Creating our own functions.
LECTURE LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,
Projectile Motion Horizontally Launched Projectiles Projectiles Launched at an Angle A.S – Due Friday, 11/14 Text Reference: chapter 3.
Projectile Motion Projectiles launched at an angle.
Projectiles calculations Calculating the components of a vector using trigonometry and vertical and horizontal problems.
Week 7 - Wednesday.  What did we talk about last time?  scanf()  Memory allocation  malloc()  free()
C++ Programming: Basic Elements of C++.
CMSC 1041 Functions II Functions that return a value.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
CSIS 113A Lecture 5 Random Numbers, while, do-while.
Copyright Sautter Motion in Two Dimension - Projectiles Projectile motion involves object that move up or down and right or left simultaneously.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
Associative Functions implemented on ClearSpeed CSX600 Mike Yuan.
C++ Programming Lecture 10 Functions – Part II
Projectile Motion Let’s Go Skydiving! Speed is the distance traveled per unit time. Velocity is an object's speed and direction of motion. Acceleration.
Vectors & Projectile Motion Chapter 3. Horizontal & Vertical Motion.
CS221 Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers.
Regents Physics Projectile Motion.
Kinematics: Projectile Motion What is Projectile Motion? Characteristics of a Projectile’s Trajectory Horizontal and Vertical Velocity Horizontal and Vertical.
Projectile Motion Projectile motion: a combination of horizontal motion with constant horizontal velocity and vertical motion with a constant downward.
Objects launched are projectiles Objects launched are projectiles balls, bullets, arrows, space ships… balls, bullets, arrows, space ships… The PATH a.
Projectile Motion. A projectile can be anything that moves though the air after it has been given an initial “thrust” or input force Projectiles are only.
Physics Lab Agenda – Thursday 12/16/04 – Pick-up materials on front desks – Intro to Horizontal Motion Lab – Bring physics HW to do in class.
Computer Graphics Basic Maths for Graphics in C++ CO2409 Computer Graphics Week 4.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
Chapter 3: Curvilinear Motion
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
Two Dimensional Motion Two components: Horizontal (x-axis) & Vertical (y-axis)
CSE202: Lecture 13The Ohio State University1 Function Scope.
Advanced loop controls. Loop Controls Recall from last week loop controls Event-controlled loops using sentinels repeats until a control variable takes.
Projectile Motion. Horizontally Launched Projectiles Projectiles which have NO upward trajectory and NO initial VERTICAL velocity. Y-Direction Free Fall.
1 Generating Random Numbers Textbook ch.6, pg
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
Motion in Two and Three Dimensions Chapter 4. Position and Displacement A position vector locates a particle in space o Extends from a reference point.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Computer Graphics Basic Maths for Graphics in C++
Functions, Part 2 of 2 Topics Functions That Return a Value
Projectile Motion Modelling assumptions
Projectile Motion.
Projectile motion Projectile Motion Subject to Gravity Assumptions:
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Angled Projectiles.
Projectile Motion Examples Quiz Demo: Bring both projectile launchers
What happens when a hot wheel is pushed across the table and then falls over the edge? OR What happens when a base-jumper runs across a surface and then.
Assignment Operators Topics Increment and Decrement Operators
Functions that return a value
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

1 Simulation Essentials Overview World System of Coordinates Device System of Coordinates Transformation Equations Projectile Motion Equations Randomization, Timer Keyboard Handling

Demo C:\Core\Massey Papers\159234\Animation-2008-OOP-v.5.0 2

3 PHYSICS PROJECTILE MOTION ter … Time, Initial Velocity, Mass, pull of gravity Air resistance, property of material, etc.

4 Transformation Equations WORLD-to-DEVICE COORDINATES x 1024 pixels 100,000,000 miles x 500,000 miles World System of Coordinates Device System of Coordinates +x +y +x +y 0 0 (X world,Y world ) (X Device,Y Device )

5 World Boundaries SETTING THE BOUNDARIES Use the upper-left and bottom-right coordinates to set the boundaries +x +y +x +y 0 0 (X world,Y world ) (X Device,Y Device ) Top-left: x1, y1 Bottom-right: x2, y2

6 Transformation Given a world coordinate, what’s the equivalent device coordinate? +x +y +x +y 0 0 (X world,Y world ) (X Device,Y Device )

7 Projectile Motion Setting the World Boundaries x +y 0 (X world,Y world ) (x1, y1) (x2, y2) x1=0 y1 = maximum_height x2 = maximum_range y2 = 0 ground

8 World Boundaries SETTING THE BOUNDARIES where  =85 degrees where  =45 degrees Time of flight : from take-off to landing

9 World-to-Device Coordinates TRANSFORMATION EQUATIONS Computed using the Physics equation for x

10 Projectile Motion PHYSICS EQUATIONS where g = 9.8 m/sec. 2 pull of gravity Vo in m/sec. initial velocity t in sec. Time  in radians Launching Angle Increment t in the equations and x and y will be automatically adjusted. Use the metric system

11 Projectile Motion Unit Conversion for Theta (degrees-to-radians)  = Theta_in_degrees * M_PI/180 Defined in math.h e.g. cos(  ), sin(  )

12 Projectile Motion PUTTING THE PIECES TOGETHER // InitGraphics here // InitBoundaries here t=0.0; while(t < tf) { cleardevice(); setcolor(RED); circle (Xdev( x(t, Vo, Theta) ), Ydev( y(t, Vo, Theta)), 12); t=t+tinc; } Physics Equation for x World-to-Device Transformation Function circle(x, y, radius)

Coordinate Transformations With the transformation equations, we can now always think in terms of world coordinates Let the Physics equations work it out where the bullet will be at any time t. Before displaying on screen, convert the world coordinate into its equivalent device coordinate. 13

Exercise Why do we need the transformation equations? Implement xDev and yDev using functions. What are the appropriate data types for the world and device boundaries? Incorporate error checking in your function implementations See transform.h, transform.cpp from C:\Core\Massey Papers\159234\Bomb-v

Demo C:\Core\Massey Papers\159234\SimpleProjectile 15

16 System of Coordinates EFFECTS OF CHANGING THE BOUNDARIES What happens if we switch the values for DeviceBound x2 and DeviceBound x1? What happens if we double all the maximum values for the WorldBound Coordinates?

17 Element of Surprise rand() generates a pseudorandom number - returns int int RandomVal(int min, int max) { return (min + (rand() % ((max-min)+1) )); } srand(time(NULL));

18 Element of Surprise srand() Seed for random-number generation Seed the random-number generator with current time so that the numbers will be different every time we run. srand( (unsigned)time( NULL ) ); /* Display 10 numbers. */ for( i = 0; i < 10;i++ ) printf( " %6d\n", rand() );

19 Element of Surprise rand() generates a pseudorandom number - returns int int RandomVal(int min, int max) { return (min + (rand() % ((max-min)+1) )); } rand() rand() returns a pseudo-random integral number in the range (0 to RAND_MAX)-1

20 Element of Surprise rand() float RandomVal(float min, float max) { float r; r = (float)rand()/RAND_MAX; r = min + (r*(max-min)); return r; } rand() rand() returns a pseudo-random integral number in the range (0 to RAND_MAX)-1

21 Time elapsed, wait… clock() void wait ( int seconds ) { clock_t clock_t endwait; endwait = clock () + seconds * CLOCKS_PER_SEC ; while (clock() < endwait) {} } clock_t clock_t startTime, elapsedTime; startTime = clock(); …... elapsedTime = (clock() - startTime)/CLOCKS_PER_SEC;

22 Keyboard Handling GetAsyncKeyState The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState. To find other pre-defined constants: google Using google, type the following keywords: msdn vk_shift Virtual-key code e.g. vk_shift vk_control SHORT GetAsyncKeyState( int vKey ); // vKey - virtual-key code

23 Keyboard Handling GetAsyncKeyState void MoveSprite() { if(GetAsyncKeyState(VK_UP) < 0) { SpriteY = SpriteY - 2; //up outtext("UP"); } if(GetAsyncKeyState(VK_DOWN) < 0) { SpriteY = SpriteY + 2; //down outtext("DOWN"); …. To find other pre-defined constants: Using google, type the following keywords: msdn virtual key codes

24 Keyboard Handling Monitoring the Control and Shift keys: if(GetAsyncKeyState(VK_CONTROL)<0) { ControlFlag =! ControlFlag; } bool ControlFlag, ShiftFlag; if(GetAsyncKeyState(VK_SHIFT)<0) { ShiftFlag =! ShiftFlag; } For the Tank to Jump to the Right: Control + Shift + Right Arrow key For the Tank to Jump to the Left: Control + Shift + Left Arrow key

25 Keyboard Handling Possible approach in monitoring key combinations : if(GetAsyncKeyState(VK_RIGHT)<0) { XDir=RIGHT; if(ShiftFlag) { outtext("SHIFT + RIGHT"); ShiftFlag=!ShiftFlag; } if(ControlFlag) { outtext("CTRL + RIGHT"); if (TankX < getmaxx()-W) TankX += 2; Angle=Angle-5; RaiseWheelFlag=TRUE; ControlFlag=!ControlFlag; } …