Game Development Reference
In-Depth Information
How to do it...
With a base application already set up, we can get straight to the creation of the Control
pattern:
1. Create a new class called DeformableControl that extends AbstractCon-
trol . It needs one private terrain field called terrain .
2. Override the setSpatial method and cast Spatial to fit your terrain field;
use terrain = (Terrain) spatial; to do this.
3. Create a method called deform that takes the 2D location, the radius of the de-
formation, and the force as an input. Also, declare two lists that we'll use in the
heightPoints and heightValues methods, as follows:
public void deform(Vector2f location, int radius,
float force) {
List<Vector2f> heightPoints = new
ArrayList<Vector2f>();
List<Float> heightValues = new ArrayList<Float>();
4. Now, we should create a nested for loop statement where we can iterate from -
radius to +radius in both x and y ( z to be correct). See how far from the cen-
ter the point is and calculate the height to change at that location. The decrease of
the force of the impact will be proportional to how far out it is from the center.
Then, save the point in the heightPoints list and the new height in the
heightValues list as follows:
for(int x = -radius; x < radius; x++){
for(int y = -radius; y < radius; y++){
Vector2f terrainPoint = new Vector2f(location.x +
x, location.y + y);
float distance = location.distance(terrainPoint);
if(distance < radius){
float impact = force * (1 - distance / radius) ;
float height = terrain.getHeight(terrainPoint);
heightPoints.add(terrainPoint);
heightValues.add(Math.max(-impact, -height));
}
}
}
Search WWH ::




Custom Search