Game Physics - Extend Unity 3D's Physics Engine in C# Code

Step-by-step tutorial on how to create a basic rigid body motion physics from scratch. Taught in C# using Unity 3D free.
4.7 (304 ratings) Instead of using a simple lifetime average, Udemy calculates a
course's star rating by considering a number of different factors
such as the number of ratings, the age of ratings, and the
likelihood of fraudulent ratings.
12,664 students enrolled
$19
$95
80% off
Take This Course
  • Lectures 45
  • Length 5.5 hours
  • Skill Level Intermediate Level
  • Languages English
  • Includes Lifetime access
    30 day money back guarantee!
    Available on iOS and Android
    Certificate of Completion
Wishlisted Wishlist

How taking a course works

Discover

Find online courses made by experts from around the world.

Learn

Take your courses with you and learn anywhere, anytime.

Master

Learn and practice real-world skills and achieve your goals.

About This Course

Published 12/2014 English

Course Description

Learn to build your own game physics engine. By the end of this mini-course course you will have gained confidence in translating theories of physics into working game code.

I will be teaching using Unity 3D free as the example game engine. I expect you to have some experience with both Unity 3D, and C# before starting this course. If you don't then check out my Complete Unity Developer course.

Why would you take this course?

  1. To understand game physics better.
  2. Practice creating your own physics components.
  3. Improve your knowledge of Newtons Laws of Motion.

There is a thriving community, and we will be providing our usual world-class support that our students have come to expect.

Note: access to this course comes with an optional, free community site where you can share games and 3D models, as well as connect with other students.

See you in there!

What are the requirements?

  • You need to be comfortable with Unity 3D
  • You need to be comfortable with C# scripting.
  • You should be able solve basic algebra equations.

What am I going to get from this course?

  • Translate new laws of physics to code.
  • Understand how game engine physics works.
  • Extend the capabilities of Unity 3Ds physics engine.
  • Transfer this knowledge to other game engines.

What is the target audience?

  • This course is ideal for you if you're running into the limits of your game engine's physics.
  • If you are not confident in how your game engine's physics works "under the hood", this course will give you that insight.
  • This course is probably not for you if you're a physics PhD, who is already confident in coding!

What you get with this course?

Not for you? No problem.
30 day money back guarantee.

Forever yours.
Lifetime access.

Learn on the go.
Desktop, iOS and Android.

Get rewarded.
Certificate of completion.

Curriculum

Section 1: Introduction & Setup
Promo Video
Preview
01:25
03:10

IN THIS VIDEO...

What you'll need to start

  • A Mac or PC that can run Unity 3D or similar.
  • You can use another game engine if you like.
  • Some coding experience (see sample code).
  • Some understanding of mechanics & units.
  • Confidence in basic algebra.


01:48

IN THIS VIDEO...

  • An overview of the interface
  • How to search for things
  • Summary
02:54

IN THIS VIDEO...

  • Download, Install & Register Unity

SOME USEFUL LINKS...

  • Unity - Download Archive

(NOTE YOU CAN CLICK THESE LINKS BELOW)

Section Summary
03:45
Section End QUIZ
5 questions
Section 2: Newtons Laws of Motion
01:26

In this section we will be dealing with a rigid body, and the effect of forces on it.

  1. Unbalanced forces lead to accelerations.
  2. Accelerations lead to a change in velocity.
  3. Velocity determines the change in position.

We will be working backwards through this list, starting with position and eventually creating components to model the effect of various forces on our game object.

Section 2 Assets
Article
Section Notes PDF
13 pages
12:40

IN THIS VIDEO…

  • Introducing Sir Isaac Newton
  • Newton's First Law
  • About the FixedUpdate() loop
  • Updating transform.position

SOME USEFUL LINKS...

  • Using FixedUpdate() - Execution Order of Event Functions

(NOTE YOU CAN CLICK THESE LINKS BELOW)

11:13

IN THIS VIDEO...

Create A Force List

  • using System.Collections.Generic;
  • Create a public list of forces, forceVectorList;
  • Create AddForces () so sum the forces.
  • Debug.LogError () if we have a net force.
  • Otherwise continue to update the position.
08:48

IN THIS VIDEO...

  • 2nd Law Defined
  • Write Code For Acceleration
  • Publicly expose float mass
  • Create UpdateVelocity () method
  • Modify velocityVector every FixedUpdate
  • Remove Debug.LogError() code
08:32

IN THIS VIDEO...

  • Defining Newton's Third Law.
  • Examples of why it's important to consider.
  • Introducing our trail drawing code.
  • Switching trail direction.
  • Recap of Newton's laws
15:36

IN THIS VIDEO...

  • What we're trying to achieve.
  • Why it's important to get this “right”.
  • Re-orgainse list iteration for variable forces.
  • Bring DrawTrails.cs into PhysicsEngine.cs.
  • Move the forces into separate components.
Mid Section QUIZ
5 questions
DOWNLOAD Unity Project
Article
10:52

IN THIS VIDEO...

  • About Units and Dimensions.
  • Why it's worth thinking about them.
  • Where to annotate our units.
  • Prepare our RocketEngine.cs class.

SOME USEFUL LINKS...

  • SI base unit - Wikipedia, the free encyclopedia

(NOTE YOU CAN CLICK THESE LINKS BELOW)

16:41

IN THIS VIDEO...

  • The thrust of a rocket engine.
  • Modelling rocket engines and fuel burn.
  • An introduction to “delta V”.

SOME USEFUL LINKS...

  • Rocket engine - Wikipedia, the free encyclopedia

(NOTE YOU CAN CLICK THESE LINKS BELOW)

16:01

IN THIS VIDEO...

  • Review the universal gravitation equation*
  • Create a method for it in Unity.
  • Check we get realistic force values.

SOME USEFUL LINKS...

  • Gravitational constant

(NOTE YOU CAN CLICK THESE LINKS BELOW)

12:48

IN THIS VIDEO...

  • Setup a football field “on Earth”.
  • Test gravitation matches real values.
  • Explore parabolic flight.
  • Set scene for air resistance.
13:09

IN THIS VIDEO...

  • Design a simple formula for air resistance.
  • Create an Air Drag component.
  • Test flight against reference ball.
  • Tweak to get similar results to Unity's engine.

SOME USEFUL LINKS...

  • Drag (physics)

(NOTE YOU CAN CLICK THESE LINKS BELOW)

10:33

IN THIS VIDEO...

  • Expand on Unity's script execution order*
  • Demonstrate our bug at “high” speeds.
  • Show how this relates to fixed update time.
  • Move Universal Gravity to separate object.

SOME USEFUL LINKS...

  • Execution Order of Event Functions

(NOTE YOU CAN CLICK THESE LINKS BELOW)

19:51

IN THIS VIDEO...

  • Make a simple hit-the-target game.
  • Add a cylinder to represent a rocket launcher.
  • Write Launcher.cs class.
05:44

IN THIS VIDEO...

  • Re-introduce Universal Gravitation.
  • Review the overall structure of what we've done.
02:43

WHAT YOU'VE LEARNT...

  • Basic physics engine architecture / trade-offs.
  • A foundation in Newton's Laws of Motion.
  • Dimensional checking as a tool.
  • How to distill information from Wikipedia etc.
  • How to create components for various forces.
Section End QUIZ
6 questions
Section 3: Simple Rotation
Introduction To Section 3
01:57
Article
  • Section Assets Pack
  • Previous section project, for Magnus Effect
Section Notes
8 pages
08:37
  • The limitations of spinning objects in Unity 5.
  • Why unstable rotation doesn't evolve.
  • How to model unstable rotation in game physics.
13:39
  • Roughly what is an inertia tensor (I)?
  • Unstable intermediate axis
  • How Unity Calculates I
  • Calculate I for your phone
  • Predict it's unstable axis
11:35
  • How about if two axis are equal?
  • Try it with a spinning cylinder.
  • Calculate the moment of inertia.
  • Render vs. collider mesh influence.
10:19
  • NOT the same as Intermediate Axis Theorem.
  • How to calculate I for compound objects.
  • Think of a discus thrower.

http://en.wikipedia.org/wiki/Parallel_axis_theorem

08:45
  • A recap of Newton's laws of motion.
  • How these relation to rotation.
  • Important differences.
  • Why we're jumping in the deep end!
11:02
  • How to use Unity's AddTorque() method.
  • How Inertia Tensor affects rotation.
  • Comparing the rotation of two objects.
07:13
  • What a cross product is.
  • Why they are so useful.
  • Using WolframAlpha to calculate.
11:10
  • What the Magnus Effect is.
  • How we can approximate ball flight with it.
  • Using Vector3.Cross in Unity.

The code you need is below...

public float magnusConstant = 1f;

private Rigidbody rigidBody;


void Start () {
rigidBody = GetComponent<Rigidbody> ();
}


// Update is called once per frame
void Update () {
rigidBody.AddForce (magnusConstant * Vector3.Cross (rigidBody.angularVelocity, rigidBody.velocity));
}

Section 3 Wrap-Up
02:37
Section 4: Making Contact
Introduction To Section 4
02:40
Your Section 4 Assets
Article
Your Section Notes
7 pages
08:25
  • Why friction is so important.
  • About physics materials and friction.
  • How static and dynamic friction relate.
  • Using the Friction Direction 2 setting.
  • Limitations of Unity's friction, and to improve it.
10:35
  • Welcome “Mu-s”, the coefficient of static friction.
  • What the static friction coefficient means.
  • How this coefficient relates to slope angle.
  • Two triangle trigonometry take-home tricks.
  • Testing critical angle in Unity Editor.
08:55
  • Discover slipping angle.
  • Try three different static friction coefficients.
  • About rolling, and “impulsing” the block.
  • Generally horsing around with blocks and slopes.
03:46
  • Why we care about friction combine.
  • The limitations of Unity's model.
  • How friction combine precedence works.
05:32
  • What is dynamic friction.
  • How it relates to terminal velocity on a slope.
  • A look at some Stribeck curves.
03:43
  • What the Friction Direction 2 setting does.
  • How it might be useful.
  • A note on using unit vectors.
  • Friction Direction 2 doesn't work in Unity 5!
02:45

Congratulations and how to continue your learning.

Students Who Viewed This Course Also Viewed

  • Loading
  • Loading
  • Loading

Instructor Biography

Ben Tristem, Best-selling Instructor, Game Developer, Online Entrepreneur

Hi, I'm Ben. I have a degree in computing from Imperial College London, and a physics diploma from the Open University.

I started working for myself at the age of 15, and never looked back. I explored careers as varied as being a commercial pilot, stunt-man, rock climbing instructor, and more. None of these provided a continued challenge, and stable income in the same way technology does.

After building and selling a home computer support business, I became an angel investor and business mentor here in Cambridge UK. I fell in love with teaching game development through one of my investments, and I now spend all of my time sharing my passion with people like you.

I can't wait to help you experience the fulfilment, and financial freedom, that having a deep understanding of technology brings.

So why not start learning to make games with me and my team now?

See you soon!

Ben

Ready to start learning?
Take This Course