Game Development Reference
In-Depth Information
3. Then, create a method called
modifyTerrain
that takes a Boolean called
pickupCube
as the input.
4. To control what is picked up or aimed at, use a pattern that we have established in
the
Firing in FPS
recipe of
Chapter 2
,
Cameras and Game Controls
. Use a ray
that originates from the camera and moves toward the camera's direction.
5. Now, collide it with the
worldnode
class of
cubeWorld
. If it collides with
something and the distance is lower than two (or some other arbitrary number)
and
pickupCube
is true, we will pick up a cube. Get the
worldTransla-
tion
vector of the geometry that the ray has collided with. Then, call a method
called
changeTerrain
in
cubeWorld
. We'll create the method in a short
while. Now, supply it with the coordinates of the geometry it collides with and the
currently empty
takenCube
field as follows:
if(coll != null && coll.getDistance() < 2f &&
pickupCube){
Vector3f geomCoords =
coll.getGeometry().getWorldTranslation();
takenCube = cubeWorld.changeTerrain(geomCoords,
takenCube);
}
6. If instead, there is no collision or the collision is too far away, and at the same
time
pickupCube
is
false
and
takenCube
is not null, try to place
takenCube
in the world. Since we don't have a collision point, move some way
along the direction of the camera and round it off to the nearest integer. Then, call
cubeWorld.changeTerrain
again with the coordinates along with
takenCube
, as follows:
Vector3f geomCoords =
cam.getLocation().add(cam.getDirection().mult(2f));
geomCoords.set(Math.round(geomCoords.x),
Math.round(geomCoords.y), Math.round(geomCoords.z));
takenCube = cubeWorld.changeTerrain(geomCoords,
takenCube);
7. In the
onAction
method, add the logic for the corresponding key press and call
modifyTerrain
, supplying either
true
if we're picking up or
false
if we're
instead trying to place a
CubeCell
field.