Game Development Reference
In-Depth Information
5. To wrap up the method, we need to apply the new heights. First, unlock the ter-
rain and then lock it again as follows:
terrain.setLocked(false);
terrain.adjustHeight(heightPoints, heightValues);
terrain.setLocked(true);
6. Since we normally work with 3D vectors rather than 2D vectors, it can be a good
idea to also create a convenience method called deform , which takes Vector-
3f as the input. It converts this input to Vector2f and in turn calls the other de-
form method as follows:
public void deform(Vector3f location, int radius,
float force){
Vector2f pos2D = new Vector2f((int)location.x,
(int)location.z);
deform(pos2D, radius, force);
}
7. Now, trigger the deformation from a method in our application. Firstly, it should
create a new ray instance that originates from the camera, as shown in the fol-
lowing code:
Ray ray = new Ray(cam.getLocation(),
cam.getDirection());
8. Next, create a new CollisionsResults object and check whether the ray in-
tersects the terrain. If there is a collision, call deform on the terrain's Deform-
ableControl object by supplying the contactPoint parameter of the colli-
sion as follows:
CollisionResults cr = new CollisionResults();
terrain.collideWith(ray, cr);
CollisionResult collision = cr.getClosestCollision();
if(collision != null){
terrain.getControl(DeformableControl.class).deform(coll.getContactPoint(),
30, 30f);
}
Search WWH ::




Custom Search