Page 1 of 1

Bug in javascript loader

Posted: Wed Nov 30, 2016 3:51 pm
by logank9
I found a small bug in your javascript loader while I was digging through it.

https://www.iforce2d.net/rube/loaders/javascript/

I borrowed code from that demo player for my own project and came across an error I needed to correct for the weld joint to work properly.

It's on line 219 of https://www.iforce2d.net/rube/loaders/j ... oadrube.js

Where it loads weld joints it says:

Code: Select all

if ( jointJso.hasOwnProperty('referenceAngle') )
     jd.referenceAngle = jointJso.referenceAngle;
While all other joints have different code for loading the reference angle:

Code: Select all

if ( jointJso.hasOwnProperty('refAngle') )
     jd.referenceAngle = jointJso.refAngle;
As you can see it checks for the referenceAngle property on the weld joint but checks the refAngle property on all other joints. This causes weld joints not to load at their correct angles, and they both bodies will be kept at the same rotation rather than the same orientation as intended. Pretty sure this was not intentional because in the C++ loader the weld joint uses refAngle to load like all the other joints.

I figured this might save someone a minute or two in the future if I posted it here :D :D :lol: :)

Re: Bug in javascript loader

Posted: Thu Dec 01, 2016 12:32 am
by iforce2d
That code was a very early version, before I made the official release available. I guess I just left it there because it was working for the demos.

The most recent code is here: https://github.com/iforce2d/b2dJson/blo ... oadrube.js

Could you try with that one? Although just from a quick look, it seems like it still has the same problem...

Re: Bug in javascript loader

Posted: Fri Dec 02, 2016 1:11 am
by logank9
iforce2d wrote:That code was a very early version, before I made the official release available. I guess I just left it there because it was working for the demos.

The most recent code is here: https://github.com/iforce2d/b2dJson/blo ... oadrube.js

Could you try with that one? Although just from a quick look, it seems like it still has the same problem...
Yes I just tried, it has the same problem with the .json files I'm testing which use the 'refAngle' json property for all joints to label the reference angle. Maybe that has changed to 'referenceAngle' in another version of RUBE. I don't have the program so I don't know.

Seems you test for refAngle || referenceAngle here:

Code: Select all

    if ( jointJso.type == "revolute" ) {
        var jd = new b2RevoluteJointDef();
        loadJointCommonProperties(jd, jointJso, loadedBodies);
        applyProperties(jointJso, jd, ['refAngle', 'referenceAngle'], ['lowerLimit', 'lowerAngle'], ['upperLimit', 'upperAngle'], 'maxMotorTorque', 'motorSpeed', 'enableLimit', 'enableMotor');
        joint = world.CreateJoint(jd);
    }
But only referenceAngle here:

Code: Select all

    else if ( jointJso.type == "weld" ) {
        var jd = new b2WeldJointDef();
        loadJointCommonProperties(jd, jointJso, loadedBodies);
        applyProperties(jointJso, jd, ['referenceAngle']);
        joint = world.CreateJoint(jd);
    }

Re: Bug in javascript loader

Posted: Fri Dec 02, 2016 9:09 am
by iforce2d
Yes, you're correct. I have updated the github source. If you want to pinpoint change your file, this is how it should be (just the applyProperties line changed):

Code: Select all

    else if ( jointJso.type == "weld" ) {
        var jd = new b2WeldJointDef();
        loadJointCommonProperties(jd, jointJso, loadedBodies);
        applyProperties(jointJso, jd, [['refAngle','referenceAngle']]);
        joint = world.CreateJoint(jd);
    }
Thanks!