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

Exposed options #17

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions src/components/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -39,12 +41,28 @@ define( function ( require ) {
Body.prototype = new Component();
Body.prototype.constructor = Body;

var linearImpulse = new Box2D.b2Vec2( 0, 0 );
var b2Vector = new Box2D.b2Vec2( 0, 0 );

function setAngularVelocity(rotation){
this.box2dBody.SetAngularVelocity(rotation);
}

function setLinearVelocity(arg1, arg2) {
var argc = arguments.length;
if( 1 === argc ) {
b2Vector.Set( arg1[0], arg1[1] );
}else{
b2Vector.Set( arg1, arg2);
}
this.box2dBody.SetLinearVelocity( b2Vector );
b2Vector.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 );
b2Vector.Set( impulse[0], impulse[1] );
this.box2dBody.ApplyLinearImpulse( b2Vector, this.box2dBody.GetPosition() );
b2Vector.Set( 0, 0 );
}

function onAngularImpulse( event ) {
Expand All @@ -55,11 +73,19 @@ 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
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 ) {
Expand Down Expand Up @@ -88,7 +114,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 ) {
Expand All @@ -112,6 +144,8 @@ define( function ( require ) {
}

var prototype = {
setAngularVelocity: setAngularVelocity,
setLinearVelocity: setLinearVelocity,
onLinearImpulse: onLinearImpulse,
onAngularImpulse: onAngularImpulse,
onUpdate: onUpdate,
Expand Down
3 changes: 2 additions & 1 deletion src/gladius-box2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" )
}

});
Expand Down
2 changes: 2 additions & 0 deletions src/resources/body-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) ?
Expand Down
4 changes: 2 additions & 2 deletions src/resources/box-shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
15 changes: 15 additions & 0 deletions src/resources/circle-shape.js
Original file line number Diff line number Diff line change
@@ -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;
});
34 changes: 34 additions & 0 deletions src/resources/circle-shape.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

};
}
);
14 changes: 14 additions & 0 deletions src/resources/fixture-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ 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_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;
};
Expand Down
9 changes: 8 additions & 1 deletion src/services/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -79,6 +79,12 @@ define( function ( require ) {

var totalForce = new math.Vector2();

var DimensionMaps = {
XY: 0,
XZ: 1,
YZ: 2
};

function resolve() {
var component;

Expand Down Expand Up @@ -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;

Expand Down