Skip to content

Commit

Permalink
Added buoyancy on Physics and loc() on body
Browse files Browse the repository at this point in the history
  • Loading branch information
danzen committed Apr 4, 2024
1 parent d38de22 commit 2f8786d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declare namespace zim {
break(joint:Box2D.b2Joint):void
attach(control:DisplayObject, obj:DisplayObject):any
unattach(id:any):void
buoyancy(height?:number, denisity?:number, linear?:number, angular?:number):any
debug():this
updateDebug():this
removeDebug():this
Expand Down
59 changes: 55 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ zim.Physics = function(gravity, borders, scroll, frame) {

var that = this;

if (zon) zog("ZIM PHYSICS");

if (zot(frame)) {
if (WW.zdf) frame = WW.zdf;
else if (WW.zimDefaultFrame) frame = WW.zimDefaultFrame;
Expand Down Expand Up @@ -225,6 +223,15 @@ zim.Physics = function(gravity, borders, scroll, frame) {
body.SetAngle(rotation*Math.PI/180);
}
});
body.loc = function(x,y) {
if (body.zimObj) {
x = x==null ? body.zimObj.x : x;
y = y==null ? body.zimObj.y : y;
}
body.SetPosition(new b2Vec2(x/scale, y/scale));
if (body.zimObj) body.zimObj.force(.01);
return body;
}
}

Object.defineProperty(this, 'gravity', {
Expand Down Expand Up @@ -500,6 +507,49 @@ zim.Physics = function(gravity, borders, scroll, frame) {
}
}

this.buoyancy = function(height, denisity, linear, angular) {

if (zot(height)) height=that.frame.stage.height/2;
if (zot(denisity)) denisity = 3;
if (zot(linear)) linear = 4;
if (zot(angular)) angular = 4;

var bc = new b2BuoyancyController();
bc.normal.Set(0,-1);
bc.offset = -(H-height)/that.scale;
bc.density = denisity;
bc.linearDrag = linear;
bc.angularDrag = angular;

that.world.AddController(bc);
bc.add = function(obj) {
if (!Array.isArray(obj)) obj = [obj]
zim.loop(obj, function(o) {
if (o.body) o = o.body;
bc.AddBody(o);
});
return bc;
}
bc.remove = function(obj) {
if (!Array.isArray(obj)) obj = [obj]
zim.loop(obj, function(o) {
if (o.body) o = o.body;
bc.RemoveBody(o);
});
return bc;
}
bc.clear = function() {
bc.Clear();
return bc;
}
bc.dispose = function() {
bc.Clear();
that.world.RemoveController(bc);
bc = null;
}
return bc;
}

// Drag wraps the demo example mouse code
var drag;
this.dragList = [];
Expand Down Expand Up @@ -618,8 +668,8 @@ zim.Physics = function(gravity, borders, scroll, frame) {
function getBodyCB(fixture) {
if(fixture.GetBody().GetType() != b2Body.b2_staticBody) {
if(fixture.GetShape().TestPoint(fixture.GetBody().GetTransform(), mousePVec)) {
selectedBody = fixture.GetBody();
return false;
selectedBody = fixture.GetBody();
return false;
}
}
return true;
Expand Down Expand Up @@ -758,6 +808,7 @@ zim.Physics = function(gravity, borders, scroll, frame) {
mappings.remove(box2DBody);
}


this.borders = function(rect) {
if (zot(rect.x) || zot(rect.y) || zot(rect.width) || zot(rect.height)) return;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zimjs/physics",
"version": "2.2.6",
"version": "2.2.7",
"description": "Physics with Box2D for ZIM JavaScript Canvas Framework",
"main": "index.js",
"types": "index.d.ts",
Expand Down

0 comments on commit 2f8786d

Please sign in to comment.