Game Development Reference
In-Depth Information
How to do it...
Based on the first rule, we'll start by defining a new
Control
class for our networked
physics objects:
1. We create a new class called
PhysicsObjectControl
that extends
Ab-
stractControl
.
2. It should have two fields: a Boolean field called
serverControlled
and an in-
teger field called
id
.
We now define a network message to handle updates to objects with physics:
1. Let's call it
PhysicsObjectMessage
and have it extend
AbstractMes-
sage
.
2. There are three mandatory fields for it; they are as follows:
◦ The first is an integer field called
objectId
◦ It also needs a
Vector3f
field called
translation
◦ Finally, we add a
Quaternion
field called
rotation
3. Don't forget to add the
@Serializable
annotation, and add it to the list of mes-
sages in the
GameUtil
class!
4. The last common implementation we do is for the
Game
class where we add a list
of
Spatials
called
physicsObjects
; the following code tells us how to do
this:
private Map<Integer, Spatial> physicsObjects = new
HashMap<Integer, Spatial>();
Now, we can dig into the server-side implementation by performing the following steps:
1. We contain most of the code in a new
AppState
class called
ServerPhysic-
sAppState
. This
AppState
class will contain the reference to the
Bul-
letAppState
class, and it will handle the initialization.
2. Inside its
initialize
method, it should add the loaded level to
physic-
sSpace
as follows:
bulletAppState.getPhysicsSpace().add(server.getLevelNode().getChild("terrain-TestScene").getControl(PhysicsControl.class));