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

Circle shape #16

Open
wants to merge 3 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
22 changes: 19 additions & 3 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 Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
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
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);
});

};
}
);
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