Rajawali / Rajawali

Android OpenGL ES 2.0/3.0 Engine
https://rajawali.github.io/Rajawali/
Other
2.34k stars 700 forks source link

Rotating Object, keep axis on same place #386

Closed alenko closed 11 years ago

alenko commented 11 years ago

Hello, i have problem when i use

3dObject.setRotX(getRotX()+0.01f)

Basicly, when it rotates on X axis, the axis z and y are rotating within, so if i want to rotate the X axis and Z axis, the rotations comes wrong. How to make that only object is rotating and the axis comes fixed? Basicly rotate on axis not rotate axis.

and no, i cant use RotateAnim.

Davhed commented 11 years ago

Are you loading in an obj or making geometry in the code?

alenko commented 11 years ago

i am loading OBJ.

Davhed commented 11 years ago

Is your object at 0,0,0 with no rotation prior to export? If it is off the origin or rotated at all it will throw the transform in rajawali all off because the object pivot will always be at 0,0,0

alenko commented 11 years ago

Well yes. I know that. My object is on 0. Problem is that not the object only is rotating. But all 3 axis. I am rotating on z and is fin. But it seems when rotating on z the y axis goes around too and cant rotate on y. Y doesnt stays up. So i can rotate only on 1 axis normaly.

Davhed commented 11 years ago

I'm not sure then. I animate like this all the time and so far never had a problem like you are describing.

alenko commented 11 years ago

Test it on simple plain. You will see what i want to tell. First rotate only on y. You will see how does it rotates. Then add z .

alenko commented 11 years ago

Can you test this.i am at wrok atm and cant.

obj.setRotZ(getRotZ()+0.01f);

obj.setRotY(getRotY()+0.01f);

obj.setRotX(getRotX()-0.01f);

alenko commented 11 years ago

tested not good

Let me explain more how and what i am supposed to have and do,

I have a bullet, bullet has 2 sides, front side and back, well, bullet rotates on Z axis so it rotates around when was fired up, and i want Y to rotate based where is moving. so is always rotated to front

rpicolet commented 11 years ago

I'm no expert and I'm not sure what you mean by the bullet having 2 sides, but it sounds like maybe you want the bullet to rotate around its own long axis (i.e. rifling) regardless of which direction the gun is pointed? If so, I think you may have to compute the spin-axis vector of the bullet based on where the gun is pointed (y-axis) first, and then rotateAround() that (rather than the Z-axis) to get the bullet spin aligned with the direction of travel... In other words, rotating happens on the object only, not on the x/y/z coordinate axes; rotating the bullet around Y does not also rotate the bullet's Z-axis...

If this is not helpful, please ignore. If I'm wrong, somebody please correct me; I have to solve a similar problem in my project.

alenko commented 11 years ago

bullet

As you can see i draw a pic, to see. I need rotating bullet on z Axis, and with Y should be rotated where is moving(np i can make this calculation), but problem is, let say we set Z axis on angle 90, then Y will be on X axis.

Davhed commented 11 years ago

OK I think I understand. The behavior you are seeing is correct, the way to work around this is to make your bullet the child of another "empty" BaseObject3D

Something like this:


private BaseObject3D nullObject, bullet;

....

initScene(){

nullObject = new BaseObject3D();

//load your bullet from OBJ or SER

bullet = LoadedOBJ
bullet.setMaterial();
....
nullObject.addChild(bullet);
addChild(nullObject);

}
....
onDrawFrame(){

//Spin bullet on Z
bullet.setRotZ(bullet.getRotZ()+zOffest);

//Rotate container on Y
nullObject.getRotY(nullObject.getRotY()+yOffset); 
}

This should enable you to freely spin the bullet on Z and maintain proper orientation in Y.

I encountered this with my plane and helicopter. The propeller needed to spin on one axis, but yet had to follow the yaw, pitch, and roll of the fuselage.

alenko commented 11 years ago

hihihih. thnx alot ;)

Davhed commented 11 years ago

I really wanted to make a picture, so I illustrated the concept. Sounds like you already understand, but maybe it will be helpful for someone else.

example

Davhed commented 11 years ago

You learned this already!

related to https://github.com/MasDennis/Rajawali/issues/346

alenko commented 11 years ago

on this issue looks like i didnt even saw what the real issue is. :P

alenko commented 11 years ago

ill close the issue while is solved