API: Physics 1.0

3D Worlds with physics. Three.js release 80. Physijs 2015. ammo.js 2015. jQuery 1.12.4.


This API has the following features:

  • Based on Three.js release 80. (Three.js continues to develop. But this API stays at this version so runs will never break.)
  • 3D and 2D graphics.
  • Built-in mouse (and touch screen) control of camera. Scroll and drag the mouse (or touch pinch and touch drag) to see camera control in both 3D and 2D Worlds.
  • Physics support provided by the Physijs library.
  • This uses the Physijs 2015 commit.
  • The previous uses a 2015 build of ammo.js.

Global variables

This API defines the following global variables:
 

 ABWorld;       // instance of class ABWorldClass - basic functionality for this API
 ABHandler;     // instance of class ABHandlerClass - mouse and touch handling for this API
 
 Physijs;          // from Physijs library 


The "ABWorld" object

The "ABWorld" object has the following public data, shown with default values where possible:

 
 
   ABWorld.camera;       // Three.js camera: THREE.PerspectiveCamera                 
   ABWorld.renderer;     // Three.js renderer: THREE.WebGLRenderer

   ABWorld.scene;        
		// This is a Physijs scene: Physijs.Scene
		// This is NOT a normal Three.js scene: THREE.Scene 
	
	
 // draw camera control buttons (Track/Move/Normal) or not 
 
   ABWorld.drawCameraControls = true; 	 
		
		
 // change these points to control camera movement:
  
   ABWorld.follow;               // THREE.Vector3          
   ABWorld.lookat;               // THREE.Vector3        

	  // when camera is in "Track" mode, it will look at ABWorld.follow
	  // when camera is in "Move" mode, it will position at ABWorld.follow,
	  //   and look at ABWorld.lookat
	 


The "ABWorld" object has the following public methods:

 
	
// start a 3D or 2D scene with camera on the canvas:		

   ABWorld.init3d ( startRadius, maxRadius, color );   
   ABWorld.init2d ( startRadius, maxRadius, color );  

     // startRadius = start position of camera  
     // maxRadius = camera frustum far plane  
  
 
// change the mode of the camera to Track/Move With/Normal
 
   ABWorld.cameraTrack(); 
   ABWorld.cameraMove();
   ABWorld.cameraFixed();

   
// Useful functions for finding size of loaded 3D objects.
// Size of object along each dimension.

   ABWorld.objectXsize ( theobject );
   ABWorld.objectYsize ( theobject );
   ABWorld.objectZsize ( theobject );

   
// Useful "ray casting" function for mouse/touch control of objects.
// Returns if click/tap on that x,y screen position hits object in 3D World.

   ABWorld.hitsObject ( x, y, theobject );		 // boolean 

See Three.js documentation and Physijs documentation.

The "ABHandler" object

The "ABHandler" object implements mouse and touch event handling. The default is that mouse and touch control the camera, but this can be overridden.

The "ABHandler" object has the following public data. Default values are shown. These can be changed.

 
   
// multiply mouse scroll by some factor to increase/decrease how much camera zooms:

   ABHandler.SCROLLFACTOR = 1;          	 

// multiply touch pinch by some factor to increase/decrease how much camera zooms:

   ABHandler.PINCHFACTOR  = 1;           	 	
	
// for 2D worlds, multiply mouse (or touch) drag by some factor to increase/decrease how much camera moves:
 
   ABHandler.DRAGFACTOR   = 1;            	 

   
// maximum position of camera:

   ABHandler.MAXCAMERAPOS;    // given some sensible default depending on World   

   
// does a "ground" exist at altitude zero (that camera should not zoom below):

   ABHandler.GROUNDZERO = false;
	


The "ABHandler" object defines the following functions for mouse and touch events to move the camera by default. To override this, and do your own mouse and touch event handling, simply redefine these functions:

 

  ABHandler.initMouseDrag = function ( x, y )  { your code };    
    // start drag now, at screen position x,y   

  ABHandler.mouseDrag     = function ( x, y )  { your code };   
    // drag went to new screen position x, y  
 
  ABHandler.mouseZoom     = function ( delta ) { your code };
    // zoom in or out by some amount 
    // the amount is set up for camera zoom 
    // for other usage of zoom, maybe just ignore delta, or only look at its sign  

	
  ABHandler.initTouchDrag = function ( x, y )  { your code };
  ABHandler.touchDrag     = function ( x, y )  { your code };
  ABHandler.touchZoom     = function ( delta ) { your code };
    

Optional: Customising scene, camera, renderer

The functions init3d and init2d create default forms of scene, camera and renderer. If you want a customised form of these, define the variable before calling init3d or init2d, and then your version will be used, not the default. Example:
 
ABWorld.renderer = new THREE.WebGLRenderer ( { antialias: true } );
ABWorld.init3d ( startRadius, maxRadius, color ); 

Note if you are customising renderer: To be able to generate World image screenshots, you need:

 
ABWorld.renderer = new THREE.WebGLRenderer ( { preserveDrawingBuffer: true } );	


Templates



Examples

Examples of Worlds that use this API:


Bouncy Balls
182 runs ♦ 2 likes
Starter user
Demo of Physics API. Balls bounce under gravity and collide in low friction world. Splash screen...
Collision World
360 runs ♦ 2 likes
Starter user
Demo of Physics API. Blocks fall under gravity and collide. Can modify gravity, friction, etc. S...

The background is a program, showing the JavaScript graphics used on this site.
 
Font:

© Ancient Brain Ltd. All rights reserved.

Ancient Brain ™ is a trademark of Ancient Brain Ltd.

Beta      Bug bounty      Contact      Stats      The name      Terms and conditions