Author Topic: How to get Object3D radian?  (Read 7443 times)

Offline .jayderyu

  • long
  • ***
  • Posts: 116
    • View Profile
How to get Object3D radian?
« on: January 31, 2009, 12:21:09 pm »
Pretty simple question. How do I get the Object3D rotation radian value.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to get Object3D radian?
« Reply #1 on: January 31, 2009, 12:44:28 pm »
You mean you want to extract the angles from the rotation matrix? In that case, this might help: http://www.jpct.net/forum2/index.php/topic,576.0.html

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: How to get Object3D radian?
« Reply #2 on: January 31, 2009, 12:53:39 pm »
I assume you are talking about determining what an Object3D's rotations are at a given point in time.  Generally, one would work with the rotation matrix preferably, but it should also be possible to get back some actual rotation angles (not necessarily the ones that were used to get the object into it's current orientation, since there are multiple possible ways to rotate an object to get it into any particular orientation). 

I see that Ego beat me to it :D, the method in that thread looks like what you are after.  I also recall a short discussion about this here.  Similar related topics have popped up in a few other threads as well, but I always seem to have trouble finding old threads. Here is one.

Offline C3R14L.K1L4

  • int
  • **
  • Posts: 54
    • View Profile
    • CK's place of many works
Re: How to get Object3D radian?
« Reply #3 on: January 31, 2009, 06:08:20 pm »
Unless you are like me, trying to get maximum performance. If so, we can live without some trigonometric functions (which are rather heavy on hardware, unless done using lookup tables ;)) so, why don't you store the rotation's values (and modifications) on three hashtables for instance (one for each rotation)? Hashtable's access is quite fast...

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: How to get Object3D radian?
« Reply #4 on: January 31, 2009, 10:34:54 pm »
I not sure that a hashtable would work unless you stored some kind of "order of rotations" history.  Problem is that the order you do the rotations in affects the object's orientation.  Consider the situation where you rotated pi/4 around the y-axis, then pi/2 around the x-axis, then pi/4 around the y-axis again.  The orientation would be quite different than if you had just rotated pi/2 around the y-axis then pi/2 around the x-axis.

In any case, it is probably best to just use the rotation matrix to accomplish whatever you are trying to do if possible, rather than retrieving rotation angles.

Offline C3R14L.K1L4

  • int
  • **
  • Posts: 54
    • View Profile
    • CK's place of many works
Re: How to get Object3D radian?
« Reply #5 on: February 01, 2009, 01:39:43 am »
No, I was thinking about the actual (total) rotation angles, like yaw, roll and pitch. For instance, each time I want to rotate an object around the three axis, I set the identity to zero and rotate the object to the value stored on the related hashtable.
It's just that on limited hardware, doing sins and cousines ;) to get the actual angles might drop the framerate, but now that I think, recalculating the rotation matrix may also do the same thing. (It actually depends on jPCT's implementation, CPU / GPU routines and the graphics driver).

Offline fireside

  • double
  • *****
  • Posts: 607
    • View Profile
Re: How to get Object3D radian?
« Reply #6 on: February 01, 2009, 04:44:42 am »
I was wondering about something like this for the lookat function we discussed a short time ago.  How would you break up the rotation to make a slower turn and still end up at the final destination?
click here->Fireside 7 Games<-

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to get Object3D radian?
« Reply #7 on: February 01, 2009, 09:54:14 am »
I was wondering about something like this for the lookat function we discussed a short time ago.  How would you break up the rotation to make a slower turn and still end up at the final destination?
I used to use matrix interpolation for this. The Matrix-class has an interpolate-method. As long as the matrices are not completely different, this works just fine.

Offline .jayderyu

  • long
  • ***
  • Posts: 116
    • View Profile
Re: How to get Object3D radian?
« Reply #8 on: February 03, 2009, 07:46:15 am »
thanks, i'm currently using Egon reference. It works well. Though another issue has some up.

Code: [Select]
 // create the box
  box = primitiveFactory:getBox(1.0, 1.01);
  box:translate(10, -100, 0);
  box:rotateX(math.pi / 2);
  box:rotateZ(math.pi / 1.333);
  box:setRotationMatrix(luajava.newInstance("com.threed.jpct.Matrix")); // <-- problem & fix line
  ....
The aforementioned problem&fix line above is the problem. There is a rotation and graphic problem
1. Graphic problem. With the line commented out the box looks fine along the Z camera view.(2d view only no rotate around.)
2. With the line in place the box doesn't isn't orientated right.

1fix. with the line comment out the rotation update doesn't work. I just get an endless super fast rotation.(ref 1 above)
2fix. with the line in the rotation works great.(ref 2 above)

here is the rotation update
Code: [Select]
 // update
  r = playerBody:getRotation();
  angle = _JPCTLib:deriveAngles(player:getRotationMatrix());

  //  Phys2d unfortunatly just adds the total radian making finding the difference unrealist.
  // so setting the radian of angle within 2pi range is forced.
  if(r > (math.pi * 2)) then r = r - (math.pi * 2) end;
  if(r < 0) then r = r + (math.pi * 2) end;
  playerBody:setRotation(r);

  // update the rotation
  shift = r - pAngle.z;
  if(shift ~= 0) then
    player:rotateZ(shift);
  end;

Any suggestions as to why and how to fix it would be helpful.

I also noticed that if I create the new Matrix imidiatly rotating along the Z by 1.333 for orientation doesn't work.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to get Object3D radian?
« Reply #9 on: February 05, 2009, 02:03:01 pm »
Seems to me that someone else is playing around with the box's matrix. By adding the commented line, you create a new matrix, so that the "somebody else" is working on an actually dead reference, hence to rotation and such. But i don't know enough about your code to really comment on this.

Offline .jayderyu

  • long
  • ***
  • Posts: 116
    • View Profile
Re: How to get Object3D radian?
« Reply #10 on: February 11, 2009, 01:19:48 pm »
Thanks I will look into that.