Assignment 3: Game Physics

In this assignment, you will design and implement a very simple game physics engine, tuned toward platformer games. This will increase your practical understanding of:

  • the design of physics engines
  • integration, collision detection and collision response
  • the use of a physics engine in a game

Overview

Physics engines in games are responsible for the movement of bodies in response to forces, the detection of collisions between bodies, and computation of the response to collisions. In this assignment, you will complete a very simple 2D physics engine, and use it in a simple platformer game.

The engine permits the application of forces to 2D rigid bodies. It detects when bodies collide, and modifies the velocity of bodies when they collide.

The engine makes several significant simplifying assumptions. Only linear motion is considered - bodies do not rotate. For the purposes of collision detection, each body is represented by an axis-aligned bounding box (i.e., a rectangle.) No provision is provided for constraints such as hinges or pistons.

Part 1: Physics Engine

Complete a set of classes implementing a simple 2D physics engine. Base your engine on the provided code. A PPhysicsBody component can be added to any game object to place it under control of the physics engine. The core method used by games to move rigid bodies is ApplyForce, which applies a force of a given magnitude and direction to the body. The PPhysicsBody provides a number of properties that game programmers can set:

  • Mass: specifies the mass of the body; higher mass increases inertia
  • Rebounds: specifies whether the body is "bouncy" or not. In a collision, if either body "rebounds", the coefficient of restitution should be set to 0.8; if neither rebounds, the coefficient of restitution should be set to 0.5.
  • Obeys Gravity: specifies whether the force of gravity should be automatically added to this body. E.g., platforms do not normally obey gravity, while the avatar normally does.

A PPhysicsEngine component implements the physics engine itself. It updates on Unity's FixedUpdate cycle. On each invocation of the physics engine, it performs an integration step on each physics body, detects any collisions, and resolves those collisions.

Part 2: Applying the Physics Engine

To illustrate the use of the physics engine, I have provided a simple platformer game, shown in the above video.

Extend this game to include at least one new, interesting feature. For example, you might include a "box gun" which fires little boxes at a random angle into the path of the avatar, or a "puff platform" which emits random upward gusts of wind, or a puzzle platform which can only be solved by pushing boxes into the right place. Describe your novel feature and explain how you implemented it. This part of the assignment will be graded both on the technical correctness of your addition to the game and the imagination and scope behind the extension.

Code to get you started is attached to the assignment description in OnQ.

Part 3: Questions

Provide brief answers to the following questions.

  1. Provide a class diagram for your physics engine. The diagram should show the key classes in the engine and their attributes and operations. Show only the engine itself - do not include the sample platformer game. Provide a brief explanation in English text of the structure of the class diagram.
  2. Does your engine use an explicit or implicit Euler approximation for computing integrals? Briefly explain how this is implemented in the code.
  3. For the purposes of collisions, all entities are treated as an axis-aligned bounding box. Explain how your algorithm would change if you were to add the possibility of using a bounding circle to represent bodies. Sketch an algorithm showing how collision response would be computed. Explain how the collision normal would be determined. Provide a diagram if that helps in your explanation.

Optional Extensions

There are many extensions you could make to this assignment. You are unlikely to have time for these now, but if you are interested in learning more about game physics, you could tackle them once term is done. There is no extra credit for doing any of these extensions - they are purely for fun and learning.

  1. The platformer game has quite complicated code to track what state the avatar is in. This code could be written much simpler using the finite state machine engine that you developed in assignment 2.
  2. Extend the physics engine to support rotations. Consult the Millington text on how to code angular motion.
  3. Introduce additional collider shapes beyond the AABB shape considered in this assignment. E.g., add circular colliders and capsule colliders.

To Hand In

Using OnQ, provide a hand-in consisting of two parts: (1) your code for parts 1 and 2, and (2) a report answering the questions from part 3.

Your modified code should be a zip file containing a folder called physics-yourLastName. That folder should contain your full running solution to parts 1 and 2, as a Unity project. If your project doesn't work, include a description in your report specifying what does and does not work. Note that this should be a zip file, not a rar file, or other compression technology.

Your report should be in PDF format. It should include your name and a title (e.g., Assignment 3 Report). Use 11 pt type with at least 1" margins. UML diagrams should be included in this report, not submitted separately as raw UMLet files.

Evaluation

This assignment will be graded using the rubric posted on OnQ. It is advisable to review this rubric in advance of submitting your work.

© 2019 Nicholas Graham