Bodies position in JSON file

General discussion about the R.U.B.E editor
dmagunov
Posts: 16
Joined: Tue Dec 10, 2013 3:40 pm
Contact:

Bodies position in JSON file

Post by dmagunov »

Hello,

How can I export original bodies position (x,y) to JSON?

At that moment I must flip canvas context to display bodies as I draw in RUBE editor, but it is not correct way for me via performance issue.

Thanks.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Bodies position in JSON file

Post by iforce2d »

I don't quite understand the question... you want to flip the physics coordinates of the whole world upside down or something like that? I think that will cause a lot of trouble, especially if you have many joints and complex fixtures.

What rendering canvas API are you using? To what degree is the performance affected by a canvas.scale(...) type operation? Usually it should barely affect the performance at all. For example, if you do more pairs of flips like this, how much slower does it get for each additional pair?

Code: Select all

//should not affect rendered result
canvas.scale(1,-1);
canvas.scale(1,-1);
dmagunov
Posts: 16
Joined: Tue Dec 10, 2013 3:40 pm
Contact:

Re: Bodies position in JSON file

Post by dmagunov »

Try to explain what I mean.

Take a look at http://awwsoft.ua/exchange/rube.jpg This is scene rendered from RUBE json file

I use easel.js as engine. I understand that I can call scale on y (-1) to flip canvas, but I think this is not good idea for performance call this function every frame and also this is not comfortable for developing. So, how can I translate RUBE body position to HTML canvas without flipping whole canvas?
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Bodies position in JSON file

Post by iforce2d »

Have you tried the sample loader that is included in the trial download? Also here: https://www.iforce2d.net/rube/loaders/javascript It runs in an HTML5 canvas, and it is rendered correctly, and the performance is fine.

Once again I would ask, what kind of performance slowdown are you observing when you use scale to flip the scene? There is a similar thread about this here - probably does not give you any new info but just in case: viewtopic.php?f=6&t=49&p=264

It should be three lines of source code to do this, and you don't need to look at the loader code any more once it has been written, so I don't understand why it's not comfortable for developing. I know it is not the answer you wanted, but flipping the canvas is the only course of action that I can recommend. If you can demonstrate that it really does have a significant performance hit, then I might need to reconsider.
dmagunov
Posts: 16
Joined: Tue Dec 10, 2013 3:40 pm
Contact:

Re: Bodies position in JSON file

Post by dmagunov »

About performance:

try to google about it. For example http://stackoverflow.com/questions/1619 ... e-browsers

> Avoid scale and rotate context because they also cause drop down in performance. If you need to scale or rotate a sprite only once, use pre-render either.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Bodies position in JSON file

Post by iforce2d »

I think he is talking about scaling which actually changes the size of an image (eg. scale(0.42,0.42)), because that requires a heavier matrix manipulation (re-sampling of texture pixels etc) than a flip. It also depends on how the browser handles these transforms too. In my experience I have noticed with some browsers that rotating an image by zero degrees will be very fast because the browser checks for zero as a special case and does nothing. I would hope that a good canvas implementation should also detect when the scale is a pure flip operation and simply invert pixel locations instead of doing a full matrix transform.

I don't want to seem unhelpful, but you have not demonstrated that there is a real problem which needs fixing. Going back to my same question again, what specific performance issues are you observing when you flip the context? I get the feeling you have not tried it at all. This is the third time I'm asking now, and you have also not commented on the sample loader example I linked to in my last post, which is a concrete demonstration of a working implementation which does exactly what I am suggesting, and is not slow. But certainly, if you can provide a sample in which the performance is significantly decreased by adding a vertical flip, I will take a look and see what can be done.
dmagunov
Posts: 16
Joined: Tue Dec 10, 2013 3:40 pm
Contact:

Re: Bodies position in JSON file

Post by dmagunov »

Thanks for answers!

I've already write a script to convert positions of bodies and fixtures to "html canvas" orientation. Only joints remains.
dmagunov
Posts: 16
Joined: Tue Dec 10, 2013 3:40 pm
Contact:

Re: Bodies position in JSON file

Post by dmagunov »

First implementation of convert coords (for now without joints) - http://argadnet.com/coordinate-transfor ... -canvas_3/
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Bodies position in JSON file

Post by iforce2d »

fyi the script function scale() was added in v1.6 which lets you flip a scene vertically, and it takes care of all joint anchors, joint limits, image positions and image angles.

You can see a video explanation here, from about 9:28
https://www.youtube.com/watch?v=NHbGtHtccx4

I should have posted a follow-up to this thread, but I guess I forgot to, sorry about that.
dmagunov
Posts: 16
Joined: Tue Dec 10, 2013 3:40 pm
Contact:

Re: Bodies position in JSON file

Post by dmagunov »

iforce2d wrote:fyi the script function scale() was added in v1.6 which lets you flip a scene vertically, and it takes care of all joint anchors, joint limits, image positions and image angles.

You can see a video explanation here, from about 9:28
https://www.youtube.com/watch?v=NHbGtHtccx4

I should have posted a follow-up to this thread, but I guess I forgot to, sorry about that.
Thanks, you saved me a lot of time!
Post Reply