Click here to Skip to main content
Click here to Skip to main content

Believable physics in C#

, 29 Dec 2003
Rate this:
Please Sign up or sign in to vote.
How to create believable physics using C#.

Sample Image - parachute.jpg

Introduction

After reading the great article 'Advanced Character Physics' [1], I realized that, contrary to my earlier belief, creating believable physics on a computer isn't overly complicated. In fact, it's rather simple and isn't especially CPU-hungry neither. As I figured this might be an interesting topic for some developers, I put together these projects and this article for CodeProject.

Setting up

Requirements

To be able to compile and use the projects, you'll need the managed extensions for Directx9 [2]. You might need to perform an extra step after installing the DirectX9 SDK. Please refer to the readme.txt that comes with the DirectX9 SDK. The project ParticleSystem depends upon DirectX9 for some basic operations on vectors and matrices. The test application depends on DirectX9 more heavily as it uses DirectX9 to visualize the particle-constraint system.

Compiling

Once you have the managed extensions for DirectX9, it should be easy to compile the projects.

Running the test application

You should see a parachuting box not unlike the picture above. There are simple commands to move the viewpoint as well as change some parameters of the particle-constraint system.

  • Arrow-keys: Moves the viewpoint.
  • A: Makes gravity vector pointing downward.
  • B: Makes gravity vector pointing upward.
  • S: Shakes the particles.
  • K: Increase the strength of the wind (that affects the parachute).
  • L: Decrease the strength of the wind (that affects the parachute).

The code in the test project is close to awful as I only patched the DirectX9 wizard generated code just enough so I could view the particle-constraint system. The interesting code is in the ParticleSystem project.

How to create a simple particle-constraint system

Let's assume you want to create a very simple system with only one small box in it.

// Begin a transaction
ParticleSystem.Transaction lTransaction = ps.GetTransaction(); 
// Adds a global constraint to the system (to make sure that the box 
// doesn't fall and fall and fall)
lTransaction.AddGlobalConstraint(new AxisAlignedBox( 
    new Vector3(-10, -15, -10), 
    new Vector3(10, 15, 10))); 
// Set the gravity vector of the system
ps.Gravity = new Vector3(0, -1, 0); 
// Creates a stick-constraint factory (since we want our box to be created 
// using stiff stick-constraints)
Tools.ITwoParticleConstraintFactory lStickConstraintFactory = 
    new Tools.StickConstraintFactory(); 
// Create the box at position (0,0,0), with mass 10.0 and side-length 1.0
Tools.CreateBox(
    new Vector3(0.0f, 0.0f, 0.0f), 
    10.0f, 
    1.0f, 
    lStickConstraintFactory, 
    lTransaction); 
// Commit the added particles and constraints to the system
lTransaction.Commit();

If you want to test what the example looks like, just replace the code in the function InitializeParticleSystem with the code above. Also you have to add the row windEffect = null;

Now let's say you wish to attach a chain to the box and the non moving particle. The code would then look as this:

// Begin a transaction
ParticleSystem.Transaction lTransaction = ps.GetTransaction(); 
// Adds a global constraint to the system (to make sure that the box 
// doesn't fall and fall and fall) 
lTransaction.AddGlobalConstraint(new AxisAlignedBox(
    new Vector3(-10, -15, -10), 
    new Vector3(10, 15, 10))); 
// Set the gravity vector of the system 
ps.Gravity = new Vector3(0, -1, 0); 
// Creates a stick-constraint factory (since we want our box to be created 
// using stiff stick-constraints) 
Tools.ITwoParticleConstraintFactory lStickConstraintFactory = 
    new Tools.StickConstraintFactory(); 
// Create the box at position (0,0,0), with mass 10.0 and side-length 1.0 
Particle[] lBoxParticles = Tools.CreateBox(
    new Vector3(0.0f, 0.0f, 0.0f), 
    10.0f, 
    1.0f, 
    lStickConstraintFactory, 
    lTransaction); 
// Create a single particle 
Particle lParticle = new Particle(1.0f, new Vector3(0.0f, 5.0f, 0.0f)); 
// Make the particle non-moving 
lParticle.Immoveable = true; 
// Add the particle the system 
lTransaction.AddParticle(lParticle); 
// Create a chain between the single particle and a box-particle 
Tools.CreateChain(
    lParticle, 
    lBoxParticles[0], 
    1.0f, 
    6, 
    lStickConstraintFactory, 
    lTransaction); 
// Commit the added particles and constraints to the system 
lTransaction.Commit();

For a more complex example, see the parachuting box code in the ParticleSystemTestApplication.

A tiny introduction to the ParticleSystem project

  • The project is named ParticleSystem. A better name would be ParticleConstraintSystem because that's essentially what it is.
  • The design goal of ParticleSystem was primarily to make it easy for a developer to add new constraints, effects and global constraints classes. Also, I wanted to make it reasonably easy to create "experiments". Performance hasn't been a primary concern.
  • You add instances of particles, constraints, effects and global constraints to a ParticleSystem using the Transaction object. You get an instance of a Transaction object by calling GetTransaction() on an instance of a ParticleSystem. Remember to commit.
  • To generate the next frame, you call TimeStep().
  • You can create own constraints, effects and global constraints by implementing the required interface. For instance, to create a constraint, the object has to implement IConstraint which only has one method: Apply().
  • A reasonable question is what should a constraint do when it gets a call to Apply()? It should check if the constraint is invalid. If its invalid, then it should move the particles that's part of the constraint so that the constraint becomes valid again. As an example, take a look at the StickConstraint; if the particles are too far away, it pulls them together until they are at the correct distance. If they are too close, it pushes them away until they are at the correct distance. It also looks at the mass of the particles and moves differently depending on how much they weigh. Please see 'Advanced Character Physics' [1] for a better insight of how to create constraints.
  • The Tool-class contains a lot of static methods intended to make it easier for a developer.

Exercises for the interested reader:

This is of course an ironical nudge towards the impossible 'exercises for the interested reader' that are so often found in student literature.

  • Implement a 'real' parachute. Instead of having a fixed wind blowing upwards, the particles in the parachute could be affected by a wind that's dependent on average velocity of the particles in the parachute. That is, the faster they are going down, the stronger the wind is blowing upwards.
  • Implement a sailboat game.

TODO:

  • Add NUnit tests
  • Improve the documentation and provide a NDoc project
  • Reengineer the gravity to be a IEffect instead of an intrinsic part of the ParticleSystem-class. This to allow systems where you have a centre of gravity or many gravity sources.
  • Implement collision detection between particles and triangles. This would enable some very cool things such as; solid bodies bouncing against each other, trampolines and friction.

Acknowledgments

Thomas Jakobsen - for without his great article [1], I doubt that I ever would have started playing around with imaginary particles and constraints (and thus leading a lesser life). Also, I have shamelessly robbed code from his article when building ParticleSystem.

References

Change History

  • 2003-12-27 - Article posted.
  • 2003-12-27 - 'Change history' section added.
  • 2003-12-27 - 'About the Author' section removed.
  • 2003-12-30 - Source code and binaries updated 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Mårten R
Web Developer
Sweden Sweden
No Biography provided

Comments and Discussions

 
GeneralHmmm #2 or Compiller error PinmemberBatishchev1-Feb-04 13:32 
I get some problems with compilation..
First of all, i have VS 7.0 (not 8,0 as your)
that's why i changed
in *.sln Format Version to "7.00" (not 8.00)
and in *.csproj SchemaVersion to "1.0" (not 2.0)
was i right?
but now, on open any *.cs i get a lot of 'Object type cannot be converted to target type' errors, but it's not main problem..
On running (compiled myself) ParticleSystemTestApplication.exe every time i see:
An exception has occured. The sample must exit.
Error in the application
-2146232832 (Unknow) at Microsoft...Device.DrawPrimitives(PrimitiveType primitiveType, Int32 startVertex, Int32 primitiveCount)
at Render() [d3dtriangle.cs: line 283]
at Render3DEnv.() [d3dapp.cs: line 988]
at FullRender() [d3dapp.cs: line 890]..
What i need to do? any ideas?
 
PS Sorry for poor english - i'm from russia Cool | :cool: WTF | :WTF:
 
When using email don't bother people with spam letters.
If spammer bothers you, politely ask him to stop.
If he does not stop, destroy him at all costs
Generalhmmm PinsussRimRimRim29-Dec-03 7:12 
GeneralRe: hmmm PinmemberMårten R30-Dec-03 4:54 
GeneralDirectX SDK PinmemberMårten R27-Dec-03 5:05 
GeneralRe: DirectX SDK PinmemberBill Seddon28-Dec-03 1:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 30 Dec 2003
Article Copyright 2003 by Mårten R
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid