Rotation around center of object, not world?

Hello all!

I have been trying to rotate my objects using Quaternions. When setting the Vector if I set it to (0,0,1) etc it will rotate around the center of the world. If I enter the actual x,y,z of the object it will stay in place and rotate randomly.

I am using Quaternion q = new Quaternion().formAngleAxis(FastMath.PI/2, new Vector3f(vector);

Is there a way to rotate from the center of the object?

Thanks!

[java]spatial.rotate (quat); [/java] rotates in local space about the model origin. When u rotate it, the next rotation is about the new local axes.

<cite>@wezrule said:</cite> [java]spatial.rotate (quat); [/java] rotates in local space about the model origin. When u rotate it, the next rotation is about the new local axes.

Sorry this was late and I should have said more.

Basically my code is as such.

[java]Quaternion YAW;
Box b = new Box[p.totalDisplays];

   for(int i = 0; i &lt; p.totalDisplays; i++)
   {
       
       
    b[i] = new Box(new Vector3f((p.displayX.get(i) + p.displayWidth.get(i) /2f)
                                ,startY - (p.displayY.get(i) + p.displayHeight.get(i)/2f)
                                ,-(p.displayDepth.get(i)/2f))
                                ,(p.displayWidth.get(i)/2f), (p.displayHeight.get(i)/2f), (p.displayDepth.get(i)/2f));   
  
   
    System.out.println(p.displaySide.get(i));
    Geometry[] g = new Geometry[p.totalDisplays];
    g[i] = new Geometry("Box", b[i]);
    
    
      if(p.displaySide.get(i)!= 0)
    {
      YAW = new Quaternion().fromAngleAxis(FastMath.DEG_TO_RAD*p.displaySide.get(i), new Vector3f(0,0,1));
      g[i].rotate(YAW);// g[i[.setLocalRotation(YAW);
    }
      g[i].scale(ppi);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setColor("Color", ColorRGBA.randomColor());
    g[i].setMaterial(mat);

    rootNode.attachChild(g[i]);
   }[/java] 

I basically am setting stuff from 2D to 3D, so don’t mind the box setup.

Basically when I try to rotate, or setLocalRotation() I get it rotating around the origin of the world, not my object.

If I use the values I put in for the Vector3f in my box it will be in the correct spot; however it will rotate in all 3, when I only need to rotate it in the Z.

That’s because you are putting the actual vertexes of the box somewhere instead of leaving them around 0,0,0 and moving the box. There is no easy way to get this to work using this non-standard approach. Just leave the box centered around it’s local origin and move the geometry.

This is actually the main reason that particular constructor is deprecated. It’s almost always a mistake to use it.

1 Like
<cite>@pspeed said:</cite> That's because you are putting the actual vertexes of the box somewhere instead of leaving them around 0,0,0 and moving the box. There is no easy way to get this to work using this non-standard approach. Just leave the box centered around it's local origin and move the geometry.

This is actually the main reason that particular constructor is deprecated. It’s almost always a mistake to use it.

Was thinking of something like that being the reason, stupid but w/e will move each separately then… Thanks! Also it doesn’t say it’s deprecated http://www.jmonkeyengine.org/doc/com/jme/scene/shape/Box.html

This, however, said it was updated 4 years ago, so not too sure if there is a more updated one.

From the javadoc link at the top of the forum:
http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/shape/Box.html

1 Like
<cite>@pspeed said:</cite> From the javadoc link at the top of the forum: http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/shape/Box.html

Thanks, I don’t know why this doc doesn’t appear in the Google searches, weak… I’ll make sure to check out the correct one as I go along, thank you so much!

1 Like
<cite>@pspeed said:</cite> From the javadoc link at the top of the forum: http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/shape/Box.html

Sorry to come back to this, but is there a reason why I cannot translate my spacial?

[java]
b[i] = new Box((p.displayWidth.get(i)/2f), (p.displayHeight.get(i)/2f), (p.displayDepth.get(i)/2f));
g[i] = new Geometry(“Box”, b[i]);
g[i].setLocalTranslation(new Vector3f((p.displayX.get(i) + p.displayWidth.get(i)/2f)
,startY - (p.displayY.get(i) + p.displayHeight.get(i)/2f)
,-(p.displayDepth.get(i)/2f)));[/java]

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_node

I am able to scale fine…

I am assuming this is the correct approach? move doesn’t work either.

That is translating it. Perhaps the value you are setting is not what you think.

<cite>@pspeed said:</cite> That is translating it. Perhaps the value you are setting is not what you think.

Yeah, nothing is happening, it just stays at the origin. All of the objects just stack up together. Is Translating it the same as using the Vector3f center in Box’s constructor? zzzz why no worky :cry:

@KonradZuse said: Yeah, nothing is happening, it just stays at the origin. All of the objects just stack up together. Is Translating it the same as using the Vector3f center in Box's constructor? zzzz why no worky :'(

Make sure that the vector3f you are passing is what you expect or that you aren’t retranslating it somewhere else.

I bet if you put together a simple test case that a) it will work and b) you will spot your problem immediately. If not then we will.

1 Like
<cite>@pspeed said:</cite> Make sure that the vector3f you are passing is what you expect or that you aren't retranslating it somewhere else.

I bet if you put together a simple test case that a) it will work and b) you will spot your problem immediately. If not then we will.

So they were moving, just not by much… Was tired last night :p.

Figured it out that it had to do with my scaling first. I also had to scale the vector I was translating as well.

@KonradZuse said: So they were moving, just not by much.... Was tired last night :p.

Figured it out that it had to do with my scaling first. I also had to scale the vector I was translating as well.

If the parent was scaled you wouldn’t even have to do that. Your scene seems constructed very strangely. :slight_smile:

Edit: or do you mean the parent is scaled and you weren’t accounting for it? Still… consistent world units would probably solve a lot of your current and future issues.

<cite>@pspeed said:</cite> If the parent was scaled you wouldn't even have to do that. Your scene seems constructed very strangely. :)

Edit: or do you mean the parent is scaled and you weren’t accounting for it? Still… consistent world units would probably solve a lot of your current and future issues.

I’m just new to this whole world :p.

I did exactly what is shown above in the code, but added [java]g[i].setLocalTranslation(new Vector3f((p.displayX.get(i) + p.displayWidth.get(i)/2f) * ppi

                                ,(startY - (p.displayY.get(i) + p.displayHeight.get(i)/2f))*ppi
                                ,-(p.displayDepth.get(i)/2f)*ppi));[/java] 

If I left it before, or after the g[i]setLocalScale(ppi); without the ppi it would just move the object a little bit, then scale it. Maybe I should scale the parent, instead of the local.q

don’t take the name as an insult but maybe this helps: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

setLocalScale() should only affect the node… not it’s translation. It only affects the translation of the children.

So if your node is 0.5 units wide and you put it at 1,1 then set its scale to 0.5 the node will look like it is 0.25 wide but it will still be located at 1,1.