home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 04 Mommersteeg / Tennis / Ball.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-23  |  4.1 KB  |  125 lines

  1. //----------------------------------------------------------------------------------------------
  2. // Sequential Prediction Demo: The positioning pattern
  3. // 
  4. // Author:  Fri Mommersteeg
  5. // Date:    10-09-2001
  6. // File:    Ball.cpp
  7. //----------------------------------------------------------------------------------------------
  8.  
  9. //----------------------------------------------------------------------------------------------
  10. // Include files
  11. //----------------------------------------------------------------------------------------------
  12.  
  13. #include "stdafx.h"
  14. #include "ball.h"
  15. #include <math.h>
  16.  
  17. //----------------------------------------------------------------------------------------------
  18. // InterpolateToBouncePosition(): interpolates the ball to where it will hot the ground
  19. //----------------------------------------------------------------------------------------------
  20.  
  21. void CTennisBall::InterpolateToBouncePosition(POINT &pt) {
  22.     float D = vz*vz+2*pz*GROUND_ACCELERATION;
  23.     if (D>=0) {
  24.         float t = (vz-(float)sqrt(D)) / (-GROUND_ACCELERATION);
  25.         t += 20; // let the ball bounce first
  26.         pt.x = (int)(px+vx*t);
  27.         pt.y = (int)(py+vy*t);
  28.     }
  29. }
  30.  
  31. //----------------------------------------------------------------------------------------------
  32. // BounceBall(): bounces the ball with a paddle
  33. //----------------------------------------------------------------------------------------------
  34.  
  35. void CTennisBall::BounceBall(float x, float y) {
  36.     SetVelocity(x, y, UPWARD_BOUNCE_FORCE);
  37.     InterpolateToBouncePosition(BouncePosition);
  38. }
  39.  
  40. //----------------------------------------------------------------------------------------------
  41. // Update(): Updates the ball state
  42. //----------------------------------------------------------------------------------------------
  43.  
  44. void CTennisBall::Update() {    
  45.     ApplyGroundForce();
  46.     ApplyVelocity();
  47. }
  48.  
  49. //----------------------------------------------------------------------------------------------
  50. // GetBoundingRect(): Returns a bounding rectangle for the ball
  51. //----------------------------------------------------------------------------------------------
  52.  
  53. void CTennisBall::GetBoundingRect(RECT * lpRect) {
  54.     lpRect->left = (int)px-BALL_SIZE;
  55.     lpRect->right = (int)px+BALL_SIZE;
  56.     lpRect->top = (int)py-BALL_SIZE;
  57.     lpRect->bottom = (int)py+BALL_SIZE;
  58. }
  59.  
  60. //----------------------------------------------------------------------------------------------
  61. // ApplyGroundForce(): Applies ground force to the ball
  62. //----------------------------------------------------------------------------------------------
  63.  
  64. void CTennisBall::ApplyGroundForce() {
  65.     if (pz > 0) {
  66.         vz = vz + NormSpeed(GROUND_ACCELERATION);    
  67.     }
  68. }
  69.  
  70. //----------------------------------------------------------------------------------------------
  71. // ApplyVelocity(): Applies the velocity vector to the ball
  72. //----------------------------------------------------------------------------------------------
  73.  
  74. void CTennisBall::ApplyVelocity() {
  75.     pz = pz - NormSpeed(vz);
  76.  
  77.     if (pz < 0 && owner != OWNER_SERVICE) {
  78.         NotifyAll(HIT_GROUND_EVENT);
  79.     }
  80.     
  81.     if (pz < 0) {
  82.         vz = - (vz-GROUND_FRICTION);
  83.         pz = (float)abs((int)vz);
  84.         if (pz<GROUND_ACCELERATION) {
  85.             pz = 0;
  86.             vz = 0;
  87.         }        
  88.     }
  89.  
  90.     if (pz == 0) {
  91.         NotifyAll(BALL_ROLLING_EVENT);
  92.     }
  93.     
  94.     px = px + NormSpeed(vx);
  95.     py = py + NormSpeed(vy);
  96. }
  97.  
  98. //----------------------------------------------------------------------------------------------
  99. // Paint(): Paint the ball
  100. //----------------------------------------------------------------------------------------------
  101.  
  102. void CTennisBall::Paint(LPDDS lpdds) {
  103.     HDC dc;
  104.     lpdds->GetDC(&dc);
  105.     int scale = BALL_SIZE + (int)(pz/2);
  106.  
  107.     HBRUSH brush = CreateSolidBrush(RGB(255,255,0));
  108.     HBRUSH oldBrush = (HBRUSH)SelectObject(dc, brush);    
  109.     HPEN pen = CreatePen(PS_NULL, 0, 0);
  110.     HPEN oldPen = (HPEN)SelectObject(dc, pen);
  111.  
  112.     Ellipse(dc, (int)px-scale, (int)py-scale, (int)px+scale, (int)py+scale);
  113.  
  114.     if (oldPen != NULL) {
  115.         SelectObject(dc, oldPen);
  116.         DeleteObject(brush);
  117.     }
  118.     if (oldBrush != NULL) {
  119.         SelectObject(dc, oldBrush);
  120.         DeleteObject(pen);
  121.     }
  122.     
  123.     lpdds->ReleaseDC(dc);
  124. }
  125.