## Monday, March 18, 2013

### Rex Kwan Do Physics

‹prev | My Chain | next›

With the end of the month fast approaching and 3D Game Programming for Kids deadlines looming, it is time to make smaller offerings to the gods of the chain so that I can focus on book writing. There still remain a few outstanding questions over approach to the last game in the book. Hopefully I can answer them in the next few days.

Tonight, I start with how to penalize a player that somehow jumps the banks of the river:

I had hoped to make the friction of the “grass” so high that the raft would come to an immediate stop. It turns out to be tricky to get the numbers just right so that the raft has no friction on the river water, but lots of friction on the grass. Also, if the player times it just right, it is possible to build enough speed to jump the entire length of the river.

So instead of playing a losing numbers game, I add an invisible lid to the game:
``````  var lid = new Physijs.ConvexMesh(
new THREE.CubeGeometry(size, size, 10),
new THREE.MeshBasicMaterial({visible:false})
);
``````
With that, a player that happens to bounce out gets forced right back down.

The other thing that I would like to do tonight it to add ramps to the river so that players can do sweet jumps. I am using Physijs height fields for the ground depression that forms the river. While I am building that, I collection the middle points of the river:
``````  var shape = new THREE.PlaneGeometry(size, size, faces, faces);
var cover = Physijs.createMaterial(new THREE.MeshPhongMaterial(), 1, 0.1);

// Doing vertices here, which is faces+1
var row_size = faces+1;
var middle_river_points = [];
for (var i=0; i<row_size; i++) {
var center = Math.sin(4*Math.PI*i/row_size);
center = center * 0.05 * faces;
center = Math.floor(center + faces/2);
middle_river_points.push(shape.vertices[i*row_size + center]);
// Make depression here...
}``````
With that, I can copy the position of arbitrary points along the river to place ramps:
``````  var ramp = new Physijs.ConvexMesh(
new THREE.CubeGeometry(50, 100, 300),
new THREE.MeshBasicMaterial({color: 0xbb0000})
);
ramp.rotation.x = Math.PI/4;
ramp.position.copy(middle_river_points[10]);