Java3D Sphere Orbit

时间:2015-05-14 10:03:01

标签: java animation transformation java-3d

我到处寻找,我无法找到关于如何使球体轨道成为Java 3D中心点的任何东西。具体来说,我想让一个球体以恒定的速度围绕原点进行圆周运动,并使其永远循环。我想这些方程式与它有关:

  

X = originX + sin(角度)*尺寸;

     

Y = originY + cos(角度)*尺寸;

我尝试使用PositionInterpolator,但只覆盖1轴,因此不会形成圆形轨道。另外我注意到一次只能进行1次转换,在轴上旋转或者在PositionInterpolator中,我如何同时将两个转换应用于对象?

      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);
    }

1 个答案:

答案 0 :(得分:3)

代码是一团糟(似乎它的一部分在发布时已经重复了?)。但是关于实际问题:

一个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方法实际上是一样的!其中一个指的是行星,其中一个指的是翻译 planet。所以对于一个真正的应用程序,它们可以组合成 one 方法。我希望在当前形式中,组装多个节点的想法可能会变得更加清晰)< / p>

  

编辑:根据评论进行扩展

为了添加围绕现有对象旋转的另一个对象(月亮)(其自身已经旋转​​并绕太阳运行),您必须将新分支附加到相应的现有场景图节点。基于上述(&#34; ASCII-Art&#34;)图像,您必须将此节点附加到&#34; TTG&#34;。新节点本身将包含轨道运行节点,转换节点和旋转节点(包含实际的月球对象)。

Orbiting

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

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);
    }


}