Moving the origin position of a body to another position

General discussion about the R.U.B.E editor
Post Reply
danand
Posts: 3
Joined: Wed May 20, 2015 5:23 pm

Moving the origin position of a body to another position

Post 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 15974 times
Any idea how I can fix that other than removing the body and recreating it centered ?

thanks in advance,

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

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

Post 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
danand
Posts: 3
Joined: Wed May 20, 2015 5:23 pm

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

Post 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
rileyrg
Posts: 54
Joined: Sun Jul 19, 2015 11:42 am

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

Post 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.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

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

Post 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.
danand
Posts: 3
Joined: Wed May 20, 2015 5:23 pm

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

Post by danand »

Thanks Riley,

That's also pretty helpful.

Cheers,

Daniel
rileyrg
Posts: 54
Joined: Sun Jul 19, 2015 11:42 am

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

Post 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
Post Reply