Page 1 of 1

Moving the origin position of a body to another position

Posted: Wed May 20, 2015 5:53 pm
by danand
Hi,

It may be a total beginner question and I may overseen this basic element, but when I create several bodies in rube, I get quite often stuck by having the "grey fading circles" (origin position of a body) not near or in the center of a body, which makes selections quite tough. I usually use the workaround to select the right body in the Items list, but I am pretty sure that there is a way to re-center the position.

The attached screenshot shows a typical situation I am facing. I would like to have the the origin fading dot moved near or into the body. I tried the solution in viewtopic.php?f=6&t=401 which sounded like a similar issue, but i only moves both the fading dot and the body simultaneously.
origin center and box body
origin center and box body
rube-box-offset.png (82.79 KiB) Viewed 16090 times
Any idea how I can fix that other than removing the body and recreating it centered ?

thanks in advance,

Daniel

Re: Moving the origin position of a body to another position

Posted: Thu May 21, 2015 6:21 am
by iforce2d
Hi Daniel

What you're talking about requires two steps
1. Move the body origin (gray circle) to the "center" position
2. Move all fixtures of the body in the opposite direction to the movement done in 1.

I put "center" in quotes like that since you did not define what that means, but let's assume you have a point in mind that you want the body origin to move to. You could use a script like this:

Code: Select all

body[] bs = sb();

for (uint i = 0; i < bs.length; i++) {
   body b = bs[i];

   // Move the body to desired location
   vec2 oldPos = b.pos;
   b.setPosition( b.getWorldCenter() ); // CoM. Change as necessary

   // Move all fixtures in the reverse direction
   vec2 toMove = oldPos - b.pos;
   fixture[] fs = b.getFixtures();
   for (uint k = 0; k < fs.length; k++) {
      fixture f = fs[k];
      translate( f.getVertices(), toMove );
   }
}
I've used getWorldCenter() (the center of mass of the body) for the location to move to as an example, but you could also use cursor(), getSelectionCenter() etc as necessary.

You might also be interested in this script which does the analogous action for rotation:
viewtopic.php?f=9&t=416

Re: Moving the origin position of a body to another position

Posted: Thu May 21, 2015 4:26 pm
by danand
Thats awesome!
Thank you very much.

It helps me to get out of that situation, which is very helpful. It may be a nice feature to allow moving the body origin "freely" as it is possible with the fixtures in the future versions, but your proposed solution is fine for me.

thanks again,

Daniel

Re: Moving the origin position of a body to another position

Posted: Mon Jul 27, 2015 5:40 am
by rileyrg
Manually, Why not select the fixture (which is a component part of the body) and translate that to the body position then move the body (fixture mode f, select fixture, t, move fixture over body, enter body mode b, selecting the body then t to enter translate mode). A little late in but might help another late comer like myself.

Re: Moving the origin position of a body to another position

Posted: Mon Jul 27, 2015 6:28 am
by iforce2d
I think he wanted to keep the fixture position unchanged, which the script I gave will do with a single keypress. Your solution will also work since it is basically the manual version of what the script does, except it requires you to make the two translations precisely the opposite of each other (eg. holding ctrl key to move in discrete steps). In the line marked "change as necessary" you could use the cursor() function to have the body move to the location of the mouse cursor.

Re: Moving the origin position of a body to another position

Posted: Mon Jul 27, 2015 9:43 am
by danand
Thanks Riley,

That's also pretty helpful.

Cheers,

Daniel

Re: Moving the origin position of a body to another position

Posted: Mon Jul 27, 2015 11:52 am
by rileyrg
And of course the most important two keys are "c s" and "s c" ;) rube's UI takes some getting used to but like emacs after a while you wouldnt have it any other way :D