From cd5c1b378b1d8dccf80afc577b043c92f4958f80 Mon Sep 17 00:00:00 2001 From: David Perit Date: Wed, 8 Aug 2012 17:47:49 -0400 Subject: [PATCH 1/7] Made body cache the service so that it can grab the dimension map from it later Made onUpdate and onComponentOwnerChanged in body set the owner's transform based on what dimension map resolver has been set to (either XY (default), XZ, or YZ Added dimensionMaps to resolver (adding dimensionMap to the options that are sent in will allow you to set what dimensions the physics service maps to in 3D) (see tank in examples for example use) Removed odd options.gravity = options.gravity || [0,0]; line of code from resolver --- src/components/body.js | 22 +++++++++++++++++++--- src/services/resolver.js | 9 ++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/components/body.js b/src/components/body.js index 1197609..c6fe240 100644 --- a/src/components/body.js +++ b/src/components/body.js @@ -14,6 +14,8 @@ define( function ( require ) { var that = this; var i; + this.service = service; + if( options.bodyDefinition) { this.box2dBody = service.world.CreateBody( options.bodyDefinition ); } else { @@ -58,8 +60,16 @@ define( function ( require ) { // TD: This will cause the transform to emit an event that we handle below. Blech! var transform = this.owner.findComponent( "Transform" ); //Note: It is currently okay to read from buffers, but writing to them will result in things breaking - transform.position = [ position2.get_x(), position2.get_y(), transform.position.buffer[2] ]; - transform.rotation.z = angle2; + if (this.service.dimensionMap === this.service.DimensionMaps.XY){ + transform.position = [ position2.get_x(), position2.get_y(), transform.position.buffer[2] ]; + transform.rotation.z = angle2; + }else if (this.service.dimensionMap === this.service.DimensionMaps.XZ){ + transform.position = [ position2.get_x(), transform.position.buffer[1], position2.get_y()]; + transform.rotation.y = angle2; + }else{ + transform.position = [transform.position.buffer[0], position2.get_y(), position2.get_x()]; + transform.rotation.x = angle2; + } } function onEntitySpaceChanged( event ) { @@ -88,7 +98,13 @@ define( function ( require ) { if( this.owner ) { var transform = this.owner.findComponent( 'Transform' ); //Note: It is currently okay to read from buffers, but writing to them will result in things breaking - this.box2dBody.SetTransform( new Box2D.b2Vec2( transform.position.buffer[0], transform.position.buffer[1] ), transform.rotation.buffer[2] ); + if (this.service.dimensionMap === this.service.DimensionMaps.XY){ + this.box2dBody.SetTransform( new Box2D.b2Vec2( transform.position.buffer[0], transform.position.buffer[1] ), transform.rotation.buffer[2] ); + }else if (this.service.dimensionMap === this.service.DimensionMaps.XZ){ + this.box2dBody.SetTransform( new Box2D.b2Vec2( transform.position.buffer[0], transform.position.buffer[2] ), transform.rotation.buffer[1] ); + }else{ + this.box2dBody.SetTransform( new Box2D.b2Vec2( transform.position.buffer[2], transform.position.buffer[1] ), transform.rotation.buffer[0] ); + } } if( this.owner === null && data.previous !== null ) { diff --git a/src/services/resolver.js b/src/services/resolver.js index 64c233d..a17ac15 100644 --- a/src/services/resolver.js +++ b/src/services/resolver.js @@ -20,9 +20,9 @@ define( function ( require ) { }; Service.call( this, scheduler, schedules ); - options.gravity = options.gravity || [0, 0]; this.gravity = new Box2D.b2Vec2(); this.world = new Box2D.b2World( this.gravity ); + this.dimensionMap = options.dimensionMap || 0; this._timeStep = 30; // time step, in milliseconds this._timeRemaining = 0; // time remaining from last frame, in milliseconds @@ -79,6 +79,12 @@ define( function ( require ) { var totalForce = new math.Vector2(); + var DimensionMaps = { + XY: 0, + XZ: 1, + YZ: 2 + }; + function resolve() { var component; @@ -124,6 +130,7 @@ define( function ( require ) { Resolver.prototype = new Service(); Resolver.prototype.constructor = Resolver; Resolver.prototype.resolve = resolve; + Resolver.prototype.DimensionMaps = DimensionMaps; return Resolver; From f7ba33edb01ba1ad7b50ea063fa80e1326981999 Mon Sep 17 00:00:00 2001 From: David Perit Date: Thu, 9 Aug 2012 16:51:09 -0400 Subject: [PATCH 2/7] Halved any dimensions passed into box-shape so that units match up exactly in gladius and box2d --- src/resources/box-shape.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/resources/box-shape.js b/src/resources/box-shape.js index cb1fbad..0c0b54c 100644 --- a/src/resources/box-shape.js +++ b/src/resources/box-shape.js @@ -6,8 +6,8 @@ if ( typeof define !== "function" ) { define( function ( require ) { require( "box2d" ); var BoxShape = function( hx, hy ) { - hx = hx || 1; - hy = hy || 1; + hx = hx/2 || 0.5; + hy = hy/2 || 0.5; var box2dPolygonShape = new Box2D.b2PolygonShape(); box2dPolygonShape._gladius = {}; box2dPolygonShape.SetAsBox( hx, hy ); From 99e5e6726278852ddb2a39384eeaf11d6d1db8e5 Mon Sep 17 00:00:00 2001 From: David Perit Date: Fri, 10 Aug 2012 17:09:51 -0400 Subject: [PATCH 3/7] Added a circle shape --- src/gladius-box2d.js | 3 ++- src/resources/circle-shape.js | 15 +++++++++++++ src/resources/circle-shape.test.js | 34 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/resources/circle-shape.js create mode 100644 src/resources/circle-shape.test.js diff --git a/src/gladius-box2d.js b/src/gladius-box2d.js index a83e3d4..2dd8fa9 100644 --- a/src/gladius-box2d.js +++ b/src/gladius-box2d.js @@ -26,7 +26,8 @@ define( function ( require ) { resources: { "BodyDefinition": require( "src/resources/body-definition" ), "FixtureDefinition": require( "src/resources/fixture-definition" ), - "BoxShape": require( "src/resources/box-shape" ) + "BoxShape": require( "src/resources/box-shape" ), + "CircleShape": require( "src/resources/circle-shape" ) } }); diff --git a/src/resources/circle-shape.js b/src/resources/circle-shape.js new file mode 100644 index 0000000..1b363ff --- /dev/null +++ b/src/resources/circle-shape.js @@ -0,0 +1,15 @@ + +if ( typeof define !== "function" ) { + var define = require( "amdefine" )( module ); +} + +define( function ( require ) { + require( "box2d" ); + var CircleShape = function( radius ) { + var box2dCircleShape = new Box2D.b2CircleShape(); + box2dCircleShape._gladius = {}; + box2dCircleShape.set_m_radius(radius); + return box2dCircleShape; + }; + return CircleShape; +}); \ No newline at end of file diff --git a/src/resources/circle-shape.test.js b/src/resources/circle-shape.test.js new file mode 100644 index 0000000..8062f5d --- /dev/null +++ b/src/resources/circle-shape.test.js @@ -0,0 +1,34 @@ +/** + * Created with JetBrains WebStorm. + * User: dperit + * Date: 12-08-10 + * Time: 4:14 PM + * To change this template use File | Settings | File Templates. + */ +if ( typeof define !== "function" ) { + var define = require( "amdefine" )( module ); +} + +define( + ["src/resources/circle-shape"], + function( CircleShape ) { + return function() { + + module( "CircleShape", { + setup: function() {}, + teardown: function() {} + }); + + test( 'default circle shape is correct', function() { + expect(0); + var shape = new CircleShape(); + }); + + test ( 'assigned circle shape is correct', function() { + expect(0); + var shape = new CircleShape(2); + }); + + }; + } +); \ No newline at end of file From ff4bca1e156346872c15ba92818571f3f3ae9c1e Mon Sep 17 00:00:00 2001 From: David Perit Date: Mon, 13 Aug 2012 15:47:00 -0400 Subject: [PATCH 4/7] Added setLinearVelocity and setAngularVelocity --- src/components/body.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/components/body.js b/src/components/body.js index c6fe240..8993cc5 100644 --- a/src/components/body.js +++ b/src/components/body.js @@ -41,12 +41,28 @@ define( function ( require ) { Body.prototype = new Component(); Body.prototype.constructor = Body; - var linearImpulse = new Box2D.b2Vec2( 0, 0 ); + var linearVector = new Box2D.b2Vec2( 0, 0 ); + + function setAngularVelocity(rotation){ + this.box2dBody.SetAngularVelocity(rotation); + } + + function setLinearVelocity(arg1, arg2) { + var argc = arguments.length; + if( 1 === argc ) { + linearVector.Set( arg1[0], arg1[1] ); + }else{ + linearVector.Set( arg1, arg2); + } + this.box2dBody.SetLinearVelocity( linearVector ); + linearVector.Set( 0, 0 ); + } + function onLinearImpulse( event ) { var impulse = event.data.impulse; - linearImpulse.Set( impulse[0], impulse[1] ); - this.box2dBody.ApplyLinearImpulse( linearImpulse, this.box2dBody.GetPosition() ); - linearImpulse.Set( 0, 0 ); + linearVector.Set( impulse[0], impulse[1] ); + this.box2dBody.ApplyLinearImpulse( linearVector, this.box2dBody.GetPosition() ); + linearVector.Set( 0, 0 ); } function onAngularImpulse( event ) { @@ -57,7 +73,7 @@ define( function ( require ) { var position2 = this.box2dBody.GetPosition(); var angle2 = this.box2dBody.GetAngle(); - // TD: This will cause the transform to emit an event that we handle below. Blech! + var transform = this.owner.findComponent( "Transform" ); //Note: It is currently okay to read from buffers, but writing to them will result in things breaking if (this.service.dimensionMap === this.service.DimensionMaps.XY){ @@ -128,6 +144,8 @@ define( function ( require ) { } var prototype = { + setAngularVelocity: setAngularVelocity, + setLinearVelocity: setLinearVelocity, onLinearImpulse: onLinearImpulse, onAngularImpulse: onAngularImpulse, onUpdate: onUpdate, From ecd212f24c8814d6fc516c3b13f10263001c5413 Mon Sep 17 00:00:00 2001 From: David Perit Date: Mon, 13 Aug 2012 16:53:51 -0400 Subject: [PATCH 5/7] Exposed the bullet option for body-definition Exposed the friction property for fixture-definition --- src/resources/body-definition.js | 2 ++ src/resources/fixture-definition.js | 1 + 2 files changed, 3 insertions(+) diff --git a/src/resources/body-definition.js b/src/resources/body-definition.js index 872c6ea..f21e959 100644 --- a/src/resources/body-definition.js +++ b/src/resources/body-definition.js @@ -9,6 +9,8 @@ define( function ( require ) { var box2dBodyDef = new Box2D.b2BodyDef(); box2dBodyDef._gladius = {}; + box2dBodyDef.set_bullet(options.hasOwnProperty( 'bullet' ) ? + options.bullet : false); box2dBodyDef.set_type( options.hasOwnProperty( 'type' ) ? options.type : Box2D.b2_dynamicBody ); box2dBodyDef.set_linearDamping( options.hasOwnProperty( 'linearDamping' ) ? diff --git a/src/resources/fixture-definition.js b/src/resources/fixture-definition.js index 4e6e895..11c03df 100644 --- a/src/resources/fixture-definition.js +++ b/src/resources/fixture-definition.js @@ -12,6 +12,7 @@ define( function ( require ) { var box2dFixtureDef = new Box2D.b2FixtureDef(); box2dFixtureDef._gladius = {}; box2dFixtureDef.set_density( options.hasOwnProperty( 'density' ) ? options.density : 1 ); + box2dFixtureDef.set_friction( options.hasOwnProperty( 'friction' ) ? options.friction : 0.2); box2dFixtureDef.set_shape( options.shape ); return box2dFixtureDef; }; From 6cab73796ba041d6e67d3a28bf62ec5fae7a304c Mon Sep 17 00:00:00 2001 From: David Perit Date: Wed, 15 Aug 2012 13:57:48 -0400 Subject: [PATCH 6/7] Exposed the filter options for fixture-definition, which allows you to set up collision groups using bit masking --- src/resources/fixture-definition.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/resources/fixture-definition.js b/src/resources/fixture-definition.js index 11c03df..a3155f8 100644 --- a/src/resources/fixture-definition.js +++ b/src/resources/fixture-definition.js @@ -13,6 +13,19 @@ define( function ( require ) { box2dFixtureDef._gladius = {}; box2dFixtureDef.set_density( options.hasOwnProperty( 'density' ) ? options.density : 1 ); box2dFixtureDef.set_friction( options.hasOwnProperty( 'friction' ) ? options.friction : 0.2); + box2dFixtureDef.set_restitution( options.hasOwnProperty( 'restitution' ) ? options.restitution : 0); + if (options.hasOwnProperty( 'filter' )){ + var filter = box2dFixtureDef.get_filter(); + if (options.filter.hasOwnProperty( 'groupIndex' )){ + filter.set_groupIndex(options.filter.groupIndex); + } + if (options.filter.hasOwnProperty( 'categoryBits' )){ + filter.set_categoryBits(options.filter.categoryBits); + } + if (options.filter.hasOwnProperty( 'maskBits' )){ + filter.set_maskBits(options.filter.maskBits); + } + } box2dFixtureDef.set_shape( options.shape ); return box2dFixtureDef; }; From 747c2c8088a04d10dca0095994d16d534ce0b7e4 Mon Sep 17 00:00:00 2001 From: David Perit Date: Fri, 24 Aug 2012 15:10:57 -0400 Subject: [PATCH 7/7] Renamed linearVector to b2Vector in body --- src/components/body.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/components/body.js b/src/components/body.js index 8993cc5..4643348 100644 --- a/src/components/body.js +++ b/src/components/body.js @@ -41,7 +41,7 @@ define( function ( require ) { Body.prototype = new Component(); Body.prototype.constructor = Body; - var linearVector = new Box2D.b2Vec2( 0, 0 ); + var b2Vector = new Box2D.b2Vec2( 0, 0 ); function setAngularVelocity(rotation){ this.box2dBody.SetAngularVelocity(rotation); @@ -50,19 +50,19 @@ define( function ( require ) { function setLinearVelocity(arg1, arg2) { var argc = arguments.length; if( 1 === argc ) { - linearVector.Set( arg1[0], arg1[1] ); + b2Vector.Set( arg1[0], arg1[1] ); }else{ - linearVector.Set( arg1, arg2); + b2Vector.Set( arg1, arg2); } - this.box2dBody.SetLinearVelocity( linearVector ); - linearVector.Set( 0, 0 ); + this.box2dBody.SetLinearVelocity( b2Vector ); + b2Vector.Set( 0, 0 ); } function onLinearImpulse( event ) { var impulse = event.data.impulse; - linearVector.Set( impulse[0], impulse[1] ); - this.box2dBody.ApplyLinearImpulse( linearVector, this.box2dBody.GetPosition() ); - linearVector.Set( 0, 0 ); + b2Vector.Set( impulse[0], impulse[1] ); + this.box2dBody.ApplyLinearImpulse( b2Vector, this.box2dBody.GetPosition() ); + b2Vector.Set( 0, 0 ); } function onAngularImpulse( event ) {