Author Topic: Rotating multiple objects around a single object or center  (Read 349 times)

Offline behelit

  • byte
  • *
  • Posts: 8
    • View Profile
Rotating multiple objects around a single object or center
« on: March 14, 2018, 04:47:23 am »
I'm struggling with a rotation concept.
I'm trying to rotate objects sequentially around a single point. Similar to each of the hours on a clock.
For example, I have 10 primitives and I want to loop these around a central point.
In opengl I could do something like:

Code: [Select]
for (int i = 0; i < 10; i++)
{
gl.glPushMatrix();

gl.glRotatef(((float) i / 10) * 361f, 0f, 0f, 1f);
gl.glTranslatef(0, 5f, 0);

clockBar.draw(gl, 0);
gl.glPopMatrix();
}

But I can't seem to achieve this through jpctae. From what I understand, all of the rotations/translations are performed at the end. So it isn't possible this way.

I've also tried attaching to a dummy object then rotating that but it's the same result because of the same fact.

Would using matrices solve this? Combinig the translation and rotations?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11862
    • View Profile
    • http://www.jpct.net
Re: Rotating multiple objects around a single object or center
« Reply #1 on: March 14, 2018, 09:08:59 am »
I'm not entirely sure, if I got the problem correctly. I would say that you could either just move the rotation pivot (which might be a little fiddly, because it's given in object space) or just use multiple dummy objects that are all located at that central point...and then rotate those.

Offline behelit

  • byte
  • *
  • Posts: 8
    • View Profile
Re: Rotating multiple objects around a single object or center
« Reply #2 on: March 14, 2018, 11:18:30 pm »
Let me explain a little better.
I'm trying to rotate objects around a central point (0,0,0), in the same manner as clock hours, like this:


They don't need to move dynamically so it is all done before onDrawFrame.
The primitive in the image is a Cone.

When trying to rotate with setRotationPivot, I do the following in a loop:
Code: [Select]
clockBar[i].translate(new SimpleVector(0, 5, 0));
clockBar[i].setRotationPivot(new SimpleVector(0, -5, 0));
clockBar[i].rotateZ(((float) i / 10) * 361f); // To create the sequential positions around the clock

world.addObject(clockBar[i]);

but this produces the following, which appears to rotate around itself instead of 0,0,0:



am I misunderstanding how the pivot works?

Eventually it will be something like this, which I wrote in normal openGL and I'm converting to jpct:

« Last Edit: March 15, 2018, 09:44:00 am by behelit »

Offline AeroShark333

  • long
  • ***
  • Posts: 173
    • View Profile
Re: Rotating multiple objects around a single object or center
« Reply #3 on: March 15, 2018, 12:56:54 am »
I'm not sure but I think you need to use radians for the rotate methods

Code: [Select]
// old
clockBar[i].rotateZ(((float) i / 10) * 361f);

//new
clockBar[i].rotateZ(((float) i / 10) * ((float)Math.PI)*2f);
« Last Edit: March 15, 2018, 12:58:35 am by AeroShark333 »

Offline behelit

  • byte
  • *
  • Posts: 8
    • View Profile
Re: Rotating multiple objects around a single object or center
« Reply #4 on: March 15, 2018, 03:26:19 am »
Thanks, you were right, now it rotates the correct amount but it is still only rotating on it's own axis and not the pivot


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11862
    • View Profile
    • http://www.jpct.net
Re: Rotating multiple objects around a single object or center
« Reply #5 on: March 15, 2018, 10:14:04 am »
Make sure to call build() before setting the pivot. Otherwise, it will be resetted to a calculated one.

Offline behelit

  • byte
  • *
  • Posts: 8
    • View Profile
Re: Rotating multiple objects around a single object or center
« Reply #6 on: March 15, 2018, 11:10:39 pm »
Yes, that was the problem.
Thanks for the help everyone.

« Last Edit: March 15, 2018, 11:12:24 pm by behelit »