Reader small image

You're reading from  Game Physics Cookbook

Product typeBook
Published inMar 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781787123663
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Gabor Szauer
Gabor Szauer
author image
Gabor Szauer

Gabor Szauer has been making games since 2010. He graduated from Full Sail University in 2010 with a bachelor's degree in game development. Gabor maintains an active Twitter presence, and maintains a programming-oriented game development blog. Gabor's previously published books are Game Physics Programming Cookbook and Lua Quick Start Guide, both published by Packt.
Read more about Gabor Szauer

Right arrow

Chapter 16. Springs and Joints

This chapter focuses on springs and joints. A spring will exert some force on one or more objects to create a spring like motion. A joint is a constraint that limits the motion of rigidbodies. Throughout the chapter, we will cover the following topics:

  • Particle Modifications

  • Springs

  • Cloth

  • Physics System Modifications

  • Joints

Introduction


Springs are one of the most powerful tools in any physics engine. On the surface, it may seem like they are only useful for creating oscillating motions, but we can use springs to model soft bodies or even cloth! In this chapter, we will learn how to use the spring formula to build soft body objects. By the end of the chapter, we will have a full cloth simulation working!

Up until this chapter, every rigidbody enjoyed having six degrees of freedom for motion. At the end of this chapter, we will introduce a way to constrain that motion using joints. In this chapter, we will build a simple distance joint that will remove a degree of freedom from a pair of rigidbodies.

Particle Modifications


In this chapter, we will attach particles to springs to create a point mass system. A point mass system contains a number of points that have mass, but not volume. A particle fits this description perfectly. However, as it is, the Particle class does not expose all the functions that we need to achieve this. In order to develop the point mass system, we need to make a few modifications to the Particle class we developed in Chapter 14, Constraint Solving.

Getting ready

In this section, we will make several modifications to the public API of the Particle class. We will introduce setter functions for the mass and friction of particles. We will also introduce getter functions for the velocity and inverse mass of particles. Finally, we will implement a function to add an impulse to particles.

How to do it…

Follow the given steps to prepare the particle class to be used with springs:

  1. Declare the new methods that we will be adding to the Particle class in Particle.h:

    void AddImpulse...

Springs


Springs are important to build realistic objects. In the real world, we use springs everywhere, from watches to the suspension of cars. In games, we can use springs to model these same interactions, or to simulate more complex systems, such as rigidbodies.

Every spring has a Resting Length, sometimes called the spring's Equilibrium. Equilibrium describes the length of a resting spring, that is, when the spring is not contracted or stretched. When a spring is contracted or stretched away from its equilibrium, the spring will try to pull back to its resting length with a force equivalent to the difference of its current length and resting length. This describes Hooke's Law. Mathematically, Hooke's Law is expressed by the following equation:

In this equation, F is the force exerted by the spring, k is the spring constant, and x is the difference between the current length and resting length of the spring. The spring constant represents the strength of the spring, that is, how stiff or...

Cloth


We can use springs to model interesting soft body objects. Unlike a rigidbody, a soft body can change its shape. In this section, we will use springs to simulate cloth. Cloth is implemented as a point mass system. In a point mass system, every vertex of a mesh is represented by a particle. Every particle is attached to other particles by springs to force the object to maintain its shape.

If we arrange all the particles representing the vertices of a cloth in a grid, we can connect every row and column using springs. These springs are the structural springs of the cloth:

This, however, is not enough for an accurate simulation. If we set any one of the particles to have infinite mass so that it does not move, the cloth will collapse into a rope. We can improve the structural integrity of the cloth by adding shear springs. Shear springs connect every particle to its neighbors diagonally:

Having both structural and shear springs makes the cloth behave as expected in the scenario where one...

Physics System Modification


In the last section of this chapter, we will build a stable Cloth class. This class contains a set of particles and three spring systems. For the cloth to actually work, we have to call its physics simulation functions every frame. In this section, we will add cloth support to the PhysicsSystem.

Getting ready

In this section, we will make several modifications to the PhysicsSystem class to add support for cloth simulation.

How to do it…

Perform the following steps to add cloth support to our physics system:

  1. Include the Cloth.h header file in PhysicsSystem.h. Add a vector of Cloth pointers to the PhysicsSystem class. Add a function to register a new cloth into the vector and a function to clear the vector:

    // Start of file unchanced
    #include "Cloth.h"
    
    class PhysicsSystem {
    protected:
        // Previous member variable declarations unchanged
        std::vector<Cloth*> cloths;
    public:
        // Previous member functions unchanged
        void AddCloth(Cloth* cloth);
        void...

Joints


In three dimensions, an object has six degrees of freedom. Three degrees of freedom come from translation and an additional three come from orientation. A constraint takes away one or more degrees of freedom. A joint is a type of constraint that limits the degrees of freedom between two objects. There are several common types of joints:

  • Distance Joint: This keeps bodies a set distance apart

  • Ball Joint: This limits translation to the pivot of two objects

  • Hinge Joint: This allows for rotation around a single axis

  • Slider Joint: This limits rotation and translation to a single axis

  • Fixed Joint: This does not allow movement

  • Motor Joint: This produces some kind of force

Several simple joints can be combined to create more complex joints. We can use joints to model hinges for doors, ragdolls that represent characters, or to simply stick objects to each other.

Getting ready

In this section, we will implement the simplest joint type there is—the Distance Joint. This joint will keep two particles...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Game Physics Cookbook
Published in: Mar 2017Publisher: PacktISBN-13: 9781787123663
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Gabor Szauer

Gabor Szauer has been making games since 2010. He graduated from Full Sail University in 2010 with a bachelor's degree in game development. Gabor maintains an active Twitter presence, and maintains a programming-oriented game development blog. Gabor's previously published books are Game Physics Programming Cookbook and Lua Quick Start Guide, both published by Packt.
Read more about Gabor Szauer