Game Development Reference
In-Depth Information
Understanding constraints
Constraints, in their most basic form, are the rules which limit the range of motion of
an object relative to some specific object or point in space. For example, think of a
desk chair. It is made up of multiple parts, but if we push the base of the chair, the rest
must move with it. The same happens if we push the top section; so even though the
chair is made of multiple pieces, they are constrained to one another by a handful of
Constraints can be used to also simulate the independent rotation of the desk chair's
top section relative to the base. The top section is able to rotate around an axis
without any dependence on what the bottom section is doing. This constraint is simu-
lated by hooking the top section to an invisible point, and only allowing rotation around
a single axis about that point.
Constraints can vary in how strongly they influence their target objects. A strong con-
straint enforces its limitation on movement at all times as strongly as it can. So, if two
objects are connected by a very strong, rigid constraint, it is the equivalent of being
attached together by invisible and unbreakable glue. In other words, if one object is
moved one unit in space, then the other must move one unit in space to follow it.
Weaker constraints are more like springs. Under the same scenario, the first object
might move one unit in space, but the second moves somewhat less, causing the two
objects to come closer together, or pushed further apart. In addition, the more they
are pushed away from their resting position, the harder the constraint pulls them back;
if we recall our Newtonian physics, this is much like how a simple spring functions.
Picking up objects
A feature of most games is to allow the player to pick up and move the objects around
with the mouse cursor or touch screen (also useful for debugging and testing!). There
are several ways to achieve this, such as with forces, or updating the rigid body's
transform each iteration, but we would like to use a constraint to achieve this effect.
The idea is to use our existing raycasting functionality to detect which object was se-
lected and the exact point of a mouse click. We then create a new constraint at that
point and attach it to the selected object. Then, every time we move the mouse (while
the mouse button is still held down), we update the position of the constraint. The ex-
Search WWH ::

Custom Search