Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by newmicro

I'm trying to create an entity that has multiple bodies that react physically as one unit. So, say we're considering a human entity, I want a shape for the head, and I want to attach it to the body of the person (which could be just a square/rectangle). When either part of the person is struck (head or body), I want the entire person to react as one rigid unit (the key is that either of the two bodies could be struck, and the whole person will move).

I get that I need either a two body entity, or a single body with separate shapes, but I'm not really sure which of these methods will create what I'm trying to do.

Also, to keep the two bodies rigid (working as one unit), I understand I need to integrate a joint somehow to keep the head apart from the body. A distance joint perhaps, but assuming that is correct, the distance joint is based on multiple "bodies", so that may be the route I have to take.

Any examples I can look at? I've looked at the car demo, but it calls methods I don't see anywhere in the source code (and it's just a pastie, so not sure if it's working at all). Thank you so much!

1 decade ago by Joncom

In case it's not clear to other readers, it would seem you're talking about Box2D, since this is not really supported in ImpactJS out of the box.

1 decade ago by newmicro

I was trying to create a multi-body entity, which is problematic, because I was also using the joncom plugin, whose entities I don't think are setup to be "multiple body"-capable. (given the single "body" instance variable).

It donned on me that what I really needed was simply two bodies and a weld joint, which I could get by spawning two Box2D entities, and welding their bodies. ("their bodies" is key, you can't weld the entities :-)

The code is pretty simple:

In main.js:

init: function()  {

// bla bla bla

this.humanHead = ig.game.spawnEntity( EntityHumanHead, ( ig.system.width / 2 ) - 38 * Box2D.SCALE, ig.system.height - 90 - 76 );
this.humanTrunk = ig.game.spawnEntity( EntityHumanTrunk, ( ig.system.width / 2 ) - 30 * Box2D.SCALE, ig.system.height - 90 );

var weldJointDef = new Box2D.Dynamics.Joints.b2WeldJointDef();
weldJointDef.bodyA = this.humanHead.body;
weldJointDef.bodyB = this.humanTrunk.body;
weldJointDef.collideConnected = false;
weldJointDef.localAnchorA = new Box2D.Common.Math.b2Vec2( 0, 83 * Box2D.SCALE );
weldJointDef.localAnchorB = new Box2D.Common.Math.b2Vec2( 0, 0 );
weldJointDef.referenceAngle = 0;
this.humanWeldJoint = ig.world.CreateJoint( weldJointDef );
}

Hope this helps someone out there :-)

1 decade ago by Jerczu

Here's a little thing I wrote few years ago - clickme

Messy but should give you an idea how to deal with multi element entities.
Page 1 of 1
« first « previous next › last »