Tricks of the Windows Game Programming Gurus:Playing God: Basic Physics Modeling

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

### Basic Ad Hoc Collision Response

As I explained earlier in the chapter, two kinds of collisions exist: elastic and non-elastic. Elastic collisions are collisions where both kinetic energy and momentum are conserved in the colliding objects while non-elastic collisions don’t conserve these values and energy is converted to heat and/or used for mechanical deformations.

Most video games don’t even try to mess with non-elastic collisions and stick to simplified elastic collisions since they themselves are hard enough to compute. Before I show you the real way to do it let’s use the other side of our brains. Game programmers that don’t know anything about elastic or inelastic collisions have been faking collisions for years and we can do the same.

#### Simple x,y Bounce Physics

Take a look at Figure 13.20. It depicts a fairly common collision problem in games, that is, bouncing an object off the boundaries of the screen. Given the object has initial velocity (xv,yv), the object can hit any of the 4 sides of the screen. If one object collides with another object that has mass much greater than the colliding object, then the collision is much simplified since we only need to figure out what happened to the single object that’s doing the colliding rather than two objects. A pool table is a good example of this. The balls have very small mass in comparison to the pool table.

Figure 13.20 The bouncing ball.

If you want to use the preceding technique as a quick cheat to make objects bounce off of each other, simply assume that each object is a bounding rectangle from the other object’s point of view. Enact the collision and then re-compute the velocities. Figure 13.22 illustrates this.

When a ball hits one of the sides then it always reflects off the side at an angle equal and opposite to its initial trajectory, as shown in Figure 13.20. Thus all we need to do to bounce an object off a pool table-like environment that consists of hard edges that have large mass is to compute the normal vector direction, the direction that the object struck at, and then reflect the object at the same angle as shown in Figure 13.21.

Figure 13.21 Bouncing a ball off an irregular object with flat facets.

Figure 13.22 A simple cheat for object-to-object collisions.

Although this isn’t as complex as the general elastic collision, it still takes a bit of trigonometry, so there’s got to be a simpler way! And of course there is. The trick is to understand the physics model that you’re trying to model. Then the idea is to see if you can solve the problem in some other way since you have exact knowledge of all the conditions. Here’s the trick: Instead of thinking in terms of angles and all that, think in terms of results. The bottom line is if the object hits a wall to the east or west then you want to reverse its X velocity while leaving its Y velocity alone. And similarly on the north and south walls, you want to reverse the Y velocity and leave the X velocity alone. Here’s the code:

    // given the object is at x,y with a velocity if xv,yv

    // test for east and west wall collisions

    if (x > EAST_EDGE || x < WEST_EDGE)

       xv=-xv; // reverse x velocity

    // now test for north and south wall collisions

    if (y > SOUTH_EDGE || y < NORTH_EDGE)

       yv=-yv; // reverse y velocity

> Tip: If you ever try to get a game programming job, I can almost guarantee they will ask you this question on the interview because it’s deceptively complex. Luckily, you can just read this section and blurt out the answer, and they’ll think you’re a genius! Let’s get started.

And amazingly the object will bounce off the walls. Of course, this simplification only works well for horizontal and vertical barriers. You’ll have to use the more general angle calculation for walls or barriers that aren’t co-linear with the x and y axes.

As an example of using these techniques, I have created a demo named `DEMO13_6.CPP|EXE` that models a pool table with balls that never stop bouncing. Figure 13.23 shows a screen shot of the game in action. Note that when running the simulation the balls don’t hit each other, just the edges of the pool table.

Figure 13.23 Simple collision pool table model.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.