200 likes | 338 Views
The Art of Computer Science Part II. Bill Klinger – RVCC Computer Science Dept. The Art of Computer Science. Architecture structure of the processes interaction of the processes Algorithms clever efficient Making the electrons dance We will examine the “art” of a computer game.
E N D
The Art of Computer SciencePart II Bill Klinger – RVCC Computer Science Dept.
The Art of Computer Science • Architecture • structure of the processes • interaction of the processes • Algorithms • clever • efficient • Making the electrons dance • We will examine the “art” of a computer game
The Art of Computer SciencePart II • Art in Algorithms • real-time problems and solutions • Examples • collaborative Pong • collaborative Driving
Homework • How do we keep users in synch when the timing intervals are much smaller than potential variances? • In a driving game, we move ahead by advancing frames based upon our speed. Given: n = frame drawings per second m = max speed of car (in mph) s = current speed of car (in mph) What algorithm should we use to draw the frames?
Problems in Real-time Games • Need to keep the action going • But • there is network delay • PCs run at different speeds • may be other activity on a PC • PCs will get out of synch
Dead Reckoning • Definition • A method of estimating the position of an craft without astronomical observations, as by applying to a previously determined position the course and distance traveled since. • Predictive calculation based on inference; guesswork
Dead Reckoning in Pong • Predict future position based upon last position and action • know ball x, y, dx, dy • know paddle y, dy • bounce off walls and paddles • Adjust, if necessary, when get new info • Problem – how to do this when listening to server and user? • another thread
Let’s Try It • Keep in mind • Threads that get your input • Threads that listen to the server • Threads that move • Sockets • Pong Exercise • login with guest name and password • go to Academic Departments • go to Computer Science • click on William Klinger faculty link • click on Participant Material on lower left side of page • click on Pattern Exercise
Art in Algorithms - 3 • Problem: how to advance the frames to show the speed of a car?
Art in Algorithms - Speed • We update the graphics n times per second • But we don’t advance the frames on each update • if at half speed, update every other frame • if at quarter speed, update one cycle, skip three cycles • if at three-fourths speed, update three cycles, skip one
Speed % of #CYCLES #CYCLES MAXSPEEDADVANCINGNOT MOVING 0 0 all 20% 1 4 25% 1 3 33% 1 2 50% 1 1 66% 2 1 75% 3 1 80% 4 1
How Long to Wait? • Let: c = cycles per second (game loops) s = speed m = max speed w = number of cycles not moving • Then w = total cycles waiting / total cycles moving w = [ c * (1 – (s / m ) ) ] / [ c * (s / m) ] w = (1 – (s / m ) ) / (s / m) w = ( m / s ) - 1 c’s cancel multiply num and denom by m / s
The Speed Formula • If speed < .5 max speed max speed # wait cycles = speed - 1 • If speed >= .5 max speed 1 # adv cycles = # wait cycles Note this is independent of the # cycles per second!
Speed Algorithm countCycles++; if( speed < (MAXSPEED / 2 )) { // the algorithm is to wait waitcycles times waitCycles = (MAXSPEED / speed) - 1 ; if( countCycles > waitCycles ) { // waited enough cycles now advance the car countCycles = 0; countMoves = 1; trackLoc++; } // end if - no else - don't advance the car } // end moving if .5 maxspeed
More Speed // the speed is .5 maxspeed domoves = 1.0 / ((MAXSPEED / speed) - 1 ); if( countmoves <= domoves) { // advance the car countmoves++; countcycles = 0; trackLoc++; } // end if advancing else { // pause a frame and reset countcycles = 0; countmoves = 0; }
I Feel the Need for Speed • Think about • threads listening to your input • threads listening to the server • threads moving the car • server threads sending moves • Driver Exercise • go back to Participant Material • click on Driver Exercise
The Art of Computer Science • You can admire the graphics • You can read the code • Some art lives where you can’t see it