Physics: the rest of box2d

  • Hello

    Is there any chance to share the code (for the physics behavior) for the Prismatic joints like you did with the Kinematic Bodies?

    Thanks in advance.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • - Sorry for the slow response, Ive been at my grandparents house for the last week and now I am catching up with work. When I get a chance I'll dig into that. I ended up abandoning the project in favor of writing my own physics system. If you don't hear back from me in a day or two, give me a shout. I don't mean to, but I'll probably get sidetracked and forget without a reminder at some point. If I recall though, the prismatic joint code was pretty easy (I think). If I can't get the code tonight, I probably can't tomorrow, so it may be this weekend before I can get back to you!

    Cheers,

  • Hey ruskul thanx for the reply

    I ll be waiting for your news.

    Cheers.

  • - Thanks for the reply, it was a good reminder. I took a moment to look into box2d and I think contruct 2 overwrote my physics behavior at some point in its update. I had it saved as vanilla physics, which probably wasn't very smart. This is no problem though, even if I can't recover the file in dropbox or something I can refigure it out. It just take a bit longer than it should of. I'll be back shortly with an update.

  • Okay, I found the files I needed. I had them saved in a weird spot. If I had lost all that work I would have deserved an award for worst file management ever.

    If you have any trouble understanding my directions, let me know.

    First, make sure you backup the physics behavior. You can edit it directly if you want but as noted above this presents certain risks. Either way you should make a backup of both the vanilla and edited versions.

    If I recall, the prismatic joint can act wonky if you don't understand how it works so if you are not familiar with it you can check out box2d documentation on it. I kept the names of the parameters the same, so it should make sense.

    In the edittime file you will want to add the following action beneath the others:

    --------------------------------------------------------------------------------

    //Limited Prismatic joint

    AddAnyTypeParam("This image point", "Name or number of image point on this object to connect object to. Use 0 for the center of gravity and -1 for the object origin.");

    AddObjectParam("Object", "The object to attach.");

    AddNumberParam("Lower translation", "The lower limit of translation allowed, in pixels.");

    AddNumberParam("Upper translation", "The upper limit of translation allowed, in pixels.");

    AddNumberParam("Angle", "The angle to which translation will be constrained to, in degrees");

    AddAction(28, af_none, "Create limited Prismatic joint", "Joints", "Create {my} limited Prismatic joint at image point {0} to {1}, limited from {2} to {3} pixels, at {4} degrees", "Connect another object to a point on this object and limit the range of movement along a particular Axis.", "CreateLimitedPrismaticJoint");

    -----------------------------------------------------------------------------------------------------------------------------------------------------

    In the runtime file, things are a little less straight forward than kinematics. you need to add the corresponding action: (make sure you add it in the right area, just to keep things tidy. Just scroll down till you find the other Acts.prototype.something.

    ----------------------------------------------------------------------------------------------------------------------------------------------------

    // Added by Ruskul

    // type 3, takes 5 params

    Acts.prototype.CreateLimitedPrismaticJoint = function (imgpt, obj, lower, upper, angle)

    {

    if (!obj || !this.enabled)

    return;

    var otherinst = obj.getFirstPicked(this.inst);

    if (!otherinst || otherinst == this.inst)

    return;

    if (!otherinst.extra.box2dbody)

    return; // no physics behavior on other object

    this.myCreatedJoints.push({type: 3, params:

    });

    this.doCreateLimitedPrismaticJoint(imgpt, otherinst.uid, lower, upper, angle);

    };

    behinstProto.doCreateLimitedPrismaticJoint = function (imgpt, otherinstuid, lower, upper, angle)

    {

    if (!this.enabled)

    return;

    var otherinst = this.runtime.getObjectByUID(otherinstuid);

    if (!otherinst || otherinst == this.inst || !otherinst.extra.box2dbody)

    return;

    otherinst.extra.box2dbody.c2userdata.joiningMe.add(this.inst);

    var myx = this.getInstImgPointX(imgpt);

    var myy = this.getInstImgPointY(imgpt);

    var jointDef = new b2PrismaticJointDef();

    jointDef.Initialize(this.body, otherinst.extra.box2dbody, getTempVec2a(myx * worldScale, myy * worldScale), getTempVec2b(1,0));

    jointDef.set_lowerTranslation(lower * worldScale);

    jointDef.set_upperTranslation(upper * worldScale);

    //if (lower <= 0 & upper >= 0) // make sure upper and lower don't exceed 0

    jointDef.set_enableLimit(true);

    this.myJoints.push(this.world.CreateJoint(jointDef));

    };

    // /End added by ruskul

    ----------------------------------------------------------------------------------------------------------------------------------------------------

    Next , in the following function "behinstProto.recreateMyJoints = function()" add an additional case:

    -----------------------------------------------------------------------------------------------------------------------------------------------------

    // added for ruskuls additions

    case 3:

    this.doCreateLimitedPrismaticJoint(j.params[0], j.params[1], j.params[2], j.params[3], j.params[4]);

    break;

    -------------------------------------------------------------------------------------------------------------------------------------------------------

    This should be in the switch statement right above default:.

    Now, I didn't provide any expressions or the like for determining if this joint exists, but setting those up shouldn't be to hard. I never needed them so I didn't add them but if you find you do and need some help, let me know.

    Also, there may be bugs. I'm not sure because I never use save game, but I imagine if you try to save the game, this might break. I never use the giant save everything feature in construct and only ever save specific variables to local storage so, again, this hasn't affected me. If this makes a bug for you, I'm pretty sure the fix is easy, and I can probably help you out there if you need it.

    lastly, I named it limited prismatic joint because it goes hand in hand with the limited revolute joint. If there was no limit it could move along the chosen axis indefinitely. The limits simply specify how far it can go in a particular direction before stopping.

    Hopefully this runs for you! Let me know if you get it working!

    Cheers

    -Ruskul

  • Colludium you may find the above post showing how to enable prismatic joints useful. You might find a use for it that you can't live without

  • Colludium - One more thing, make sure you back up any project game file before trying this stuff out. If you add this and then take it out later, or be dumb like me you can find nefarious and subtle ways to corrupt a save file. This is true of adding and changing any behavior in a project. Mostly problems exist when you remove an action or property that a save file references.... and let me tell you, editing the construct 2 json file is not fun but is the only way to fix it if you have no backup.

  • ruskul - this is awesome - thank you! I managed without hacking the standard plugin for umbra, but it was a massive pain not having kinematic bodies (for crying out loud!) and access to raycasting etc. I'm going to make these changes to a copied branch of the standard physics plugin, and I will then change the name so there can be no confusion - I also have a rather haphazard filing process for my stuff!!

  • Hey ruskul thanks for the reply.

    I get this error when i try to run a project with two physics bodies with prismatic joint attached .

    Uncaught ReferenceError:b2prismaticJointDef is not defined

    I follow exactly your instructions . Is there anyway i can fix this?

    Cheers

    Never Mind i fixed it.

    Just in case anyone is interest . in the runtime .js

    at line with

    // Import Box2D names

    var b2BodyDef = Box2D.b2BodyDef,

    b2Body = Box2D.b2Body,

    b2FixtureDef = Box2D.b2FixtureDef,

    b2Fixture = Box2D.b2Fixture,

    b2World = Box2D.b2World,

    b2PolygonShape = Box2D.b2PolygonShape,

    b2CircleShape = Box2D.b2CircleShape,

    b2DistanceJointDef = Box2D.b2DistanceJointDef,

    b2PrismaticJointDef = Box2D.b2PrismaticJointDef,

    b2RevoluteJointDef = Box2D.b2RevoluteJointDef;

    and you are ready to go.

    Again ruskul realy thanks for that !

  • - sorry about that, glad you figured it out. I typically comment all the things I change so I can find them again but I must have missed that!

  • ruskul No worries mate.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)