Physics: the rest of box2d

Post your work in progress addons and get feedback

Post » Sun Mar 15, 2015 12:52 am

@ruskul - thank you very much indeed!! :)
B
63
S
16
G
63
Posts: 2,171
Reputation: 39,989

Post » Mon Mar 16, 2015 11:27 pm

@Colludium - Here is the code that can be inserted into the vanilla behaviors. If you don't understand anything let me know.

To start with, locate the behavior file "Physics". You should make a copy of this folder someplace safe so if you mess anything up you can restore it without reinstalling construct.

You can use this code to modify either ams.js physics or box2dweb physics. Either way, you need to open up both the edittime.js file and the runtime file (or both runtime files if you want the changes available in both physics engines).

You need to add this line of code to the actions are in the edit time file. This creates an action that can be used in the construct 2 editor:

AddAction(28, af_none, "Set body to Kinematic", "Object Settings", "Set {my} Kinematic", "Set this object to be a Kinematic body.", "SetKinematic");

This goes in at around line 195. Do you see the first parameter in the AddAction call. Currently it is set to 28. This number has to be unique. If Ashley updates physics and you have this attached to the vanilla behavior and have to re add it, you may have to change that number (you can tell by looking at the last AddAction call and see what number it is using. In my case, this number was 27.

After that, add the following to the runtime:

Acts.prototype.SetKinematic = function (k)
{
if (!this.enabled)
return;

this.body.SetType(1);
this.body.SetAwake(true);
};

I added to where all the other Actions are (denoted by "Acts.prototype...."). You can find it easier by typing "cntr + f" to find it. The physics behavior is 13,000 lines of code, so reading through it to find where to put it can take a while. (it is at the very bottom though, right above exps.prototype.... for the expressions.

and that's it!

If you want to create your own behavior instead of modifying the existing one, you should copy the whole physics folder, rename the folder what you want the new behavior to be called and then change the top lines in the edittime file to reflect the new name. Replace the name with your new name (as you want it to appear in construct) and then replace the ID. I know it says never change it, but as you are making a new behavior, you need to change it to be something unique, else construct will think the vanilla physics and this new one are the same thing and then you will have problems :)

Let me know if it all works out!


stupid simple... Its like 5 lines of code. I never understood why this wasn't included in the behavior >.>
Image
B
26
S
9
G
2
Posts: 534
Reputation: 4,678

Post » Mon Mar 16, 2015 11:50 pm

@ruskul - I owe you one - thank you very much!!

Edit to add: I don't understand why this has not been done already!!!!!!!!!!!!!!!!!
B
63
S
16
G
63
Posts: 2,171
Reputation: 39,989

Post » Tue Mar 17, 2015 12:08 am

@Colludium - did you get it working?
Image
B
26
S
9
G
2
Posts: 534
Reputation: 4,678

Post » Tue Mar 17, 2015 2:03 am

@ruskul - yes!! That was far too easy, LOL! I was distracted by trying to create an artifact-free trailer for this. I really appreciate the time you took to figure this out - it's going to prove very very useful. Cheers!
B
63
S
16
G
63
Posts: 2,171
Reputation: 39,989

Post » Sun Nov 22, 2015 11:26 am

Hello @ruskul.
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.
B
33
S
15
G
1
Posts: 59
Reputation: 3,319

Post » Wed Nov 25, 2015 6:57 pm

@saour - 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,
Image
B
26
S
9
G
2
Posts: 534
Reputation: 4,678

Post » Thu Nov 26, 2015 6:49 pm

Hey @ruskul thanx for the reply
I ll be waiting for your news.
Cheers.
B
33
S
15
G
1
Posts: 59
Reputation: 3,319

Post » Sat Nov 28, 2015 6:30 pm

@saour - 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.
Image
B
26
S
9
G
2
Posts: 534
Reputation: 4,678

Post » Sat Nov 28, 2015 6:57 pm

@saour

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: [imgpt, otherinst.uid, lower, upper, angle]});
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
Image
B
26
S
9
G
2
Posts: 534
Reputation: 4,678

PreviousNext

Return to Work in Progress Addons

Who is online

Users browsing this forum: No registered users and 0 guests