i'm making tree in js. draw on canvas. i'm using p2.js physics engine branches' realistic moving. i'm joining branches revoluteconstraint. have 1 problem. branches jam between themselves.
all worked. want brenches' bodies absolute , can move through other brenches.
i tried set tolerance of world's solver high, or low, doesn't work.
code:
world = new p2.world({ gravity: [0, 10] }); //new branch (fromx, fromy - prev branch) var body = new p2.body({ mass: 0, angle: this._generateangle(-45, 45) }); body.position[0] = fromx-(math.sin(body.angle))*shape.height/2; body.position[1] = fromy-(-math.cos(body.angle))*shape.height/2; body.addshape(new p2.rectange(0.3, math.random()*2)); //joining prev branch , branch var rc = new p2.revoluteconstraint(prev_branch, body, { localpivota: [0, -prev_branch.shapes[0].height/2], localpivotb: [0, body.shapes[0].height/2] }); rc.setlimits(-math.pi/8, math.pi/8); world.addconstraint(rc); world.addbody(body); full code: http://dkaraush.shpp.me/tree.js
the easiest solution disable collisions in scene. done turning off collision response on bodies:
// turns off collisions body body.collisionresponse = false; if want disable collisions between branches, still enable collisions other things in scene, need use collision groups , masks.
to this, create 2 collision groups, 1 scenery shapes , 1 branch shapes. when creating shapes in scene, tell them group belongs (.collisiongroup) , group(s) can collide (.collisionmask).
example:
// create collision groups var scenery_group = 1; var branch_group = 2; // branch shape branchshape.collisiongroup = branch_group; // set group branch. branchshape.collisionmask = scenery_group; // can collide scenery. // ground shape groundshape.collisiongroup = scenery_group; // set group ground. groundshape.collisionmask = branch_group; // can collide branches. if want see p2.js feature in action, have @ ragdoll demo.
Comments
Post a Comment