×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Regular Contributor
    Join Date
    Feb 2005
    Location
    Kista, Sweden
    Posts
    267

    How to spin an object with both x and z-rotation in M3G?

    I want to spin an object with both x and z-rotation in M3G. I get a good result when I implement this in OpenGL, but the combined rotation doesn't get right when I try to do this in M3G. The x rotation and the z rotation looks like they should when I only use one of them. This is achieved by adding only spinAnimationTrack or rotationAnimationTrack. The problem is that the rotation gets messed up when I add both spinAnimationTrack and rotationAnimationTrack. What am I doing wrong? This is my code:

    KeyframeSequence spinKeyframes =
    new KeyframeSequence(3, 4, KeyframeSequence.SLERP);
    spinKeyframes.setRepeatMode(KeyframeSequence.LOOP);
    spinKeyframes.setDuration(4000);

    spinKeyframes.setKeyframe(0, 0,
    getRotationQuaternion(-50.0f, new float[]{1.0f, 0.0f, 0.0f}));
    spinKeyframes.setKeyframe(1, 2000,
    getRotationQuaternion(50.0f, new float[]{1.0f, 0.0f, 0.0f}));

    spinKeyframes.setKeyframe(2, 4000,
    getRotationQuaternion(-50.0f, new float[]{1.0f, 0.0f, 0.0f}));

    AnimationTrack spinAnimationTrack =
    new AnimationTrack(spinKeyframes,
    AnimationTrack.ORIENTATION);

    AnimationController animatorS = new AnimationController();
    spinAnimationTrack.setController(animatorS);
    animatorS.setSpeed(0.4f * speed, 0);

    KeyframeSequence rotationKeyframes =
    new KeyframeSequence(2, 4, KeyframeSequence.SLERP);
    rotationKeyframes.setRepeatMode(KeyframeSequence.LOOP);
    rotationKeyframes.setDuration(4000);

    rotationKeyframes.setKeyframe(0, 0,
    getRotationQuaternion(359.0f, new float[]{0.0f, 0.0f, 1.0f}));
    rotationKeyframes.setKeyframe(1, 4000,
    getRotationQuaternion(0.0f, new float[]{0.0f, 0.0f, 1.0f}));

    AnimationTrack rotationAnimationTrack1 =
    new AnimationTrack(rotationKeyframes,
    AnimationTrack.ORIENTATION);

    rotationAnimationTrack1.setController(animatorR1);
    animatorR1.setSpeed(0.4f * speed, 0);

    mesh1.addAnimationTrack(spinAnimationTrack);
    mesh1.addAnimationTrack(rotationAnimationTrack1);

  2. #2
    Nokia Developer Moderator
    Join Date
    Dec 2011
    Posts
    174

    Re: How to spin an object with both x and z-rotation in M3G?

    Hi MobileVisuals,

    In the below link example the results to get the quaternions for key frames are calculated differently then in the example that you are using. I don't know if it's the correct way for quaternion calculations but it seems to work.

    http://read.pudn.com/downloads191/so...n3D.java__.htm

    Regards,
    tiviinik

  3. #3
    Regular Contributor
    Join Date
    Feb 2005
    Location
    Kista, Sweden
    Posts
    267

    Re: How to spin an object with both x and z-rotation in M3G?

    Thanks for the link. I replaced my setKeyframe(0, 0,getRotationQuaternion
    calls with setKeyframe setKeyframe(0, 0, getXRoation(angle) and setKeyframe setKeyframe(0, 0, getYRoation(angle)

    The problem now is that I need a method for Z rotation. The methods for x and y rotation are

    private float[] getYRoation(float angle) {

    angle = (float)Math.toRadians(angle);
    return new float[] {0, (float)Math.sin(angle), 0, (float)Math.cos(angle)};
    }

    private float[] getXRoationTop(float angle) {
    angle = (float)Math.toRadians(angle);
    return new float[] {(float)Math.sin(angle), 0, 0, (float)Math.cos(angle)};
    }

    So shouldn't the method for Z rotation be like this?

    private float[] getZRoation(float angle) {

    angle = (float)Math.toRadians(angle);
    return new float[] {0, 0,(float)Math.sin(angle), (float)Math.cos(angle)};
    }

    I tried with this, but it didn't produce a z rotation.

  4. #4
    Nokia Developer Moderator
    Join Date
    Dec 2011
    Posts
    174

    Re: How to spin an object with both x and z-rotation in M3G?

    The correct way to generate a quaternion with the rotaion axis and the angle is

    Angle = Math.toRadians(Angle)/2;
    // where the axis is normalised
    x = axis.x * Math.sin(Angle);
    y = axis.y * Math.sin(Angle);
    z = axis.z * Math.sin(Angle);
    w = Math.cos(Angle);

    When using quaternion to rotate try to use one rotation axis at a time. And if you want to change the rotation axis in the animation you have to multiply the two quaternions together to get the correct rotation. See code snippet...

    Code:
    		private void animationInitialization(Mesh mesh) {
    
    			KeyframeSequence keyframes =new KeyframeSequence(3, 4, KeyframeSequence.SLERP);
    			keyframes.setRepeatMode(KeyframeSequence.LOOP);
    			keyframes.setDuration(duration);
    
    			float axis[] =   new float[] {1.0f, 1.0f,1.0f};  
    
    			axis = normalize(axis);
    
    			keyframes.setKeyframe(0, 0,getQuaternion(0.0f, axis));
    
    			float q1[] = getQuaternion(120.0f, axis);
    
    			keyframes.setKeyframe(1, duration/2,q1);
    
    			axis =   new float[] {0.0f, 0.0f,1.0f};  
    
    			axis = normalize(axis);
    
    			float q2[] = getQuaternion(90.0f, axis);
    
    			keyframes.setKeyframe(2, duration,multiplyQuaternions(q2,q1));
    
    			AnimationTrack animationTrackX =new AnimationTrack(keyframes,AnimationTrack.ORIENTATION);
    			AnimationController animatorXZ = new AnimationController();
    			animatorXZ.setActiveInterval(0, duration);
    			animationTrackX.setController(animatorXZ);
    			mesh.addAnimationTrack(animationTrackX);
    
    		}
    		
    		private float[] multiplyQuaternions(float[] q1,float[] q2)
    		{
    			float temp[] = new float[q1.length];
    
    			temp[0] = q1[3]*q2[0] + q1[0]*q2[3] + q1[1]*q2[2] - q1[2]*q2[1]; 
    			temp[1] = q1[3]*q2[1] - q1[0]*q2[2] + q1[1]*q2[3] + q1[2]*q2[0]; 
    			temp[2] = q1[3]*q2[2] + q1[0]*q2[1] - q1[1]*q2[0] + q1[2]*q2[3]; 
    			temp[3] = q1[3]*q2[3] - q1[0]*q2[0] - q1[1]*q2[1] - q1[2]*q2[2]; 
    
    			return temp; 
    		}
    Below is a link for more information on quaternions.
    http://www.euclideanspace.com/maths/...nion/index.htm

    Regards,
    tiviinik

  5. #5
    Regular Contributor
    Join Date
    Feb 2005
    Location
    Kista, Sweden
    Posts
    267

    Re: How to spin an object with both x and z-rotation in M3G?

    I read the info about quaternions in your link, really interesting! I tried the approach in the code snippet, but the normalize method was not included. Is that method available also? So I should use this axis for a z rotation:
    float axis[] = new float[]{0f, 0f, 1.0f};
    then normalize it and then pass it to getQuaternion method to produce a z rotation?

    I should then use this axis for a x rotation:
    axis = new float[]{1.0f, 0.0f, 0f};
    normalize it and then pass it to getQuaternion method. And then combine the rotations with the multiplyQuaternions method?
    I assume that I only have to use one animationTrack and AnimationController for that combined rotation.

  6. #6
    Nokia Developer Moderator
    Join Date
    Dec 2011
    Posts
    174

    Re: How to spin an object with both x and z-rotation in M3G?

    You first set a axis and the rotation angle for the animation. Then if you want to change the axis to a different one with in the animation then you have to multiply them together. You can manage with one animationTrack and one animationController.

    Here's the normalize method. ( The vectors like {0.0f, 0.0f, 1.0f},{0.0f, 1.0f, 0.0f},{1.0f, 0.0f, 0.0f} dont have to be normalised, because they are unit vectors.)

    Code:
    		private float[] normalize(float[] vector)
    		{
    			double magnitude = 0;
    
    			float tempVector[] =  new float[vector.length];
    
    			for(int i = 0;i< vector.length;i++)
    			{
    				magnitude +=vector[i]*vector[i]; 
    			}
    
    			magnitude = Math.sqrt(magnitude);
    
    			for(int i = 0;i< vector.length;i++)
    			{
    				tempVector[i] =(float)(vector[i]/magnitude); 
    			}
    
    			return tempVector;
    
    		}
    -tiviinik
    Last edited by tiviinik; 2012-01-27 at 07:18.

  7. #7
    Regular Contributor
    Join Date
    Feb 2005
    Location
    Kista, Sweden
    Posts
    267

    Re: How to spin an object with both x and z-rotation in M3G?

    I tried with the normalize method, but the rotation doesn't look like I should. Do you know what I am doing wrong? This is my code for a combined 10 degree X and 360 degree Z rotation:

    int duration = 4000;
    KeyframeSequence keyframes = new KeyframeSequence(3, 4, KeyframeSequence.SLERP);
    keyframes.setRepeatMode(KeyframeSequence.LOOP);
    keyframes.setDuration(duration);

    float axis[] = new float[] {1.0f, 1.0f,1.0f};
    axis = normalize(axis);
    keyframes.setKeyframe(0, 0,getRotationQuaternion(0.0f, axis));
    //next keyframe:
    float axisX[] = new float[]{1.0f, 0.0f, 0.0f};
    axisX = normalize(axisX);
    float quaterionX[] = getRotationQuaternion(10f, axisX);

    float axisZ[]= new float[]{0.0f, 1.0f, 1.0f};
    axisZ = normalize(axisZ);
    float quaterionZ[] = getRotationQuaternion(360f, axisZ);
    float quaterionCombined[] = multiplyQuaternions(quaterionX, quaterionZ);
    keyframes.setKeyframe(1, duration / 2, quaterionCombined);
    //last keyframe:
    keyframes.setKeyframe(2, duration, getRotationQuaternion(0.0f, axis));

    AnimationTrack animationTrackX = new AnimationTrack(keyframes, AnimationTrack.ORIENTATION);
    AnimationController animatorXZ = new AnimationController();
    animationTrackX.setController(animatorXZ);
    plane2.addAnimationTrack(animationTrackX);

  8. #8
    Regular Contributor
    Join Date
    Feb 2005
    Location
    Kista, Sweden
    Posts
    267

    Re: How to spin an object with both x and z-rotation in M3G?

    I got this to work now. This code fixed the problem:

    private final void animationInitialization3() {

    int duration = 4000;
    KeyframeSequence keyframes = new KeyframeSequence(3, 4, KeyframeSequence.SLERP);
    keyframes.setRepeatMode(KeyframeSequence.LOOP);
    keyframes.setDuration(duration);

    float axis[] = new float[] {1.0f, 1.0f,1.0f};
    axis = normalize(axis);
    keyframes.setKeyframe(0, 0,getRotationQuaternion(0.0f, axis));
    //next keyframe:
    float axisX[] = new float[]{1.0f, 0.0f, 0.0f};
    axisX = normalize(axisX);
    float quaterionX[] = getRotationQuaternion(60f, axisX);

    float axisZ[]= new float[]{0.0f, 0.0f, 1.0f};
    axisZ = normalize(axisZ);
    float quaterionZ[] = getRotationQuaternion(180f, axisZ);
    float quaterionCombined[] = multiplyQuaternions(quaterionX, quaterionZ);
    keyframes.setKeyframe(1, duration / 2, quaterionCombined);
    //last keyframe:
    keyframes.setKeyframe(2, duration, getRotationQuaternion(360f, axis));//multpiply to get combined
    AnimationTrack animationTrackX = new AnimationTrack(keyframes, AnimationTrack.ORIENTATION);
    animatorXZ = new AnimationController();
    animationTrackX.setController(animatorXZ);

    plane2.addAnimationTrack(animationTrackX);
    mesh1.addAnimationTrack(animationTrackX);
    mesh2.addAnimationTrack(animationTrackX);
    mesh3.addAnimationTrack(animationTrackX);

    mesh1c.addAnimationTrack(animationTrackX);
    mesh2c.addAnimationTrack(animationTrackX);
    mesh3c.addAnimationTrack(animationTrackX);

    }

  9. #9
    Regular Contributor
    Join Date
    Feb 2005
    Location
    Kista, Sweden
    Posts
    267

    Re: How to spin an object with both x and z-rotation in M3G?

    I can spin the object with both x and z-rotation now. It is a bit more complicated than this, because the object has 3 layers. The best thing would be if each layer could spin in the same z rotation, but with different x rotation. I tried to implement this with a KeyframeSequence and an AnimationController, like in the animationInitialization above. This was however not possible, because it was not possible to have different x rotations, while keeping the z rotation synchronized.

    Another alternative would be to set the rotations in the run() method of the thread. If I call setOrientation with X rotation and then call setOrientation with Z rotation, only the later of these rotations get executed. So is there any way to combine the X and Z rotations, so I can use the setOrientation method in the run method?
    Last edited by MobileVisuals; 2012-02-04 at 14:07. Reason: Previous posting not necessary.

  10. #10
    Nokia Developer Moderator
    Join Date
    Dec 2011
    Posts
    174

    Re: How to spin an object with both x and z-rotation in M3G?

    Hi MobileVisuals,

    Have you tryed with postRotate(float angle,float ax, float ay, float az) or perRotate(float angle,float ax, float ay, float az). Those should work.

    http://library.developer.nokia.com/i...sformable.html

    -tiviinik

  11. #11
    Regular Contributor
    Join Date
    Feb 2005
    Location
    Kista, Sweden
    Posts
    267

    Re: How to spin an object with both x and z-rotation in M3G?

    postRotate worked very well!

  12. #12
    Nokia Developer Moderator
    Join Date
    Dec 2011
    Posts
    174

    Re: How to spin an object with both x and z-rotation in M3G?

    Great! Also to save some heavy calculations (sqrt) you don't have to normalize unit vectors,because they allready are normalized. For example...

    Code:
    float axisX[] = new float[]{1.0f, 0.0f, 0.0f};
    axisX = normalize(axisX);
    // result:  axisX = {1.0f, 0.0f, 0.0f};
    -tiviinik

Similar Threads

  1. Problem with changing frame when object collides an object..
    By miksu174 in forum [Archived] Flash Lite on Nokia Devices
    Replies: 8
    Last Post: 2010-11-05, 15:51
  2. Replies: 4
    Last Post: 2010-05-07, 11:14
  3. Phone won't send object on OBEX object exchange example...
    By dcg_sorenson in forum Bluetooth Technology
    Replies: 2
    Last Post: 2009-04-30, 16:55
  4. Replies: 9
    Last Post: 2007-03-21, 07:57
  5. synchronously Active Object to asynchronously Avtive Object
    By nextage in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2003-07-10, 08:08

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •