/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Mick Grierson, Matthew Yee-King, Marco Gillies
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var Vec2 = Box2D.Common.Math.b2Vec2
, BodyDef = Box2D.Dynamics.b2BodyDef
, CircleDef = Box2D.Collision.Shapes.b2CircleDef
, Body = Box2D.Dynamics.b2Body
, FixtureDef = Box2D.Dynamics.b2FixtureDef
//, Fixture = Box2D.Dynamics.b2Fixture
, World = Box2D.Dynamics.b2World
//, MassData = Box2D.Collision.Shapes.b2MassData
, PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
, CircleShape = Box2D.Collision.Shapes.b2CircleShape
, DebugDraw = Box2D.Dynamics.b2DebugDraw
, AABB = Box2D.Collision.b2AABB
, DistanceJoint = Box2D.Dynamics.Joints.b2DistanceJoint
, DistanceJointDef = Box2D.Dynamics.Joints.b2DistanceJointDef
, RevoluteJoint = Box2D.Dynamics.Joints.b2RevoluteJoint
, RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef
, PrismaticJoint = Box2D.Dynamics.Joints.b2PrismaticJoint
, PrismaticJointDef = Box2D.Dynamics.Joints.b2PrismaticJointDef
, PulleyJoint = Box2D.Dynamics.Joints.b2PulleyJoint
, PulleyJointDef = Box2D.Dynamics.Joints.b2PulleyJointDef
, GearJoint = Box2D.Dynamics.Joints.b2GearJoint
, GearJointDef = Box2D.Dynamics.Joints.b2GearJointDef
;
//Body.prototype.getWorldCenter = function (){
// v = new Vec2(0, 0);
// v.set(this.GetPosition());
// return v;
//};
//Body.prototype.setPosition = function(v){
// angle = this.GetAngle();
// this.SetPositionAndAngle(v, angle);
//};
Body.prototype.getMass = Body.prototype.GetMass;
Body.prototype.getInertia = Body.prototype.GetInertia;
Body.prototype.getMassData = Body.prototype.GetMassData;
Body.prototype.setMassData = Body.prototype.SetMassData;
Body.prototype.resetMassData = Body.prototype.ResetMassData;
Body.prototype.applyImpulse = Body.prototype.ApplyImpulse;
Body.prototype.setTransform = Body.prototype.SetPositionAndAngle;
Body.prototype.getTransform = Body.prototype.GetTransform;
Body.prototype.getPosition = Body.prototype.GetPosition;
Body.prototype.setPosition = Body.prototype.SetPosition;
Body.prototype.getAngle = Body.prototype.GetAngle;
Body.prototype.setAngle = Body.prototype.SetAngle;
Body.prototype.getWorldCenter = Body.prototype.GetWorldCenter;
Body.prototype.getLocalCenter = Body.prototype.GetLocalCenter;
Body.prototype.setLinearVelocity = Body.prototype.SetLinearVelocity;
Body.prototype.getLinearVelocity = Body.prototype.GetLinearVelocity;
Body.prototype.setAngularVelocity = Body.prototype.SetAngularVelocity;
Body.prototype.getAngularVelocity = Body.prototype.GetAngularVelocity;
Body.prototype.applyForce = Body.prototype.ApplyForce;
Body.prototype.applyForceToCenter = function(force){
this.applyForce(force, this.getWorldCenter());
};
Body.prototype.applyTorque = Body.prototype.ApplyTorque;
Body.prototype.applyLinearImpulse = Body.prototype.ApplyImpulse;
Body.prototype.getWorldPoint = Body.prototype.GetWorldPoint;
Body.prototype.getWorldPointToOut = function(localPoint, out){
out = this.getWorldPoint(localPoint);
};
Body.prototype.getWorldVector = Body.prototype.GetWorldVector;
Body.prototype.getWorldVectorToOut = function(localPoint, out){
out = this.getWorldVector(localPoint);
};
Body.prototype.getWorldVectorToOutUnsafe = Body.prototype.getWorldVectorToOut;
Body.prototype.getLocalPoint = Body.prototype.GetLocalPoint;
Body.prototype.getLocalPointToOut = function(worldPoint, out){
out = this.getLocalPoint(worldPoint);
};
Body.prototype.getLocalVector = Body.prototype.GetLocalVector;
Body.prototype.getLocalVectorToOut = function(worldPoint, out){
out = this.getLocalVector(worldPoint);
};
Body.prototype.getLocalVectorToOutUnsafe = Body.prototype.getLocalVectorToOut;
Body.prototype.getLinearVelocityFromWorldPoint = Body.prototype.GetLinearVelocity;
Body.prototype.getLinearVelocityFromWorldPointToOut = function(worldPoint, out){
out = this.getLinearVelocityFromWorldPoint(worldPoint);
};
Body.prototype.getLinearVelocityFromLocalPoint = Body.prototype.GetLinearVelocity;
Body.prototype.getLinearVelocityFromLocalPointToOut = function(localPoint, out){
out = this.getLinearVelocityFromLocalPoint(localPoint);
};
Body.prototype.getLinearDamping = Body.prototype.GetLinearDamping;
Body.prototype.setLinearDamping = Body.prototype.SetLinearDamping;
Body.prototype.getAngularDamping = Body.prototype.GetAngularDamping;
Body.prototype.setAngularDamping = Body.prototype.SetAngularDamping;
Body.prototype.getType = Body.prototype.GetType;
Body.prototype.setType = Body.prototype.SetType;
Body.prototype.isBullet = Body.prototype.IsBullet;
Body.prototype.setBullet = Body.prototype.SetBullet;
Body.prototype.isSleepingAllowed = Body.prototype.IsSleepingAllowed;
Body.prototype.setSleepingAllowed = Body.prototype.SetSleepingAllowed;
Body.prototype.isAwake = Body.prototype.IsAwake;
Body.prototype.setAwake = Body.prototype.SetAwake;
Body.prototype.isActive = Body.prototype.IsActive;
Body.prototype.setActive = Body.prototype.SetActive;
Body.prototype.isFixedRotation = Body.prototype.IsFixedRotation;
Body.prototype.setFixedRotation = Body.prototype.SetFixedRotation;
Vec2.prototype.set = function(x,y){
if(y == undefined){
this.SetV(x);
}else{
this.Set(x,y);
}
};
Vec2.prototype.setZero = Vec2.prototype.SetZero;
Vec2.prototype.mul = function (a){
return new Vec2(this.x * a, this.y * a);
};
Vec2.prototype.mulLocal = function (a){
this.Multiply(a);
return this;
};
Vec2.prototype.add = function (v){
//retVal = new Vec2(0, 0);
//retVal.set(this);
retVal = this.clone();
retVal.Add(v);
return retVal;
};
Vec2.prototype.addLocal = function (x,y){
if(y == undefined){
this.Add(x);
}else{
this.Add(new Vec2(x,y));
}
return this;
};
Vec2.prototype.sub = function (v){
//retVal = new Vec2(0, 0);
//retVal.set(this);
retVal = this.clone();
retVal.Subtract(v);
return retVal;
};
Vec2.prototype.subLocal = function (x,y){
if(y == undefined){
this.Subtract(x);
}else{
this.Subtract(new Vec2(x,y));
}
return this;
};
Vec2.prototype.negate = Vec2.prototype.GetNegative;
Vec2.prototype.negateLocal = Vec2.prototype.NegativeSelf;
Vec2.prototype.normalize = Vec2.prototype.Normalize;
Vec2.prototype.length = Vec2.prototype.Length;
Vec2.prototype.lengthSquared = Vec2.prototype.LengthSquared;
Vec2.prototype.abs = function (){
retVal = new Vec2(this.x, this.y);
return retVal.Abs();
};
Vec2.prototype.absLocal = Vec2.prototype.Abs;
Vec2.prototype.isValid = Vec2.prototype.IsValid;
Vec2.prototype.clone = function (){
return new Vec2(this.x, this.y);
};
DistanceJoint.prototype.getType = DistanceJoint.prototype.GetType;
DistanceJoint.prototype.getBodyA = DistanceJoint.prototype.GetBodyA;
DistanceJoint.prototype.getBodyB = DistanceJoint.prototype.GetBodyB;
DistanceJoint.prototype.getAnchorA = DistanceJoint.prototype.GetAnchorA;
DistanceJoint.prototype.getAnchorB = DistanceJoint.prototype.GetAnchorB;
DistanceJoint.prototype.getReactionForce = DistanceJoint.prototype.GetReactionForce;
DistanceJoint.prototype.getReactionTorque = DistanceJoint.prototype.GetReactionTorque;
DistanceJoint.prototype.getNext = DistanceJoint.prototype.GetNext;
DistanceJoint.prototype.getUserData = DistanceJoint.prototype.GetUserData;
DistanceJoint.prototype.setUserData = DistanceJoint.prototype.SetUserData;
DistanceJoint.prototype.isActive = DistanceJoint.prototype.IsActive;
DistanceJoint.prototype.getLength = DistanceJoint.prototype.GetLength;
DistanceJoint.prototype.setLength = DistanceJoint.prototype.SetLength;
DistanceJoint.prototype.getFrequency = DistanceJoint.prototype.GetFrequency;
DistanceJoint.prototype.setFrequency = DistanceJoint.prototype.SetFrequency;
DistanceJoint.prototype.getDampingRatio = DistanceJoint.prototype.GetDampingRatio;
DistanceJoint.prototype.setDampingRatio = DistanceJoint.prototype.SetDampingRatio;
RevoluteJoint.prototype.getType = RevoluteJoint.prototype.GetType;
RevoluteJoint.prototype.getBodyA = RevoluteJoint.prototype.GetBodyA;
RevoluteJoint.prototype.getBodyB = RevoluteJoint.prototype.GetBodyB;
RevoluteJoint.prototype.getAnchorA = RevoluteJoint.prototype.GetAnchorA;
RevoluteJoint.prototype.getAnchorB = RevoluteJoint.prototype.GetAnchorB;
RevoluteJoint.prototype.getReactionForce = RevoluteJoint.prototype.GetReactionForce;
RevoluteJoint.prototype.getReactionTorque = RevoluteJoint.prototype.GetReactionTorque;
RevoluteJoint.prototype.getNext = RevoluteJoint.prototype.GetNext;
RevoluteJoint.prototype.getUserData = RevoluteJoint.prototype.GetUserData;
RevoluteJoint.prototype.setUserData = RevoluteJoint.prototype.SetUserData;
RevoluteJoint.prototype.isActive = RevoluteJoint.prototype.IsActive;
RevoluteJoint.prototype.getJointAngle = RevoluteJoint.prototype.GetJointAngle;
RevoluteJoint.prototype.getJointSpeed = RevoluteJoint.prototype.GetJointSpeed;
RevoluteJoint.prototype.isLimitEnabled = RevoluteJoint.prototype.IsLimitEnabled;
RevoluteJoint.prototype.enableLimit = RevoluteJoint.prototype.EnableLimit;
RevoluteJoint.prototype.getLowerLimit = RevoluteJoint.prototype.GetLowerLimit;
RevoluteJoint.prototype.getUpperLimit = RevoluteJoint.prototype.GetUpperLimit;
RevoluteJoint.prototype.setLimits = RevoluteJoint.prototype.SetLimits;
RevoluteJoint.prototype.isMotorEnabled = RevoluteJoint.prototype.IsMotorEnabled;
RevoluteJoint.prototype.enableMotor = RevoluteJoint.prototype.EnableMotor;
RevoluteJoint.prototype.getMotorTorque = RevoluteJoint.prototype.GetMotorTorque;
RevoluteJoint.prototype.getMotorSpeed = RevoluteJoint.prototype.GetMotorSpeed;
RevoluteJoint.prototype.setMotorSpeed = RevoluteJoint.prototype.SetMotorSpeed;
RevoluteJoint.prototype.setMaxMotorTorque = RevoluteJoint.prototype.setMaxMotorTorque;
RevoluteJoint.prototype.getMaxMotorTorque = RevoluteJoint.prototype.getMaxMotorTorque;
PrismaticJoint.prototype.getType = PrismaticJoint.prototype.GetType;
PrismaticJoint.prototype.getBodyA = PrismaticJoint.prototype.GetBodyA;
PrismaticJoint.prototype.getBodyB = PrismaticJoint.prototype.GetBodyB;
PrismaticJoint.prototype.getAnchorA = PrismaticJoint.prototype.GetAnchorA;
PrismaticJoint.prototype.getAnchorB = PrismaticJoint.prototype.GetAnchorB;
PrismaticJoint.prototype.getReactionForce = PrismaticJoint.prototype.GetReactionForce;
PrismaticJoint.prototype.getReactionTorque = PrismaticJoint.prototype.GetReactionTorque;
PrismaticJoint.prototype.getNext = PrismaticJoint.prototype.GetNext;
PrismaticJoint.prototype.getUserData = PrismaticJoint.prototype.GetUserData;
PrismaticJoint.prototype.setUserData = PrismaticJoint.prototype.SetUserData;
PrismaticJoint.prototype.isActive = PrismaticJoint.prototype.IsActive;
PrismaticJoint.prototype.getJointTranslation = PrismaticJoint.prototype.GetJointTranslation;
PrismaticJoint.prototype.getJointSpeed = PrismaticJoint.prototype.GetJointSpeed;
PrismaticJoint.prototype.isLimitEnabled = PrismaticJoint.prototype.IsLimitEnabled;
PrismaticJoint.prototype.enableLimit = PrismaticJoint.prototype.EnableLimit;
PrismaticJoint.prototype.getLowerLimit = PrismaticJoint.prototype.GetLowerLimit;
PrismaticJoint.prototype.getUpperLimit = PrismaticJoint.prototype.GetUpperLimit;
PrismaticJoint.prototype.setLimits = PrismaticJoint.prototype.SetLimits;
PrismaticJoint.prototype.isMotorEnabled = PrismaticJoint.prototype.IsMotorEnabled;
PrismaticJoint.prototype.enableMotor = PrismaticJoint.prototype.EnableMotor;
PrismaticJoint.prototype.getMotorForce = PrismaticJoint.prototype.GetMotorForce;
PrismaticJoint.prototype.getMotorSpeed = PrismaticJoint.prototype.GetMotorSpeed;
PrismaticJoint.prototype.setMotorSpeed = PrismaticJoint.prototype.SetMotorSpeed;
PrismaticJoint.prototype.setMaxMotorForce = PrismaticJoint.prototype.SetMaxMotorForce;
PrismaticJoint.prototype.getMaxMotorForce = PrismaticJoint.prototype.GetMaxMotorForce;
PulleyJoint.prototype.getType = PulleyJoint.prototype.GetType;
PulleyJoint.prototype.getBodyA = PulleyJoint.prototype.GetBodyA;
PulleyJoint.prototype.getBodyB = PulleyJoint.prototype.GetBodyB;
PulleyJoint.prototype.getAnchorA = PulleyJoint.prototype.GetAnchorA;
PulleyJoint.prototype.getAnchorB = PulleyJoint.prototype.GetAnchorB;
PulleyJoint.prototype.getReactionForce = PulleyJoint.prototype.GetReactionForce;
PulleyJoint.prototype.getReactionTorque = PulleyJoint.prototype.GetReactionTorque;
PulleyJoint.prototype.getNext = PulleyJoint.prototype.GetNext;
PulleyJoint.prototype.getUserData = PulleyJoint.prototype.GetUserData;
PulleyJoint.prototype.setUserData = PulleyJoint.prototype.SetUserData;
PulleyJoint.prototype.isActive = PulleyJoint.prototype.IsActive;
PulleyJoint.prototype.getLengthA = PulleyJoint.prototype.GetLengthA;
PulleyJoint.prototype.getLengthB = PulleyJoint.prototype.GetLengthB;
PulleyJoint.prototype.getCurrentLengthA = PulleyJoint.prototype.GetCurrentLengthA;
PulleyJoint.prototype.getCurrentLengthB = PulleyJoint.prototype.GetCurrentLengthB;
PulleyJoint.prototype.getLengthA = PulleyJoint.prototype.GetLengthA;
PulleyJoint.prototype.getLengthB = PulleyJoint.prototype.GetLengthB;
PulleyJoint.prototype.getLocalAnchorA = PulleyJoint.prototype.GetLocalAnchorA;
PulleyJoint.prototype.getLocalAnchorB = PulleyJoint.prototype.GetLocalAnchorB;
PulleyJoint.prototype.getGroundAnchorA = PulleyJoint.prototype.GetGroundAnchorA;
PulleyJoint.prototype.getGroundAnchorB = PulleyJoint.prototype.GetGroundAnchorB;
PulleyJoint.prototype.getLength1 = PulleyJoint.prototype.GetLength1;
PulleyJoint.prototype.getLength2 = PulleyJoint.prototype.GetLength2;
PulleyJoint.prototype.getRatio = PulleyJoint.prototype.GetRatio;
GearJoint.prototype.getType = GearJoint.prototype.GetType;
GearJoint.prototype.getBodyA = GearJoint.prototype.GetBodyA;
GearJoint.prototype.getBodyB = GearJoint.prototype.GetBodyB;
GearJoint.prototype.getAnchorA = GearJoint.prototype.GetAnchorA;
GearJoint.prototype.getAnchorB = GearJoint.prototype.GetAnchorB;
GearJoint.prototype.getReactionForce = GearJoint.prototype.GetReactionForce;
GearJoint.prototype.getReactionTorque = GearJoint.prototype.GetReactionTorque;
GearJoint.prototype.getNext = GearJoint.prototype.GetNext;
GearJoint.prototype.getUserData = GearJoint.prototype.GetUserData;
GearJoint.prototype.setUserData = GearJoint.prototype.SetUserData;
GearJoint.prototype.isActive = GearJoint.prototype.IsActive;
GearJoint.prototype.getRatio = GearJoint.prototype.GetRatio;
GearJoint.prototype.setRatio = GearJoint.prototype.SetRatio;
/*
Vec2 = function(x,y){
v = new b2Vec2(x,y);
v.mul = function (x,y){
this.Multiply(x,y);
return this;
};
v.add = function (x,y){
retVal = new Vec2(0, 0);
retVal.set(this);
retVal.Add(x,y);
return retVal;
};
v.sub = function (x,y){
retVal = new Vec2(0, 0);
retVal.set(this);
retVal.Subtract(x,y);
return retVal;
};
v.normalize = v.Normalize;
v.set = function(x,y){
if(y == undefined){
v.SetV(x);
}else{
v.Set(x,y);
}
}
return v;
};
*/
Physics = function (sketch, screenW, screenH,
gravX, gravY,
screenAABBWidth, screenAABBHeight,
borderBoxWidth, borderBoxHeight,
pixelsPerMeter){
if(gravX == undefined) gravX = 0.0;
if(gravY == undefined) {
gravY = 10.0;
}else{
gravY = -gravY;
}
if(screenAABBWidth == undefined) screenAABBWidth = 2*screenW;
if(screenAABBHeight == undefined) screenAABBHeight = 2*screenH;
if(borderBoxWidth == undefined) borderBoxWidth = screenW;
if(borderBoxHeight == undefined) borderBoxHeight = screenH;
if(pixelsPerMeter == undefined) pixelsPerMeter = 100;
var physics = {
m_sketch : sketch,
m_gravity : new Vec2(gravX, gravY),
m_density : 0.0,
m_restitution : 0.1,
m_friction : 0.9,
m_bullet : false,
m_sensor : false,
m_pixelsPerMeter : pixelsPerMeter,
m_screenHeight : screenH,
createHollowBox : function(centerX, centerY, width, height, thickness) {
//console.log("creating hollow box");
var result = new Array();
console.log(this.createRect);
result[0] = this.createRect(centerX - width*0.5 - thickness*0.5, centerY - height*0.5 - thickness*0.5,
centerX - width*0.5 + thickness*0.5, centerY + height*0.5 + thickness*0.5);
result[1] = this.createRect(centerX + width*0.5 - thickness*0.5, centerY - height*0.5 - thickness*0.5,
centerX + width*0.5 + thickness*0.5, centerY + height*0.5 + thickness*0.5);
result[2] = this.createRect(centerX - width*0.5 - thickness*0.5, centerY + height*0.5 - thickness*0.5,
centerX + width*0.5 + thickness*0.5, centerY + height*0.5 + thickness*0.5);
result[3] = this.createRect(centerX - width*0.5 - thickness*0.5, centerY - height*0.5 - thickness*0.5,
centerX + width*0.5 + thickness*0.5, centerY - height*0.5 + thickness*0.5);
//console.log("finished creating hollow box");
return result;
},
setCustomRenderingMethod : function (obj, nm){
physics.m_customRenderingMethod = nm;
},
unsetCustomRenderingMethod : function(){
delete physics.m_customRenderingMethod;
},
createRect : function(x0, y0, x1, y1) {
//console.log("creating rect");
var cxs = (x0 + x1) * 0.5;
var cys = (y0 + y1) * 0.5;
var wxs = Math.abs(x1-x0);
var wys = Math.abs(y1-y0);
var center = this.screenToWorld(cxs, cys);
var halfWidthWorld = 0.5*this.screenToWorld(wxs);
var halfHeightWorld = 0.5*this.screenToWorld(wys);
var fixDef = new FixtureDef();
this.setShapeDefProperties(fixDef);
//console.log("creating BodyDef");
var bodyDef = new BodyDef();
//console.log("setting up bodydef");
if(this.m_density < 0.00001) {
bodyDef.type = Body.b2_staticBody;
} else {
bodyDef.type = Body.b2_dynamicBody;
}
bodyDef.position.x = center.x;
bodyDef.position.y = center.y;
//console.log("creating shapedef");
fixDef.shape = new PolygonShape;
fixDef.shape.SetAsBox(halfWidthWorld, halfHeightWorld);
//console.log("creating body");
var b = this.m_world.CreateBody(bodyDef);
b.CreateFixture(fixDef);
//this.enhanceBody(b);
//console.log(b);
//console.log(b.GetPosition());
//console.log("finished creating rect");
return b;
},
/**
* Create a circle in screen coordinates
* @param x
* @param y
* @param r
* @return
*/
createCircle : function(x, y, r) {
var center = this.screenToWorld(x,y);
var rad = this.screenToWorld(r);
var fixDef = new FixtureDef();
this.setShapeDefProperties(fixDef);
var bodyDef = new BodyDef();
//console.log("density " + this.m_density)
if(this.m_density < 0.00001) {
//console.log("static body");
bodyDef.type = Body.b2_staticBody;
} else {
//console.log("dynamic body");
bodyDef.type = Body.b2_dynamicBody;
}
//console.log("radius " + rad);
bodyDef.position.x = center.x;
bodyDef.position.y = center.y;
fixDef.shape = new CircleShape(rad);
var b = this.m_world.CreateBody(bodyDef);
b.CreateFixture(fixDef);
//var cd = new CircleDef();
//cd.radius = rad;
//this.setShapeDefProperties(cd);
//var bd = new BodyDef();
//this.setBodyDefProperties(bd);
//bd.AddShape(cd);
//bd.position.Set(center.x, center.y);
//var b = this.m_world.CreateBody(bd);
//this.enhanceBody(b);
return b;
},
/**
* Create a polygon based on vertices.
*
* Polygons must be:
*
* createPolygon(x0,y0,x1,y1,x2,y2,x3,y3);* or *
* createPolygon(x0,y0,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5);* or *
* float[] xyInterleaved = {x0,y0,x1,y1,x2,y2,x3,y3,x4,y4}; * createPolygon(xyInterleaved);* are all fine. * @param vertices Any number of pairs of x,y floats, or an array of the same (screen coordinates) * @return */ createPolygon : function() { var vertices; if (arguments.length == 1 && arguments[0].length){ vertices = arguments[0]; } else { vertices = arguments; } if (vertices.length % 2 != 0) throw new IllegalArgumentException("Vertices must be given as pairs of x,y coordinates, " + "but number of passed parameters was odd."); var nVertices = vertices.length / 2; var pd = new b2PolyDef(); pd.vertexCount = nVertices; var average = new Vec2(0, 0); for (var i = 0; i < nVertices; i++) { var v = this.screenToWorld(vertices[2*i],vertices[2*i+1]); //console.log("creating vertex " + v.x + " " + v.y); pd.vertices[i].Set(v.x, v.y); average.x += v.x; average.y += v.y; } if(nVertices > 0){ average.x /= nVertices; average.y /= nVertices; } for (var i = 0; i < nVertices; i++) { pd.vertices[i].x -= average.x; pd.vertices[i].y -= average.y; } this.setShapeDefProperties(pd); var bd = new BodyDef(); this.setBodyDefProperties(bd); bd.position.Set(average.x, average.y); bd.AddShape(pd); var b = this.m_world.CreateBody(bd); //this.enhanceBody(b); return b; }, setBodyDefProperties : function(bd) { bd.isBullet = this.m_bullet; }, setShapeDefProperties : function(sd) { sd.density = this.m_density; sd.friction = this.m_friction; sd.restitution = this.m_restitution; sd.isSensor = this.m_sensor; //console.log(sd); }, /* enhanceBody : function (b){ b.getWorldCenter = function (){ v = new Vec2(0, 0); v.set(b.GetPosition()); return v; }; b.setPosition = function(v){ angle = this.GetAngle(); this.SetPositionAndAngle(v, angle); }; b.getMass = b.GetMass; b.applyImpulse = b.ApplyImpulse; }, */ /** * Create a distance (stick) joint between two bodies * that holds the specified points at a constant distance. *