Sunday, January 5, 2014

Game Physics - Jumping

In modeling physical behaviors for the purposes of games, we try to get as close to a realistic model of the world as possible. Things like cars will have a velocity, acceleration, and friction, and even gravity will come into play if you catch air. Generally, we just apply all the forces acting on the car - acceleration from the engine, friction from the brakes, drag from the air, and gravity. Generally these are applied over long periods of time, and so modeling these all as forces and handing them off to the physics engine works very well.

However, jumping is not so simple. Unless we’re talking about rocket-boosted jumps, jumping is not a continuous force applied over time. Instead, it is an impulse - a powerful force with a very short duration, followed by ballistic motion until the jumper returns to earth. And for the purposes of a game, we may not want to think in terms of very large, short-lived forces - it would be preferable to think in terms of maximum jump height. That’s much easier to code with, and makes a level designer’s life a lot easier as well.

Let’s go through the physics to see how we can relate a maximum jump height to the impulse force necessary to produce it.

First, let’s start with the equation for ballistic motion - the motion that occurs after the jumper leaves the ground. We can ignore the horizontal motion for now; we’re only concerned with only the vertical component.

y=y0+vy0t+12gt2

What we want to determine is an expression for the maximum height achieved during the jump. To that, we take the derivative with respect to time, and set it equal to zero:

δyδt=vy0+gt0=vy0+gtt=vy0g

We can plug this value of t in to the original equation to get a maximum value for y. We can also assume that y0 is 0, and that y just refers to the jump height relative to the starting point:

y=vy0vy0g+12g(vy0g)2y=v2y0g+v2y02gy=v2y02g

Looks like the only variables governing jump height are the initial velocity, which is determined by the impulse we talked about earlier, and gravity, which is a constant. We can rearrange terms to get an expression for the initial velocity in terms of the jump height and gravity, which will be useful later on:

vy0=2gy

By convention, +y means up, and therefore g will negative and y will be positive, resulting in only real numbers. If we reversed the convention, it wouldn’t matter: g and y will always have opposite signs, and the equation will still work. We also threw away the ± in front of the square root, because negative initial jump velocities make no physical sense.

Next, let’s figure out the impulse part. For this, we’ll want to use Newton’s Second Law of Motion. It is usually written as follows:

F=ma

When dealing with impulses, however, it’s somtimes easier to write it differently:

FΔt=mΔv

What this says is that to achieve a change in velocity of Δv for a mass m, we need to apply a force F over a duration of Δt. In our case, Δv will be the difference between the pre-jump velocity - zero - and the initial jump velocity, which we calculated as 2gy. To get the jump to be instantaneous, we’ll want the force to be applied over a single step of our physics engine. Usually, it’s easy to query the physics engine directly for that value. And if we’re using a physics engine based on forces, we’ll have to have a mass defined for our jumper as well. The only unknown know is F, which we can solve for:

F=m2gyΔt

Apply this force to the jumper when it jumps, and the peak of its jump should be exactly y.

Why go to all this trouble of calculating a force when I could just set the velocity directly?

It’s certainly possible to do things that way - you just need to be careful you’re not messing up your physics engine, or making weird things happen. For example, if your jumper is moving upwards on a fast platform, and you set the velocity as 2gy directly, the jumper might fall right through the bottom of the platform instead of jumping off of it. The right way to do jumping as a velocity instead of a force would be to do something like this:

    velocity.y += Math.sqrt(-2 * jumpHeight * gravity);

Thursday, January 2, 2014

Markdown Cheat Sheet

Markdown Cheat Sheet

Table of Contents

[TOC]

Outline

# Header 1
## Header 2
### Header 3
...

Basic Formatting

Bold

__bold__
**bold**

bold
bold

Italic

_italic_
*italic*

italic
italic

Blockquote

> Something famous was said

Something famous was said

[link text](www.google.com)

link text

[link text][name]

[name]: www.google.com

link text

<a name="anchor">Anchor</a>
[link to anchor](#anchor)

Anchor
link to anchor

Footnotes

Some text[^footnote]

[^footnote]: Footnote content can be placed anywhere in the document.

Some text1

Images

Inline

![alt text](https://www.google.com/images/logos/ps_logo_6.png)

alt text

Reference

![alt text][id]

[id]: https://www.google.com/images/logos/ps_logo_6.png

alt text

Lists

Bulleted

  • Unordered
  • Lists
    • Nested
    • Multi
      paragrah
  • Mixed
    1. Children

Numbered

  1. Ordered
  2. Lists
    1. Nested
    2. Multi
      Paragraph
  3. Mixed
    • Children

Definitions

Term
Definition 1
Definition 2

Tables

| Header | Centered | Right Align |
| ------ | :------: | ----------: |
| Left   | Center   | Right       |
| Colspan          || Right 2     |
Header Centered Right Align
Left Center Right
Colspan Right 2

Code

 ```python
 def fib(n):
     if n <= 1:
         return 1
     else:
         return fib(n-1) + fib(n-2)
 ```
def fib(n):
    if n <= 1:
        return 1
    else:
        return fib(n-1) + fib(n-2)

Math

Block

$$ e^x = \sum_{i=0}^{\infty}\frac{x^i} {i!}$$

ex=i=0xii!

Inline

The quadratic equation is $ x = \frac{-b\pm\sqrt{b^2-4ac}} {2a} $

The quadratic equation is x=b±b24ac2a


  1. Footnote content can be placed anywhere in the document.