How to get the current position from a M3G animation?
I use AnimationController, AnimationTrack and KeyframeSequence to move the camera in 3D space. I need to get the current position, but I can't find any way to retrieve the current x,y and z values. I don't find any method that I can use for this in AnimationController, AnimationTrack and KeyframeSequence. This is very easy to do in OpenGL, but I find no way of solving this problem in M3G. Does anyone know how I can get the current position?
Re: How to get the current position from a M3G animation?
Hi MobileVisuals,
You can get the angle and the axis from a Camera or a Mesh with the method getOrientation(float[] angleAxis).
For example
[CODE]float angleAndAxis[] = new float[4];//(angle x y z)
mesh.getOrientation(angleAndAxis);
System.out.println(angleAndAxis[0]+" , "+angleAndAxis[1]+" , "+angleAndAxis[2]+" , "+angleAndAxis[3]);[/CODE]
Regards,
tiviinik
Re: How to get the current position from a M3G animation?
Thanks, I can get the current position now. I want to calculate the alpha transparency values of objects based on their distance to the camera in real time. I tried to make a AnimationTrack.ALPHA for this. I have to set the keyframe values for KeyframeSequence in animationInitialization().
This doesn't seem to work because animationInitialization() is only called one time in the beginning of the app and from there, I have no way of setting the keyframe values based on the objects distance to the camera in real time. I have to set the keyframe values to fixed values. Do you know if there is any way to calculate the alpha transparency values of objects based on their distance to
the camera in real time?
I have got this to work in OpenGL, but I can't get it to work in M3G.
Re: How to get the current position from a M3G animation?
Hi MobileVisuals,
You have to make a alpha apperance for your node and then use the setAlphaFactor(float) to set the alpha transparency. Below an example how to make the apperance.
[CODE] private Appearance alphaAppearance = new Appearance();;
public void alphaAppearanceInit(Texture2D texture)
{
PolygonMode alphaPolygonMode = new PolygonMode();
alphaPolygonMode.setPerspectiveCorrectionEnable(true);
alphaAppearance.setPolygonMode(alphaPolygonMode);
CompositingMode alphaCompositeMode = new CompositingMode();
alphaCompositeMode.setBlending(CompositingMode.ALPHA_ADD);
alphaAppearance.setCompositingMode(alphaCompositeMode);
alphaAppearance.setTexture(0, texture);
}[/CODE]
And then just set the appearance to your mesh and set the alpha factor.
[CODE]mesh.setAppearance(0, alphaAppearance);
mesh.setAlphaFactor(0.3f);[/CODE]
-tiviinik
Re: How to get the current position from a M3G animation?
I see, but I want to calculate the alpha transparency values of objects based on their distance to the camera in real time. I have to set the keyframe values for KeyframeSequence in animationInitialization().
AnimationInitialization() is only called one time in the beginning of the app. How can I set the keyframe values based on the objects distance to the camera in real time? This distance changes all the time. It seems like I only can set the keyframe values to fixed values in AnimationInitialization().
Re: How to get the current position from a M3G animation?
Once you set and start the animation you can't change the values in real time by setting the keyframes in the KeyframeSequence. So they are fixed. But you can use methods like setTranslation(float,float,float), setOrientation(float,float,float), setAlphaFactor(float) in your thread to set values to objects.
More on node methods see
[URL="http://library.developer.nokia.com/index.jsp?topic=/Java_Developers_Library/GUID-07274ED2-697C-4987-ABE9-7FFE82605633/javax/microedition/m3g/Node.html"]http://library.developer.nokia.com/index.jsp?topic=/Java_Developers_Library/GUID-07274ED2-697C-4987-ABE9-7FFE82605633/javax/microedition/m3g/Node.html[/URL]
-tiviinik
Re: How to get the current position from a M3G animation?
I see, that would be very useful! Which thread do you mean? Do you mean the thread, which is handled by the run method in the TimerTask class?
Or do you mean that I should start an extra animation thread?
Re: How to get the current position from a M3G animation?
It's good to use as less threads as possible so don't start a new thread for it. Use the same thread that you are using for you animation.
-tiviinik
Re: How to get the current position from a M3G animation?
I got this to work now! Thanks a lot for the advice. This is the code that solved the problem:
class MyTimerTask extends TimerTask {
public void run() {
{
float cameraZ = 0;
float alphaConstant;
if (plane2 != null) {
if (cz < -optimZLimit) {
cz = 0;
} else {
cz -= frameIncr;
}
iCamera.setTranslation(0, 0, cz);
for (int i = 0; i < plane2.length; i++) {
if ((plane2[i] != null) && (gpos[i] != null)) {
cameraZ = cz;
alphaConstant = (float) ((optimZLimit - (cameraZ - gpos[i][2])) / ((float) optimZLimit));
if (alphaConstant > 1) {
alphaConstant = 1;
}
if (alphaConstant < 0) {
alphaConstant = 0;
}
plane2[i].setAlphaFactor(alphaConstant);
meshD[i].setAlphaFactor(alphaConstant * 0.8f);
mesh1[i].setAlphaFactor(alphaConstant * 0.7f);
mesh1b[i].setAlphaFactor(alphaConstant * 0.7f);
mesh1c[i].setAlphaFactor(alphaConstant * 0.7f);
}
}
}
AstralCanvas.getInstance().repaint();
}
}
}
Re: How to get the current position from a M3G animation?
Great and your welcome. One more thing when creating animations it would be better to use a Thread instead of a TimerTask. TimerTask has a fixed time which will result in a fixed frames per second and will not benefit from a better hardware. Using Threads will give the best frames per seconds and will make animations run as smooth as possible.
-tiviinik