📓
Nez framework documentation
  • Nez Setup
  • Nez Core
  • Scene-Entity-Component
  • Rendering
  • Content management
  • Dear IMGUI
  • Physics
  • Farseer physics
  • Verlet physics
  • Entity Processing Systems
  • Deferred lighting
  • Nez UI
  • Pathfinding
  • Runtime Inspector
  • Svg Support
  • AI (FSM, Behavior Tree, GOAP, Utility AI)
  • Links
Powered by GitBook
On this page
  • Basic Example
  • Verlet Objects: Particles, Constraints and Composites

Was this helpful?

Verlet physics

PreviousFarseer physicsNextEntity Processing Systems

Last updated 5 years ago

Was this helpful?

includes a flexible system. It comes with example objects such as lines, tires, balls, cloth and more. differ greatly from the normal rigid body physics most folks are accustomed to (, , , , etc). The goal of Nez's simulation is not to be a full physics simulation. It is here to provide some neat effects and interactions to jazz up your games.

A few things should be noted up front: the system is not a renderer. It handles the physics simulation. It is up to you to render your meshes and textures on top of the simulation data. Verlet objects can interact with standard but in the name of efficiency they will not interact with each other.

Basic Example

Getting started using the is pretty simple. All you have to do is create a object and call it's Update method. An example that does this is below. The verlet system has a built-in debug visual system. It is being used here so that you have something to visualize.

public class VerletDemo : RenderableComponent, IUpdatable
{
    public override float Width { get { return 800; } }
    public override float Height { get { return 600; } }

    World _world;

    public override void OnAddedToEntity()
    {
        // create the verlet world which handles simulation
        _world = new World( new Rectangle( 0, 0, 800, 600 ) );

        // add a couple built-in Composite objects
        _world.AddComposite( new Tire( new Vector2( 100, 100 ), 50, 20 ) );
        _world.AddComposite( new Cloth( new Vector2( 10, 10 ), 200, 100 ) );
    }

    public void Update()
    {
        _world.Update();
    }

    public override void Render( Batcher batcher, Camera camera )
    {
        _world.DebugRender( batcher );
    }
}

Verlet Objects: Particles, Constraints and Composites

// create the Composite object
var composite = new Composite();

// add some particles. In this case we have three particles in a 'v' shape
composite.AddParticle( new Particle( new Vector2( 50, 50 ) ) );
composite.AddParticle( new Particle( new Vector2( 150, 50 ) ) );
composite.AddParticle( new Particle( new Vector2( 150, 150 ) ) );

// add a squishy AngleConstraint to keep the 'v' angle in place
composite.AddConstraint( new AngleConstraint( composite.Particles[0], composite.Particles[1], composite.Particles[2], 0.1f ) );

// add two DistanceConstraints so the two ends of the 'v' keep their length fairly constant (0.8 stiffness so there is a little give)
composite.AddConstraint( new DistanceConstraint( composite.Particles[0], composite.Particles[1], 0.8f ) );
composite.AddConstraint( new DistanceConstraint( composite.Particles[1], composite.Particles[2], 0.8f ) );

// add the Composite to the World simulation
_world.AddComposite( composite );

The verlet simulation consists of just two objects: and Particles by default have a 0 radius but they can be made as large as you would like. Particles also have mass which is used when integrating movement. You can optionally pin a Particle which will make it fixed in space and you can optionally have Particles collide with normal .

Constraints are just mathematic rules that are solved and adjust the Particle positions. At the very simplest we have a . As it's name implies it will maintain a certain distance between any two Particles. On it's own that isn't very interesting but where it gets fun is that Constraints have a stiffness property. A low stiffness means that the Constraint will squash and stretch whereas a high stiffness will act more rigid.

Also included is which will keep a specific angle between any 3 points. Note that you are not limited to just these Constraints. The verlet system is built to allow you to implement your own unique Constraints. All you have to do is subclass Constraint and implement the abstract methods. You can make constraints do whatever you want.

In the example above, we used two of the built-in Composites, Tire and Cloth (both classes are simple subclasses of Composite). Composites are just a container for Particles and Constraints so that managing them is easier. Whenever you have an object that you will be creating multiple times you can just subclass to encapsulate it. You can also create Composites on the fly. Lets take a look at how to do that:

Nez
verlet physics
verlet physics
Box2D
Chipmunk
PhysX
Bullet
verlet physics
verlet physics
Nez
Colliders
verlet physics
World
Component
Particles
Constraints
Nez
Colliders
DistanceConstraint
AngleConstraint
Composite