GTCS Game Engine:
RigidShape Physics Tutorial
Introduction
This tutorial will cover the physics aspects of the Rigid Shapes and the variables that controls how the RigidShapes reacts with other shapes.
There are 3 variables that affect the physics of the RigidShape.
Mass | How heavy the shape is |
Restitution | How much velocity is kept when colliding with another object |
Friction | How much it clings to other objects when it collides with them |
Now let’s take a closer look at these variables to get a better idea at how changing them will affect how the object interacts with others.
Object Exapmles: Bouncy Ball • Soccer Ball • Rock • Ice Block • Wood Ball • Bowling Ball
Environment Examples: Wood • Ice • Dirt • Mud
Source Code: You can download the source code from here.
Demo: You can try the demo for the RigidShape Physics here.
Mass
If an object collides with another object with less mass, the one with the lighter mass will be affected more. So creating an object with high mass will make it less likely to be affected by other objects.
Objects with a mass of 0 don’t move and are unaffected by any collision so when you wish to create a piece of environment that utilizes physics but will never move, set the mass to 0.
Restitution
This determines how much velocity both it and objects that collide with it retain. Put simply, the higher the value the more it’ll bounce when colliding with other objects. The reverse applies as well.
Friction
This determines how much resistance a shape will have when colliding with other shapes. Another way to see friction is how much slipperiness a shape will have. So if a RigidShape has little friction, it will just slip away at fast speeds. While if it has a lot of friction. It’ll move slow if at all.
Let's look at some Objects and Environments that can be created.
Objects
These are RigidShapes that can be interacted with and influinced by other RIgidShapes
Bouncy Ball
Mass | 1 |
Restitution | .9 |
Friction | .7 |
The most important value in this is the restitution. Because this shape is trying to replicate a bouncy ball, it’s very important that the restitution is high so that it can replicate that bouncy rubber effect.
Soccer Ball
Mass | 1 |
Restitution | .7 |
Friction | .6 |
This is a ball that trades the level of bounciness a bouncy ball would have for a bit less friction so that it can maintain its motion for longer when rolling on another shape.
Rock
Mass | 20 |
Restitution | .4 |
Friction | .8 |
A lot of mass is important because the point of this shape is that it’s isn’t affected by lighter objects. We want enough restitution that objects can bounce off a rock, but not enough that the rock itself becomes bouncy.
Ice Block
Mass | 1 |
Restitution | .4 |
Friction | .01 |
Friction is set extremely low because when colliding with other objects the desired effect is to slide as fast as possible. The reason for the restitution amount is the same as the rock, to allow for other objects to bounce off of the ice block.
Wood Ball
Mass | 1 |
Restitution | .5 |
Friction | .5 |
Wood has a fair amount of Friction and Restitution to it so by setting both to .5 you’ll have a fair balance to both without either being too much.
Bowling Ball
Mass | 10 |
Restitution | .3 |
Friction | .2 |
The two important aspects of bowling balls are that they’re heavy and the don’t have too much friction. Setting the friction to .2 will allow for smooth travel while still differentiating itself from ice.
Environments
Here are some examples of environments you can use for walls, floors, platforms, etc. All environment RigidShapes have a mass of 0.
Wood
Restitution | .8 |
Friction | .5 |
Compared to the object version, this has a higher restitution, so it can support other objects bouncing off it. Since environments have a mass of 0, there’s no need to worry about how a larger restitution will affect the RigidShape. Just like with the wood object, the friction is set to .5 for a balance between having a good grip while still not being too slippery.
Ice
Restitution | .6 |
Friction | .01 |
Like the ice object, the friction is put extremely low to allow for extremely fast sliding. The restitution is slightly bumped up to allow for other objects to retain more of its velocity upon impact.
Dirt
Restitution | .3 |
Friction | .7 |
While objects can bounce off dirt, it doesn’t retain much of it’s original velocity. Hence, you want to set the restitution so that it allows at least a small bounce. There is a solid amount of friction, more so than wood. As such, putting slightly more friction will achieve the desired result.
Mud
Restitution | .01 |
Friction | .1 |
RigidShapes that collide with mud usually either just sticks or slides a far distance. Since there shouldn’t be objects bouncing off this environment, we set the restitution to be extremely low. The friction is set low to replicate wet mud where an object would slide a fair distance before it stopped.
User Code
Now that you know what these variables do, here's the code to initialize these RigidShapes.
MyGame.prototype.initialize = function(){ this.shapeSet = new GameObject(); var shape = new RigidRectangle(xf, w, h); shape.setMass(0); shape.setRestitution(res); shape.setFriction(frct); this.shapeSet.addToSet(shape); };
xf is a transform that you must provide when intializing the RigidShape. If there isn't one then create one.
Make sure the w and h variables (width and height) match for both the transform and RigidShape.
MyGame.prototype.draw = function(aCamera){ this.shapeSet.draw(aCamera); }; MyGame.prototype.update = function(){ this.shapeSet.update(); gEngine.Physics.processCollision(this.shapeSet,[]); };
Since all the shapes you want to check for collision are going to be in the same GameObjectSet, you only need to call one draw, update, and collision check for all of them.
Conclusion
With this tutorial you should be able to get an idea of how these variables affect the physics of shapes. Using this knowledge, many new types of objects an environments can be created.