Java3D球体轨道

Java3D球体轨道,java,animation,transformation,java-3d,Java,Animation,Transformation,Java 3d,我到处都找过了,但在Java3D中找不到任何关于如何使球体环绕中心点的信息。具体地说,我想让一个球体以恒定的速度围绕原点做圆周运动,使它永远循环。我猜这些方程与此有关: X=原点X+sin(角度)*尺寸 Y=原点+余弦(角度)*尺寸 我试着使用一个位置插值器,但它只覆盖一个轴,所以不能形成一个圆形轨道。我还注意到一次只能进行一个变换,轴上的旋转或位置插值,如何将这两个变换同时应用于对象 Planet planet = new Planet(new Color3f(0.2f,0.2f,

我到处都找过了,但在Java3D中找不到任何关于如何使球体环绕中心点的信息。具体地说,我想让一个球体以恒定的速度围绕原点做圆周运动,使它永远循环。我猜这些方程与此有关:

X=原点X+sin(角度)*尺寸

Y=原点+余弦(角度)*尺寸

我试着使用一个位置插值器,但它只覆盖一个轴,所以不能形成一个圆形轨道。我还注意到一次只能进行一个变换,轴上的旋转或位置插值,如何将这两个变换同时应用于对象

      Planet planet = new Planet(new Color3f(0.2f,0.2f,0.2f),new Vector3f(1.0f,0.0f,-10.0f), 0.2f,1.0f,1.0f, 1.0f);
      Sphere planet = new Sphere(planet.radius,planet.pl);
    
    Transform3D tfgPlanet = new Transform3D();
    
  //  tfg.setTranslation(planet.position);
    tfgplanet.setTranslation(planet.position);
    TransformGroup tgm = new TransformGroup(tfgPlanet);
    tgm.addChild(planet);
    theScene.addChild(tgm);


    Transform3D planetRotate = new Transform3D();
    
    int timerotation = 1500;//A slow rotation takes 1.5 seconds.
    
    //The Alpha for rotation
    Alpha planetRotationStart = new Alpha(1000,
            Alpha.INCREASING_ENABLE,0,0,timerotation,0,0,0,0,0);

    //rotate around axis
    RotationInterpolator planetrotation = new RotationInterpolator(
    planetRotationStart,tgm,
    planetRotate,planet.orbitAngle,(float) Math.PI*2);

    BoundingSphere bind = new BoundingSphere(new Point3d(0.0,0.0,0.0),Double.MAX_VALUE);
    planetrotation.setSchedulingBounds(bind);

    tgm.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    tgm.addChild(planetrotation);
    

    Alpha planetOrbit = new Alpha(1000,
            Alpha.INCREASING_ENABLE,0,0,timerotation,0,0,0,0,0);
    
    Transform3D axis = new Transform3D();
    PositionInterpolator pi = new PositionInterpolator(planetOrbit,tgm,axis,1.0f, 10.0f);
    pi.setSchedulingBounds(bind);
    tgm.addChild(pi);

    //compiles scene
     theScene.compile();

    //Add everything to the universe.
    su.addBranchGraph(theScene);
    }
代码乱七八糟(而且似乎在发布时部分代码被复制了?)。然而,关于实际问题:

确实,一个
TransformGroup
只能包含一个特定的
Transform3D
。虽然可以在单个
Transform3D
中组合多个变换(例如旋转和平移),但这与预定义的插值器不太匹配

Java3D是一个基于场景图的API,它的整体思想是组装一个节点“树”,其中每个节点都有特定的用途

在这种情况下,树将由几个节点组成:

 S     Sphere: The planet
 |
 |
RTG    Rotation TransformGroup: Responsible for 
 |     rotating the planet about its y-asis
 |
 |
TTG    Translation TransformGroup: Responsible for 
 |     translating the (rotating) planet away from
 |     the sun
 |
OTG    Orbit TransformGroup: Responsible for 
 |     rotating the (translated and rotating) planet 
 |     about the center of the sun
 |
Root   The root node of your universe
通过提供适当的变量和方法名,可以确保代码的结构与图形的结构相似

有一个围绕中心旋转的旋转对象的完整示例:

import java.awt.GraphicsConfiguration;

import javax.media.j3d.Alpha;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Node;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class SphereOrbit
{
    public static void main(String[] args) 
    {
        System.setProperty("sun.awt.noerasebackground", "true");
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;

        GraphicsConfiguration config = 
            SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas = new Canvas3D(config);
        frame.getContentPane().add(canvas);
        SimpleUniverse simpleUniverse = new SimpleUniverse(canvas);

        BranchGroup rootBranchGroup = new BranchGroup();
        createContents(rootBranchGroup);

        simpleUniverse.addBranchGraph(rootBranchGroup);
        Transform3D viewPlatformTransform = new Transform3D();
        Transform3D t0 = new Transform3D();
        t0.setTranslation(new Vector3d(0,0,10));
        Transform3D t1 = new Transform3D();
        t1.rotX(Math.toRadians(-30));
        viewPlatformTransform.mul(t1, t0);
        simpleUniverse.getViewingPlatform().
            getViewPlatformTransform().setTransform(viewPlatformTransform);;

        frame.setSize(800,800);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    private static BoundingSphere boundingSphere = 
        new BoundingSphere(new Point3d(0.0,0.0,0.0), Double.MAX_VALUE);


    // Build the transform group that does the rotation
    // of the planet in its local coordinate system 
    // (This will cause the planet to spin about its own y-axis)
    private static TransformGroup createRotationTransformGroup(
        int rotationTimeMs)
    {
        TransformGroup rotationTransformGroup = new TransformGroup();
        rotationTransformGroup.setCapability(
            TransformGroup.ALLOW_TRANSFORM_WRITE);
        Alpha rotationAlpha = new Alpha(-1, rotationTimeMs);
        RotationInterpolator rotationInterpolator = 
            new RotationInterpolator(rotationAlpha, rotationTransformGroup);
        rotationInterpolator.setSchedulingBounds(boundingSphere);
        rotationTransformGroup.addChild(rotationInterpolator);
        return rotationTransformGroup;
    }

    // Build the transform group that moves the (rotating) planet 
    // about a certain (fixed) distance, away from the center
    private static TransformGroup createTranslatingTransformGroup(
        double distanceFromCenter)
    {
        TransformGroup translationTransformGroup = new TransformGroup();
        Transform3D translationTransform = new Transform3D();
        translationTransform.setTranslation(
            new Vector3d(distanceFromCenter, 0, 0));
        translationTransformGroup.setTransform(translationTransform);
        return translationTransformGroup;
    }

    // Build the transform group that orbits the planet. This
    // transform group will rotate the (translated and rotating)
    // planet around the center
    private static TransformGroup createOrbitTransformGroup(int orbitTimeMs)
    {
        TransformGroup orbitTransformGroup = new TransformGroup();
        orbitTransformGroup.setCapability(
            TransformGroup.ALLOW_TRANSFORM_WRITE);
        Alpha orbitAlpha = new Alpha(-1, orbitTimeMs);
        RotationInterpolator orbitInterpolator = 
            new RotationInterpolator(orbitAlpha, orbitTransformGroup);
        orbitInterpolator.setSchedulingBounds(boundingSphere);
        orbitTransformGroup.addChild(orbitInterpolator);
        return orbitTransformGroup;
    }


    private static void createContents(BranchGroup rootBranchGroup)
    {
        // The basic properties of the Planet
        int rotationTimeMs = 1500;
        double distanceFromCenter = 3;
        int orbitTimeMs = 4000;

        // The planet (using a color cube here, so that its 
        // own rotation is visible)
        //Node planet = new Sphere(0.2f);
        Node planet = new ColorCube(0.2);

        TransformGroup rotationTransformGroup = 
            createRotationTransformGroup(rotationTimeMs);

        // Attach the planet to the rotation transform group
        rotationTransformGroup.addChild(planet);

        TransformGroup translationTransformGroup =
            createTranslatingTransformGroup(distanceFromCenter);

        // Attach the rotating planet to the translation transform group
        translationTransformGroup.addChild(rotationTransformGroup);

        TransformGroup orbitTransformGroup = 
            createOrbitTransformGroup(orbitTimeMs);

        // Add the (translated and rotating) planet to the orbitTransformGroup
        orbitTransformGroup.addChild(translationTransformGroup);

        rootBranchGroup.addChild(orbitTransformGroup);
    }
}
(注意:仔细观察时,您会注意到,
createRotationTransformGroup
方法和
createOrbitTransformGroup
方法实际上也在做同样的事情!其中一个是指行星,另一个是指平移的行星。因此,对于真正的应用程序,它们可以组合成一个方法我希望在目前的形式下,组装几个节点的想法可能会变得更清晰)


编辑:根据注释进行扩展

为了添加另一个围绕现有对象旋转的对象(月亮)(我自己已经在旋转并围绕太阳旋转),必须将新的分支附加到相应的现有场景图节点。根据上述(“ASCII艺术”)图像,必须将此节点附加到“TTG”。新节点本身将包含一个轨道节点、一个平移节点和一个旋转节点(其中包含实际的月球对象)

如果您打算构建一个完整的太阳系,那么您可能应该引入适当的实用方法-类似于我在这个扩展示例中已经描述的方法:

import java.awt.GraphicsConfiguration;

import javax.media.j3d.Alpha;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Node;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class SphereOrbitExtended
{
    public static void main(String[] args) 
    {
        System.setProperty("sun.awt.noerasebackground", "true");
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;

        GraphicsConfiguration config = 
            SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas = new Canvas3D(config);
        frame.getContentPane().add(canvas);
        SimpleUniverse simpleUniverse = new SimpleUniverse(canvas);

        BranchGroup rootBranchGroup = new BranchGroup();
        createContents(rootBranchGroup);

        simpleUniverse.addBranchGraph(rootBranchGroup);
        Transform3D viewPlatformTransform = new Transform3D();
        Transform3D t0 = new Transform3D();
        t0.setTranslation(new Vector3d(0,0,10));
        Transform3D t1 = new Transform3D();
        t1.rotX(Math.toRadians(-30));
        viewPlatformTransform.mul(t1, t0);
        simpleUniverse.getViewingPlatform().
            getViewPlatformTransform().setTransform(viewPlatformTransform);;

        frame.setSize(800,800);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    private static BoundingSphere boundingSphere = 
        new BoundingSphere(new Point3d(0.0,0.0,0.0), Double.MAX_VALUE);


    // Build the transform group that does a rotation about the
    // y-axis, rotating once in the given time
    private static TransformGroup createRotationTransformGroup(
        int rotationTimeMs, boolean forward)
    {
        TransformGroup rotationTransformGroup = new TransformGroup();
        rotationTransformGroup.setCapability(
            TransformGroup.ALLOW_TRANSFORM_WRITE);
        Alpha rotationAlpha = new Alpha(-1, rotationTimeMs);
        float angle = forward ? (float) (2 * Math.PI) : (float)(-2 * Math.PI);
        RotationInterpolator rotationInterpolator = 
            new RotationInterpolator(rotationAlpha, rotationTransformGroup, 
                new Transform3D(), 0.0f, angle);
        rotationInterpolator.setSchedulingBounds(boundingSphere);
        rotationTransformGroup.addChild(rotationInterpolator);
        return rotationTransformGroup;
    }

    // Build the transform group that performs the specified translation
    private static TransformGroup createTranslatingTransformGroup(
        double dx, double dy, double dz)
    {
        TransformGroup translationTransformGroup = new TransformGroup();
        Transform3D translationTransform = new Transform3D();
        translationTransform.setTranslation(
            new Vector3d(dx, dy, dz));
        translationTransformGroup.setTransform(translationTransform);
        return translationTransformGroup;
    }


    private static void createContents(BranchGroup rootBranchGroup)
    {
        int orbitTimeMs = 4000;
        TransformGroup orbitTransformGroup = 
            createRotationTransformGroup(orbitTimeMs, true);
        rootBranchGroup.addChild(orbitTransformGroup);

        double distanceFromCenter = 3;
        TransformGroup translationTransformGroup =
            createTranslatingTransformGroup(distanceFromCenter, 0, 0);
        orbitTransformGroup.addChild(translationTransformGroup);

        int rotationTimeMs = 1500;
        Node planet = new ColorCube(0.2);
        TransformGroup rotationTransformGroup = 
            createRotationTransformGroup(rotationTimeMs, true);
        rotationTransformGroup.addChild(planet);
        translationTransformGroup.addChild(rotationTransformGroup);

        int moonOrbitTimeMs = 1000;
        TransformGroup moonOrbitTransformGroup = 
            createRotationTransformGroup(moonOrbitTimeMs, false);
        translationTransformGroup.addChild(moonOrbitTransformGroup);

        double moonDistanceFromPlanet = 0.8;
        TransformGroup moonTranslationTransformGroup =
            createTranslatingTransformGroup(moonDistanceFromPlanet, 0, 0);
        moonOrbitTransformGroup.addChild(moonTranslationTransformGroup);

        int moonRotationTimeMs = 500;
        Node moon = new ColorCube(0.1);
        TransformGroup moonRotationTransformGroup = 
            createRotationTransformGroup(moonRotationTimeMs, true);
        moonRotationTransformGroup.addChild(moon);
        moonTranslationTransformGroup.addChild(moonRotationTransformGroup);
    }


}

您的代码似乎只适用于colorcube,当它被具有材质的球体替换时,不会绘制任何内容。它会显示最后一个节点中的任何内容。如果它不显示球体,则问题不在我发布的代码中。使用
node planet=new sphere(0.2f,new Appearance())
您将看到一个球体。有关材质和照明的进一步问题应在其他地方澄清。如何更改这些方法的中心轨道点,我已使其成为新的Vector3d(parentPosition.x+distanceFromCenter,parentPosition.y,parentPosition.z))是平移变换组的中心,并创建了一个边界球体,该球体是父行星位置的位置为了更改中心轨道点(即“太阳”的位置),您必须插入另一个变换组(具有所需的平移)在
rootBranchGroup
orbitTransformGroup
之间。我不确定你的意思,我试图让月球绕地球运行,但它绕着中心点运行,更改
orbitTransformGroup
中的代码不会更改月球的轨道点,我不确定在代码行之间添加什么转换,以使其围绕不断移动的中心点旋转