SHARE
TWEET

AmmoUtils.js

a guest Jul 7th, 2019 7 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Ammo.Utils = {
  3.  
  4.   // -----------------------------------------------------------------------------
  5.   b2three: function( trans, mat ) {
  6.  
  7.     var basis = trans.getBasis();
  8.     var origin = trans.getOrigin();
  9.     var m = mat.elements;
  10.     var v = basis.getRow(0);
  11.     m[0] = v.x(); m[4+0] = v.y(); m[8+0] = v.z(); m[12+0] = origin.x();
  12.     v = basis.getRow(1);
  13.     m[1] = v.x(); m[4+1] = v.y(); m[8+1] = v.z(); m[12+1] = origin.y();
  14.     v = basis.getRow(2);
  15.     m[2] = v.x(); m[4+2] = v.y(); m[8+2] = v.z(); m[12+2] = origin.z();
  16.     m[3] = 0.0; m[4+3] = 0.0; m[8+3] = 0.0; m[12+3] = 1.0;
  17.  
  18.   },
  19.  
  20.   // -----------------------------------------------------------------------------
  21.   createAmmoVector3FromThree: function( threeVector ) {
  22.  
  23.     return new Ammo.btVector3( threeVector.x, threeVector.y, threeVector.z );
  24.  
  25.   },
  26.  
  27.   // -----------------------------------------------------------------------------
  28.   createWorld: function( gravity) {
  29.  
  30.     gravity = ( gravity != undefined ) ? gravity: -10;
  31.  
  32.     var collision_config = new Ammo.btDefaultCollisionConfiguration();
  33.     var dispatcher = new Ammo.btCollisionDispatcher( collision_config );
  34.     var overlappingPairCache =  new Ammo.btDbvtBroadphase();
  35.     var solver = new Ammo.btSequentialImpulseConstraintSolver();
  36.     var dynamicsWorld = new Ammo.btDiscreteDynamicsWorld( dispatcher, overlappingPairCache, solver, collision_config );
  37.     dynamicsWorld.setGravity(new Ammo.btVector3(0, gravity, 0));
  38.     return dynamicsWorld;
  39.  
  40.   },
  41.  
  42.   // -----------------------------------------------------------------------------
  43.   createStaticBox: function( xdim, ydim, zdim, x, y, z ) {
  44.  
  45.     var shape = new Ammo.btBoxShape( new Ammo.btVector3( xdim / 2, ydim / 2, zdim / 2 ) );
  46.     var transform = new Ammo.btTransform();
  47.     transform.setIdentity();
  48.     transform.setOrigin( new Ammo.btVector3( x, y, z ) );
  49.     var localInertia = new Ammo.btVector3( 0, 0, 0 );
  50.     var motionState = new Ammo.btDefaultMotionState( transform );
  51.     var cInfo = new Ammo.btRigidBodyConstructionInfo( 0, motionState, shape, localInertia);
  52.     var body = new Ammo.btRigidBody(cInfo);
  53.  
  54.     return body;
  55.  
  56.   },
  57.  
  58.   // -----------------------------------------------------------------------------
  59.   createStaticCylinder: function( xdim, ydim, zdim, x, y, z ) {
  60.  
  61.     var shape = new Ammo.btCylinderShape( new Ammo.btVector3( xdim / 2, ydim, zdim / 2 ) );
  62.     var transform = new Ammo.btTransform();
  63.     transform.setIdentity();
  64.     transform.setOrigin( new Ammo.btVector3( x, y, z ) );
  65.     var localInertia = new Ammo.btVector3( 0, 0, 0 );
  66.     var motionState = new Ammo.btDefaultMotionState( transform );
  67.     var cInfo = new Ammo.btRigidBodyConstructionInfo( 0, motionState, shape, localInertia);
  68.     var body = new Ammo.btRigidBody(cInfo);
  69.  
  70.     return body;
  71.  
  72.   },
  73.  
  74.   // -----------------------------------------------------------------------------
  75.   createStaticConvexHull: function( points, x, y, z, yaw, pitch, roll ) {
  76.  
  77.     yaw = ( yaw != undefined ) ? yaw: 0;
  78.     pitch = ( pitch != undefined ) ? pitch: 0;
  79.     roll = ( roll != undefined ) ? roll: 0;
  80.  
  81.     var shape = new Ammo.btConvexHullShape();
  82.  
  83.     for ( var i = 0; i < points.length; i+= 3 ) {
  84.  
  85.       var point = new Ammo.btVector3( points[ i ], points[ i + 1 ], points[ i + 2 ] );
  86.       shape.addPoint( point );
  87.     }
  88.  
  89.     var rotation = new Ammo.btQuaternion( yaw, pitch, roll );
  90.  
  91.     var transform = new Ammo.btTransform();
  92.     transform.setRotation( rotation );
  93.     transform.setOrigin( new Ammo.btVector3( x, y, z ) );
  94.     var localInertia = new Ammo.btVector3( 0, 0, 0 );
  95.     var motionState = new Ammo.btDefaultMotionState( transform );
  96.     var cInfo = new Ammo.btRigidBodyConstructionInfo( 0, motionState, shape, localInertia);
  97.     var body = new Ammo.btRigidBody(cInfo);
  98.  
  99.     return body;
  100.  
  101.   },
  102.  
  103.   // -----------------------------------------------------------------------------
  104.   createDynamicSphere: function( radius, x, y, z ) {
  105.  
  106.     var shape = new Ammo.btSphereShape( radius );
  107.     var transform = new Ammo.btTransform();
  108.     var mass = 1;
  109.     transform.setIdentity();
  110.     transform.setOrigin( new Ammo.btVector3( x, y, z ) );
  111.     var localInertia = new Ammo.btVector3( 0, 0, 0 );
  112.     shape.calculateLocalInertia( mass, localInertia );
  113.     var motionState = new Ammo.btDefaultMotionState( transform );
  114.     var cInfo = new Ammo.btRigidBodyConstructionInfo( mass, motionState, shape, localInertia);
  115.     var body = new Ammo.btRigidBody( cInfo );
  116.     body.setDamping( 0.25, 0.25 );
  117.     return body;
  118.  
  119.   },
  120.  
  121.   // -----------------------------------------------------------------------------
  122.   createHeightfield: function( geometry, widthSegments, heightSegments ) {
  123.  
  124.     geometry.computeBoundingBox();
  125.  
  126.     var ptr = Ammo.allocate(4 * widthSegments * heightSegments, "float", Ammo.ALLOC_NORMAL);
  127.     var bounds = geometry.boundingBox;
  128.  
  129.     var points = [];
  130.  
  131.     var gridX = widthSegments + 1;
  132.     var gridZ = heightSegments + 1;
  133.     var vertCount = gridX * gridZ - 1;
  134.     var absMaxHeight = Math.max( geometry.boundingBox.max.y, Math.abs( geometry.boundingBox.min.y ) );
  135.  
  136.     for ( var iz = 0; iz < gridZ; iz ++ ) {
  137.  
  138.         for ( var ix = 0; ix < gridX; ix ++ ) {
  139.  
  140.             var vertIndex = ( vertCount - iz * gridX ) - ix;
  141.             Ammo.setValue( ptr + vertIndex, geometry.vertices[ vertIndex ].y, 'float' );
  142.  
  143.         }
  144.     };
  145.  
  146.     // var a, b;
  147.     //   for ( var i = 0; i < geometry.vertices.length; i++ ) {
  148.  
  149.     //     a = i % gridX;
  150.     //     b = Math.round( ( i / gridX ) - ( (i % gridX) / gridX ) );
  151.     //     points[i] = geometry.vertices[ a + ( ( gridZ - b - 1 ) * gridZ ) ].y;
  152.  
  153.     //   }
  154.  
  155.     for (var f = 0; f < points.length; f++) {
  156.       Ammo.setValue(ptr + f,  points[f]  , 'float');
  157.     }
  158.  
  159.     var shape = new Ammo.btHeightfieldTerrainShape(
  160.       gridX,
  161.       gridZ,
  162.       ptr,
  163.       1, // has no effect?
  164.       -absMaxHeight,
  165.       absMaxHeight,
  166.       1,
  167.       0,
  168.       true
  169.     );
  170.  
  171.     var xsize = geometry.boundingBox.max.x - geometry.boundingBox.min.x;
  172.     var zsize = geometry.boundingBox.max.z - geometry.boundingBox.min.z;
  173.  
  174.     var scaling = new Ammo.btVector3( 0, 0, 0 );
  175.  
  176.     shape.setLocalScaling( new Ammo.btVector3( xsize / widthSegments, 1, zsize / heightSegments ) );
  177.  
  178.     var transform = new Ammo.btTransform();
  179.     transform.setIdentity();
  180.     transform.setOrigin( new Ammo.btVector3( 0, 0, 0) );
  181.     var localInertia = new Ammo.btVector3( 0, 0, 0 );
  182.     var motionState = new Ammo.btDefaultMotionState( transform );
  183.     var cInfo = new Ammo.btRigidBodyConstructionInfo(0, motionState, shape, localInertia);
  184.     var body = new Ammo.btRigidBody(cInfo);
  185.  
  186.     return body;
  187.  
  188.   },
  189.  
  190.   // -----------------------------------------------------------------------------
  191.   body2world: function(body,v3B) {
  192.  
  193.     var trans = body.getWorldTransform();
  194.     var result = trans.op_mul(v3B);
  195.     return result;
  196.  
  197.   }
  198.  
  199. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top