Zero-valued Vector2 JSON Export issue

General discussion about the R.U.B.E editor
Post Reply
kalium
Posts: 6
Joined: Thu Sep 26, 2013 8:25 pm

Zero-valued Vector2 JSON Export issue

Post by kalium »

So, I've been using RUBE as Box2D editor to work on the game i'm developing in C# with Farseer Physics.
And so far, it's been working great. I've made my own parser to read the JSON files using the .NET JSONDeserializer and some object template classes of my own. Thing is, when I have a Vector2 with X and Y values both set to zero, RUBE exports it as a single value instead of a Vector2 with X and Y components, which basically breaks the deserializer.

So I'm coming here to ask, is there any way of making RUBE export zero-valued Vector2's as this:

Code: Select all

"position" : 
{
	"x" : 0.0,
	"y" : 0.0
},
by default, instead of this?

Code: Select all

"position" :  0
Dunno if this is a bug, or intended. But I'd be really glad if there was a way of turning it off, or at least working around it.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Zero-valued Vector2 JSON Export issue

Post by iforce2d »

This is intended, to save file space. I am not familiar with the parser you are using, but is it not possible to check the value type of a property?
eg. as done in this snippet from the C++ loader:

Code: Select all

        if ( value[name].isInt() ) //zero vector
            vec.Set(0,0);
        else {
            vec.x = jsonToFloat("x", value[name]);
            vec.y = jsonToFloat("y", value[name]);
        }
I suppose an option could be added in the settings to turn this behavior off, but using it would result in your loader being incapable of dealing with files anything other than those you have exported yourself, which kinda defeats the purpose of having an easily exchangeable format.
I would expect any decent parser to have a way for you to find out what the type of a property is - after all the parser itself has to know this in order to parse anything at all.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Zero-valued Vector2 JSON Export issue

Post by iforce2d »

Not sure if this will help or not, but somebody posted here a while ago about a loader they had made for Farseer:
viewtopic.php?f=6&t=132

The relevant part of the code seems to be:

Code: Select all

				JObject vecValue = (JObject)value[name];
				if (null == vecValue)
					return defaultValue;
				else if (vecValue["x"] == null) // should be zero vector
					vec.X = vec.Y = 0;
				else
				{
					vec.X = jsonToFloat("x", vecValue);
					vec.Y = jsonToFloat("y", vecValue);
				}
... and it looks like he is using this parser:

Code: Select all

using Newtonsoft.Json.Linq;
kalium
Posts: 6
Joined: Thu Sep 26, 2013 8:25 pm

Re: Zero-valued Vector2 JSON Export issue

Post by kalium »

I appreciate it, but it doesn't exactly help me much, and here's why:
What I did, was create template classes for the World, Bodies, Fixtures, Shapes, Joints and CustomProperties, containing variables with the corresponding types to receive the values read from the .JSON file.
Basically, I created a class, that when serialized by a JSON serializer, would create a file with the same structure as the ones exported by RUBE, so I actually can read and import the whole .JSON file using just a few lines of code:

Code: Select all

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            StreamReader reader = new StreamReader(path);
            worldTemplate = serializer.Deserialize<WorldTemplate>(reader.ReadToEnd());
This saves a whole lot of work on handling and reading every specific value on the .json file. I've compared my code with the alternative you provided, and I've managed to do in under 350 lines of code what he does in over 2000. My alternative only runs into a problem when the serializer tries to read a single float when the template is expecting a Vector2. And I can't change the way it parses that, because I can't modify the .NET library.

So, If I can't have rube exporting the values that way, I'll have to make up a script to fix those values manually, I guess.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Zero-valued Vector2 JSON Export issue

Post by iforce2d »

I don't think it's fair to say that you managed to do in 350 lines of code what the other guy did in 2000 - let's keep in mind that he has a working loader and you are asking for the file format to be modified. All too often with "it's so easy" methods, they trade flexibility for less coding and end up limited to certain use cases.

However, I can see that a little work from me will save you a lot of time (and this issue has also come up with Java loaders using the same concept of rigid class mappings), so I will add an option to skip the shortening of zero-values in the next update. If you can let me know the OS you're using then I'll get you a preview build as soon as it's ready.

Apart from the niggling feeling that modifying the format to accommodate the shortcomings of one loader implementation is backwards, it does worry me a bit that I am opening the door for something that will need to be continually chased around after in future. For example, when new properties are added, will they confuse the loader again and require further options to be set up?

Even for the current case, I am not sure if this one change will be sufficient - is this the only property where you have trouble? Have you successfully loaded a file that has those values replaced with the full property? How about custom properties, which depend on having only some properties present, and should not have default values initialized for the properties that do not exist... for example, these are both valid custom properties:

Code: Select all

{
    "name": "timeout", 
    "float": 2.5
}
{
    "name": "breakable", 
    "bool": true
}
I don't know the details of how the class template works, but I am guessing it would not like this either...

I have been meaning to make a scene which has every possible feature in it (every fixture type, every joint type, all custom property types etc), that people writing loaders could use to check if their implementation is correct. I'll see if I can get that done soon too, maybe it will help us find anything else that needs to be handled.

About converting the file by some kind of script, on Linux (and probably Mac too), you could use this to replace the position elements with the full version:

Code: Select all

sed 's/\"position\" *: *0/\"position\":\{\"x\":0.0,\"y\":0.0\}/' inputfile > outputfile
There is a win32 port of sed that you might be able to use on Windows too. You could set this up to run automatically after the raw info file is exported by using a system hook. Search for "system hooks" in the help for details on that.
kalium
Posts: 6
Joined: Thu Sep 26, 2013 8:25 pm

Re: Zero-valued Vector2 JSON Export issue

Post by kalium »

By all means, I didn't mean to sound arrogant. As you did point out, his loader works perfectly, as far as we know, while mine still has its shortcomings. I'm just reluctant to implement his at this point, because mine already works in almost perfect integration with my project. I just don't want to change my entire implementation, which so far works great to my needs, as at this point, either rewriting it entirely in a way similar to what he has done, or learning how his loader works, implementing it and adapting it to my project both seem to be very time-consuming alternatives just to work around a minor issue.

As for the custom properties, I do use many of them, and they work just fine. I have no problem at all reading the arrays of custom properties, even when some are unitialized.

The only thing causing me problems, so far, are the Vector2's. If you think it's not worth changing that in RUBE, I understand. I'll just have to list all the variables that are Vector2 in rube and fix them manually using the script.
To be honest, I only had trouble with the AnchorA and AnchorB local values for joints, which are usually initialized to 0 when centered on the body's position, but I'm pretty sure every other Vector2 value when initialized to 0 would cause the same problem. I used position just as an example.

I have no problem in sharing the code of my loader, if you want, but It's still lacking a few things that I'm not using yet, as I've just recently started working on it. If you think it can provide some valuable insight, just let me know and I'll post the code here.

Either way, thanks a lot for taking your time to look into my issue. That script with system hook will probably do the trick.
kalium
Posts: 6
Joined: Thu Sep 26, 2013 8:25 pm

Re: Zero-valued Vector2 JSON Export issue

Post by kalium »

Oh, and about the preview version, the OS I'm using is Windows, 64bit. Sorry for the late answer, and thanks again for taking the time to look into it.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Zero-valued Vector2 JSON Export issue

Post by iforce2d »

I added an option to write non-compacted zero vectors in v1.5.1 (in the scene settings dialog, Export options tab). Let me know how it works out.
kalium
Posts: 6
Joined: Thu Sep 26, 2013 8:25 pm

Re: Zero-valued Vector2 JSON Export issue

Post by kalium »

It works perfectly now! Many thanks! :D
Post Reply