Game Development Reference
In-Depth Information
Figure
9-16 .
The
springboard
consists
of
the
following
(from
top
to
bottom):
the
board
(spring-
board_spring.png), the spring (springboard_spring.png), and the base (springboard_metal.png)
The board will be affixed to the spring, which poses an interesting question: how do you
get two independent physics bodies to move together without their relative positions devi-
ating at all?
Short answer: you can't do it. And you do not need to use two physics bodies.
No joint is “strong” enough to immovably affix one body to another. Two bodies connec-
ted with a joint can always deviate in their relative position to each other, no matter how
stiff the connection is set up to be. You could also update the affixed body's position every
frame after the physics simulation has run, but this may lead to undesirable physics colli-
sion behavior because the affixed body would snap rather than move into place.
The actual solution is to use just a single physics body that defines the collision properties
and shape for the combined object. The affixed “bodies” are simply child sprites of the ac-
tual body, without physics. In this case, the spring will define the collision shape for both
the spring and board, while the board has no physics body and is simply added as a child
of the spring, thus following it at a fixed offset.
Another issue is how to keep the spring moving straight up, rather than rotating or moving
sideways when it hits an object. This requires adding an invisible shaft to the base body
and the aforementioned invisible “stopper” body that stops the spring at just the right mo-
ment.
 
Search WWH ::




Custom Search