Skip to main content

3D Arrow rotatin

No replies
goldemish
Offline
Joined: 2008-04-29

Hello,
first of all sorry for my language...

I need to create a simple rotating 3D arrow, something like this (http://gifanimate.html.it/ga_img/gif-animate/Back/BACK01.GIF).
I dont need the animation, but i just want to set the correct X,Y,Z rotation of the arrow after an user input (something to show the direction).
How can i do?
I tried a lot of examples, i also create every surfaces of the arrow with new Point3f but i still have some lights trobles... i can rotate, but only one surface is lighted!

I try to post my code...
I don't think it is very good, if you have some suggestions or there is a better way to design a 3d arrow, please let me know it :-) Thanks!

[code]

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class Arrow3D extends Applet {

private SimpleUniverse u = null;

public BranchGroup createSceneGraph() {

BranchGroup objRoot = new BranchGroup();
Point3f[] myCoords = {

new Point3f( 0.0f, 0.0f, 0.1f ),
new Point3f( 0.0f, 0.0f, 0.1f ),
new Point3f( 0.3f, 0.3f, 0.1f ),
new Point3f( -0.3f, 0.3f, 0.1f ),
new Point3f( -0.1f, 0.3f, 0.1f ),
new Point3f( 0.1f, 0.3f, 0.1f ),
new Point3f( 0.20f, 0.8f, 0.1f ),
new Point3f( -0.20f, 0.8f, 0.1f ),

new Point3f( -0.20f, 0.8f, 0.0f ),
new Point3f( 0.20f, 0.8f, 0.0f ),
new Point3f( 0.1f, 0.3f, 0.0f ),
new Point3f( -0.1f, 0.3f, 0.0f ),
new Point3f( -0.3f, 0.3f, 0.0f ),
new Point3f( 0.3f, 0.3f, 0.0f ),
new Point3f( 0.0f, 0.0f, 0.0f ),
new Point3f( 0.0f, 0.0f, 0.0f ),

new Point3f( 0.0f, 0.0f, 0.0f ),
new Point3f( 0.0f, 0.0f, 0.1f ),
new Point3f( -0.3f, 0.3f, 0.1f ),
new Point3f( -0.3f, 0.3f, 0.0f ),

new Point3f( -0.1f, 0.3f, 0.1f ),
new Point3f( -0.1f, 0.3f, 0.0f ),
new Point3f( -0.3f, 0.3f, 0.0f ),
new Point3f( -0.3f, 0.3f, 0.1f ),

new Point3f( -0.1f, 0.3f, 0.1f ),
new Point3f( -0.20f, 0.8f, 0.1f ),
new Point3f( -0.20f, 0.8f, 0.0f ),
new Point3f( -0.1f, 0.3f, 0.0f ),

new Point3f( 0.3f, 0.3f, 0.0f ),
new Point3f( 0.3f, 0.3f, 0.1f ),
new Point3f( 0.0f, 0.0f, 0.1f ),
new Point3f( 0.0f, 0.0f, 0.0f ),

new Point3f( 0.3f, 0.3f, 0.1f ),
new Point3f( 0.3f, 0.3f, 0.0f ),
new Point3f( 0.1f, 0.3f, 0.0f ),
new Point3f( 0.1f, 0.3f, 0.1f ),

new Point3f( 0.1f, 0.3f, 0.1f ),
new Point3f( 0.1f, 0.3f, 0.0f ),
new Point3f( 0.20f, 0.8f, 0.0f ),
new Point3f( 0.20f, 0.8f, 0.1f ),

new Point3f( 0.20f, 0.8f, 0.0f ),
new Point3f( -0.20f, 0.8f, 0.0f ),
new Point3f( -0.20f, 0.8f, 0.1f ),
new Point3f( 0.20f, 0.8f, 0.1f )
};

Vector3f[] myNormals = {
new Vector3f( 1.0f, 0.0f, 0.0f ),
new Vector3f( 0.0f, 1.0f, 0.0f ),
new Vector3f( 0.0f, 0.0f, 1.0f )
};

QuadArray myQuads = new QuadArray(myCoords.length, GeometryArray.COORDINATES | GeometryArray.NORMALS );
myQuads.setCoordinates( 0, myCoords );

Appearance appearance = new Appearance();
Material lightingProps = new Material(new Color3f(0f,0f,0f), new Color3f(0f,0f,0.4f), new Color3f(1f,0f,1f), new Color3f(1f,0f,0f), 28f);
appearance.setMaterial(lightingProps);

myQuads.setNormals( 0, myNormals );
Shape3D myShape = new Shape3D( myQuads, appearance );

Transform3D trasform = new Transform3D();
Transform3D rotazX = new Transform3D();
Transform3D rotazY = new Transform3D();
Transform3D rotazZ = new Transform3D();
rotazX.rotX(-45 * Math.PI/180);
rotazY.rotY(0 * Math.PI/180);
rotazZ.rotZ(0 * Math.PI/180);

trasform.setTranslation(new Vector3d(0,-0.4,0));
trasform.mul(rotazX);
trasform.mul(rotazY);
trasform.mul(rotazZ);

TransformGroup objTrans = new TransformGroup(trasform);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

objTrans.addChild(myShape);
objRoot.addChild(objTrans);

Color3f lightColour = new Color3f(1.0f, 1.0f, 1.0f);
Vector3f lightDir = new Vector3f(0.0f, -1.0f, -1.0f);
DirectionalLight light = new DirectionalLight(lightColour, lightDir);
light.setInfluencingBounds(new BoundingSphere(new Point3d(0.0, -1.0, 0.0),1));

objRoot.addChild(light);

objRoot.compile();

return objRoot;
}

public Arrow3D() {
}

public void init() {
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
BranchGroup scene = createSceneGraph();
u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}

public void destroy() {
u.cleanup();
}

public static void main(String[] args) {
new MainFrame(new Arrow3D(), 256, 256);
}
}

Message was edited by: goldemish