Game Development Reference
In-Depth Information
4. The second thing is a change to
ClientMessageHandler
. If the message is
an instance of
PhysicsObjectMessage
, it should get the
physicsObject
Map
from the
Game
class as follows:
Map<Integer, Spatial> physicsObjects =
game.getPhysicsObjects();
5. A spatial should then be selected based on the
objectId
in the message as fol-
lows:
int objectId = physicsMessage.getObjectId();
Spatial s = physicsObjects.get(objectId);
6. The rotation and translation should be applied as
physicsLocation
and
physicsRotation
respectively on the spatial's
RigidBodyControl
:
PhysicsObjectControl physicsControl =
s.getControl(PhysicsObjectControl.class);
if(physicsControl.getId() == objectId){
s.getControl(RigidBodyControl.class).setPhysicsLocation(physicsMessage.getTranslation());
s.getControl(RigidBodyControl.class).setPhysicsRotation(physicsMessage.getRotation());
}
7. Now, the pipeline for transmitting physics updates from the server to the clients
should work. If we run it, not much is happening. This is because the players in
physics. They were simply coded to stick to the surface of the terrain. We can
change the player's representation to handle this.
8. In
ServerPlayerControl
, we add a
BetterCharacterControl
field
called
physicsCharacter
and a Boolean field called
usePhysics
.
9. Next, we override the
setSpatial
method, and perform a check to see whether
the spatial supplied has
BetterCharacterControl
. If it does,
usePhys-
ics
should be set to
true
and the local
physicsCharacter
field should be
set to
spatial
as follows:
if(spatial.getControl(BetterCharacterControl.class)
!= null){
usePhysics = true;
physicsCharacter =