Java 3d

amanatarra7man Messages postés 10 Date d'inscription lundi 23 février 2009 Statut Membre Dernière intervention 18 mars 2011 - 27 févr. 2009 à 23:19
Sizvix Messages postés 2 Date d'inscription mardi 3 mai 2005 Statut Membre Dernière intervention 14 août 2012 - 3 mars 2009 à 10:22
Svp ce code affiche 2 cylindre de grandeurs differents 
je ve afficher l'un sous l'autre mais je pe pas svp aidez moi
voila le code:
/ Etape 1 :
// Importation des packages Java 2
import java.applet.Applet;
import java.awt.*;


// Etape 2 :
// Importation des packages Java 3D
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;


public class Cylindre3D extends Applet {


  public Cylindre3D() {
    this.setLayout(new BorderLayout());


    // Etape 3 :
    // Creation du Canvas 3D
    Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());


    this.add(canvas3D, BorderLayout.CENTER);


    // Etape 4 :
    // Creation d'un objet SimpleUniverse
    SimpleUniverse simpleU = new SimpleUniverse(canvas3D);


    // Etape 5 :
    // Positionnement du point d'observation pour avoir une vue correcte de la
    // scene 3D
    simpleU.getViewingPlatform().setNominalViewingTransform();


    // Etape 6 :
    // Creation de la scene 3D qui contient tous les objets 3D que l'on veut visualiser
    BranchGroup scene = createSceneGraph();


    // Etape 7 :
    // Compilation de la scene 3D
    scene.compile();


    // Etape 8:
    // Attachement de la scene 3D a l'objet SimpleUniverse
    simpleU.addBranchGraph(scene);
  }


  /**
   * Creation de la scene 3D qui contient tous les objets 3D
   * @return scene 3D
   */
  public BranchGroup createSceneGraph() {
    // Creation de l'objet parent qui contiendra tous les autres objets 3D
    BranchGroup parent = new BranchGroup();


    /************ Partie de code concernant l'animation *************/
    /* Elle sera expliquee en details dans les chapitres relatifs aux
     transformations geometriques et aux animations */
    TransformGroup objSpin = new TransformGroup();
    objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    Alpha rotationAlpha = new Alpha(-1, 4000);
    RotationInterpolator rotator =
        new RotationInterpolator(rotationAlpha, objSpin);
    BoundingSphere bounds = new BoundingSphere();
    rotator.setSchedulingBounds(bounds);
    objSpin.addChild(rotator);
   
     Transform3D rotate = new Transform3D();
   rotate.rotX(Math.PI/3.0d);
    //2cylindre
   
    Transform3D trans=new Transform3D();
    trans.set(new Vector3f(0.5f, -0.3f, 0.9f));
    TransformGroup objSpin2 = new TransformGroup();
//    objSpin2.set(new Vector3f(0.7f, -0.3f, 1.0f));
   
    objSpin2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    Alpha rotationAlpha2 = new Alpha(-1, 4000);
    RotationInterpolator rotator2 =
        new RotationInterpolator(rotationAlpha2, objSpin2);
    BoundingSphere bounds2 = new BoundingSphere();
    rotator2.setSchedulingBounds(bounds2);
    objSpin2.addChild(rotator2);
   
   
   
   
    //
   
  
   
   
   
    /*************** Fin de la partie relative a l'animation ***************/


    // Arriere plan en blanc
    Background background = new Background(1, 1, 1);
    background.setApplicationBounds(new BoundingBox());
    parent.addChild(background);


    // Construction du cylindre
    objSpin.addChild(new Cylinder(0.9f, 0.1f));
    parent.addChild(objSpin);
    //construction cylindre 2
     objSpin2.addChild(new Cylinder(0.5f, 0.2f));
    parent.addChild(objSpin2);


    return parent;
  }


  /**
   * Etape 9 :
   * Methode main() nous permettant d'utiliser cette classe comme une applet
   * ou une application.
   * @param args
   */
  public static void main(String[] args) {
    Frame frame = new MainFrame(new Cylindre3D(), 256, 256);
  }
}

1 réponse

Sizvix Messages postés 2 Date d'inscription mardi 3 mai 2005 Statut Membre Dernière intervention 14 août 2012
3 mars 2009 à 10:22
une des solutions est de rajouter un noeud qui prend ta translation :

" ...
    Transform3D trans=new Transform3D();
    trans.set(new Vector3f(0.5f, -0.3f, 0.9f));
    TransformGroup tgTrans = new TransformGroup(trans) ;        //on créer un TransformGoup qui contiendra trans
...  
    objSpin2.addChild(rotator2);
    tgTrans.addChild(objSpin2);        //on met objSpin2 dans les "fils" de tgTrans
...
    objSpin2.addChild(new Cylinder(0.5f, 0.2f));
    parent.addChild(tgTrans);        //au lieu de parent.addChild(objSpin2);  objSpin2 étant maintenant dans la branche de tgTrans
...

Il y a une autre solution avec une "multiplication" entre ta rotation et ta translation, mais je viens de commencer, je suis pas encore à l'aise avec ces multiplications.
Je ne sais pas si c'est la manière la plus propre que je te propose, mais en tout cas elle marche.

Sizvix--
0
Rejoignez-nous