Tufts OpenCourseware
Search
Author: Ming Y Chow

Tufts OpenCourseWare
Introduction to Game Development
M. Chow
Spring 2012

 

Physics

Basic Physics: Preliminaries

  • We will assume fixed discrete simulation (constant time step)
  • Approximation
  • Remember, we are working with a 2D coordinate system
  • If you need a formula (e.g., that involves forces or mass), look it up!
  • Keep it simple!
  • We will use the metric system (e.g., m, m/s)

Position and Velocity

  • Question: Where is the object at time t?
  • Velocity - "The rate of change of position along a straight line with respect to time"
  • Remember, speed and velocity are not the same: velocity is a vector quantity (has direction) while speed is a scalar quantity
  • Given variables: object_x, object_y, speed, angle
  • Calculation (in your game loop):
    • scale_x = cos(angle)
    • scale_y = sin(angle)
    • velocity_x = (speed * scale_x)
    • velocity_y = (speed * scale_y)
    • object_x = object_x + velocity_x
    • object_y = object_y + velocity_y

Acceleration

  • Acceleration - "The rate of change of velocity with respect to time"
  • When an object moves along at a certain velocity, an amount of acceleration is continuously added to the velocity
  • Given variables: object_x, object_y, speed, acceleration, angle
  • Calculation (in your game loop):
    • scale_x = cos(angle)
    • scale_y = sin(angle)
    • speed = speed + acceleration
    • velocity_x = (speed * scale_x)
    • velocity_y = (speed * scale_y)
    • object_x = object_x + velocity_x
    • object_y = object_y + velocity_y

Gravity

  • Downwards acceleration
  • Earth's gravity constant = 9.81 meters per second squared
  • In a game, the gravity constant depends on frame rate and the effect
  • Given variables: object_x, object_y
  • Calculation (in your game loop; assume that the object is moving):
    • velocity_y = velocity_y - gravity
    • (If the object has not hit the ground yet) object_y = object_y + velocity_y
    • object_x = object_x + velocity_x
  • Where useful: a character jumping in a sidescroller, projectiles

Projectiles

Collision:

  • Detection:
    • Bounding box method
    • Problem: irregularly shaped objects (thus wasted space). However, empirically, less space will be wasted using the bounding box method over bounding sphere method.
    • Collision test, two methods:
      1. if (object.x >= box.min_x and object.x <= box.max_x and object.y >= box.min_y and object.y <= box.max_y) then collision occurred
        • Use this same approach for mouse picking! That is when in handling mouse events, check to see if the object was picked via x-y coordinates.
      2. Use Pygame's sprite collision methods: pygame.sprite.collide_rect(), pygame.sprite.collide_circle()
        • Each method returns either True or False
        • Ideally used for detecting collision of objects (e.g., cars)
        • Each method takes in two sprites (must extend one of Pygame's sprite base classes)
        • Sprite must have rect attribute and set the location of the bounding box. That is:
          		self.x = x
          		self.y = y
          		self.rect = self.image.get_rect()
          		self.image_w, self.image_h = self.image.get_size()
          		self.rect.move(self.x, self.y)
          		self.rect.topleft = (self.x, self.y)
          		self.rect.bottomright = (self.x + self.image_w, self.y + self.image_h)	
          
        • Each time you move a sprite, you also have to change the location (including the topleft and bottomright) of the bounding box!
        • More details: http://www.pygame.org/docs/ref/sprite.html
        • Example:
          	sprite1 = SimpleSprite(...)
          	sprite2 = SimpleSprite(...)
          	if pygame.sprite.collide_rect(sprite1, sprite2):
          		# Do something
          
  • Response:
    • Different objects respond differently to collisions
    • For most collisions however, most collisions will resemble exactly that of balls in the game of pool (elastic collision)
    • Angle of incidence - The angle between the ball movement vector and the plane of the rail that it strikes when the ball moving in a straight line collides with it. Remember, when the ball hits the rail, it will bounce away from the rail at the same angle that it strikes the rail. In other words, the angle of reflection = angle of incidence
    • Calculation:
      	if (ballPoint.x + ballSpeed.x) <= plane.x:
          	ballSpeed.x = (ballSpeed.x * -1)
      	if (ballPoint.x + ballSpeed.x) >= plane.width:
      		ballSpeed.x = (ballSpeed.x * -1)
      	if (ballPoint.y + ballSpeed.y) <= plane.y:
      		ballSpeed.y = (ballSpeed.y * -1)
      	if (ballPoint.y + ballSpeed.y) >= plane.height:
      		ballSpeed.y = (ballSpeed.y * -1)
      
    • Why does the above work? Think back to high school algebra and geometry...