Java3D 球体轨道
Java3D Sphere Orbit
我找遍了所有地方,但找不到关于如何在 Java 3D 中使球体绕中心点旋转的任何信息。具体来说,我想让一个球体以恒定的速度围绕原点做圆周运动,并让它永远循环。我猜这些方程式与它有关:
X = originX + sin(angle)*Size;
Y = originY + cos(angle)*Size;
我尝试使用 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);
}
代码一团糟(好像有部分代码在发布时被复制了?)。但是关于实际问题:
一个TransformGroup
确实只能包含一个特定的Transform3D
。尽管可以在 单个 Transform3D
中进行 assemble 多重变换(例如旋转和平移),但这不能很好地与预定义的插值器一起工作。
作为 基于场景图的 API,Java3D 的整体理念是 assemble 节点的 "tree",每个节点都有特定的用途。
在这种情况下,您的树将包含几个节点:
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
方法其实是一样的!其中一个指行星,一个指行星到 translated planet。所以对于一个真正的应用程序,它们可以组合成 one 方法。我希望在当前的形式中,组装几个节点可能会更清楚)
Edit: Extended based on the comments
为了添加另一个围绕现有对象旋转的对象(月亮)(它自己已经在旋转并围绕太阳运行),您必须将一个新分支附加到适当的现有场景图节点。根据上面的 ("ASCII-Art") 图像,您必须将此节点附加到 "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);
}
}
我找遍了所有地方,但找不到关于如何在 Java 3D 中使球体绕中心点旋转的任何信息。具体来说,我想让一个球体以恒定的速度围绕原点做圆周运动,并让它永远循环。我猜这些方程式与它有关:
X = originX + sin(angle)*Size;
Y = originY + cos(angle)*Size;
我尝试使用 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);
}
代码一团糟(好像有部分代码在发布时被复制了?)。但是关于实际问题:
一个TransformGroup
确实只能包含一个特定的Transform3D
。尽管可以在 单个 Transform3D
中进行 assemble 多重变换(例如旋转和平移),但这不能很好地与预定义的插值器一起工作。
作为 基于场景图的 API,Java3D 的整体理念是 assemble 节点的 "tree",每个节点都有特定的用途。
在这种情况下,您的树将包含几个节点:
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
方法其实是一样的!其中一个指行星,一个指行星到 translated planet。所以对于一个真正的应用程序,它们可以组合成 one 方法。我希望在当前的形式中,组装几个节点可能会更清楚)
Edit: Extended based on the comments
为了添加另一个围绕现有对象旋转的对象(月亮)(它自己已经在旋转并围绕太阳运行),您必须将一个新分支附加到适当的现有场景图节点。根据上面的 ("ASCII-Art") 图像,您必须将此节点附加到 "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);
}
}