Bug in javascript loader

Report problems here (or use the built-in feedback dialog in the editor)
Post Reply
logank9
Posts: 2
Joined: Thu Nov 24, 2016 9:07 am

Bug in javascript loader

Post by logank9 » Wed Nov 30, 2016 3:51 pm

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: :)

iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Re: Bug in javascript loader

Post by iforce2d » Thu Dec 01, 2016 12:32 am

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...

logank9
Posts: 2
Joined: Thu Nov 24, 2016 9:07 am

Re: Bug in javascript loader

Post by logank9 » Fri Dec 02, 2016 1:11 am

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);
    }

iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Re: Bug in javascript loader

Post by iforce2d » Fri Dec 02, 2016 9:09 am

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!

Post Reply