Skip to content

Instantly share code, notes, and snippets.

@cjcliffe
Created March 10, 2012 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjcliffe/2013755 to your computer and use it in GitHub Desktop.
Save cjcliffe/2013755 to your computer and use it in GitHub Desktop.
Debugging ammo.js heightfield
<html>
<head>
<script src="ammo.js" type='text/javascript'></script>
<script type='text/javascript'>
var upIndex = 1;
var maxHeight = 100;
var flipQuadEdges=true;
var points = [0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15];
var xdiv = 4,
zdiv = 4; // 4x4 heightfield
var scalarType = {
FLOAT: 0,
DOUBLE: 1,
INTEGER: 2,
SHORT: 3,
FIXEDPOINT88: 4,
UCHAR: 5
};
/* Floats */
var ptr = Ammo.allocate(points.length, "float", Ammo.ALLOC_NORMAL);
for (var f = 0, fMax = xdiv*zdiv; f < fMax; f++) {
// Ammo.HEAPF32[(ptr>>2)+f] = points[f]; // also works in place of next line
Ammo.setValue(ptr+(f<<2), points[f], 'float');
// Data in/out confirmed working:
console.log("Input Position["+(f<10?" ":"")+f+"]: "+Ammo.getValue(ptr+(f<<2), 'float'));
}
btShape = new Ammo.btHeightfieldTerrainShape(xdiv, zdiv, ptr, 1, -maxHeight, maxHeight, upIndex, scalarType.FLOAT, flipQuadEdges);
/* */
/* Doubles * /
var heapf64 = new Float64Array(Ammo.HEAPF32.buffer);
var ptr = Ammo.allocate(points.length, "double", Ammo.ALLOC_NORMAL);
for (f = 0, fMax = xdiv*zdiv; f < fMax; f++) {
// heapf64[(ptr>>3)+f] = points[f];
Ammo.setValue(ptr+(f<<3), points[f], 'double');
console.log("Input Position["+(f<10?" ":"")+f+"]: "+Ammo.getValue(ptr+(f<<3), 'double'));
}
btShape = new Ammo.btHeightfieldTerrainShape(xdiv, zdiv, ptr, 1, -maxHeight, maxHeight, upIndex, scalarType.DOUBLE, flipQuadEdges);
/* */
console.log("-----------------");
for (var i = 0; i < zdiv; i++) {
for (var j = 0; j < xdiv; j++) {
var idx = (i*xdiv+j);
console.log("Read position ["+(idx<10?" ":"")+idx+"] = "+btShape.getRawHeightFieldValue(j,i));
}
}
</script>
</head>
<body>
<br/>Check the console...<br/>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment