Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPM Support #213

Open
agilgur5 opened this issue Nov 10, 2014 · 2 comments
Open

NPM Support #213

agilgur5 opened this issue Nov 10, 2014 · 2 comments

Comments

@agilgur5
Copy link

I'd love to be able to use this with Browserify/NPM, and implementing an optional module system would be really useful

@escapedcat
Copy link

I saw the #217 which implements it for v0.1.x.
I saw the 2.0 branch, tried to add module support and use it with Webpack. But somehow i failed. I tried this directly on the dist file.

physi.js

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('THREE')) :
    typeof define === 'function' && define.amd ? define(['THREE'], factory) :
    global.physijs = factory(global.THREE);
}(this, function (THREE) {
...[physi.js code]...
}));

main.js

var THREE = require('three');
var physijs = require('../../node_modules/physijs/dist/physi.js');

var renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setClearColor( new THREE.Color( 0xffffff ) );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

var scene = new physijs.Scene( '../../node_modules/physijs/dist/physijs-worker.js' );

var camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 100 );
camera.position.z = 10;

var sphere_geometry = new THREE.SphereGeometry( 1, 12, 12 );
var normal_material = new THREE.MeshNormalMaterial();

var sphere = new physijs.Sphere( sphere_geometry, normal_material, { mass: 0 } );
scene.add( sphere );

var box = new physijs.Box(
  new THREE.BoxGeometry( 1, 1, 1 ), normal_material
  , { mass: 2 }
);
box.position.set( 0.13, 3, 0 );
box.physics.linear_velocity.y = -5;
scene.add( box );

sphere.addEventListener(
  'physics.newContact',
  function( event ) {
    console.log( 'collided at', event.relative_linear_velocity.length() );
  }
);

var physics_framerate = 1000 / 60;
function onStep() {
  renderer.render( scene, camera );
  setTimeout( scene.step.bind( scene, physics_framerate / 1000, undefined, onStep ), physics_framerate );
}
scene.step( physics_framerate / 1000, undefined, onStep );

This throws

THREE.Object3D.add: object not an instance of THREE.Object3D. 

pointing to

var sphere = new physijs.Sphere( sphere_geometry, normal_material, { mass: 0 } );
scene.add( sphere );

When I try it with var physijs = require('../../node_modules/physijs/dist/physi.js')(THREE);
I get __webpack_require__(...) is not a function.

No idea... this is a bit too much for me. Maybe someone knows more about this or can give me a hand.

@agilgur5
Copy link
Author

agilgur5 commented Sep 16, 2018

Well totally random and a few years later, I decided to randomly work on my fork -- https://github.com/agilgur5/physijs-webpack -- and now it's working with NPM. It works with webpack and browserify out-of-the-box, and should be easy to support other bundlers if needed.

Uses the latest version of Physijs (v1.x I guess since v2 was never completed), a slightly modified version of the ammo.js version used in the examples/ directory that works with webpack etc, and has a peerDep to Three r73.

I'll leave this open since it's not in the core fork and so anyone else searching can find it more easily.
https://github.com/agilgur5/physijs-webpack

Edit: I got ownership of the physijs-webpack NPM package, so it is legit up on NPM now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants