gpt4 book ai didi

Java3D 球体轨道

转载 作者:行者123 更新时间:2023-11-30 08:12:50 25 4
gpt4 key购买 nike

我到处都看过,但找不到任何关于如何在 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 中组合多个变换(例如旋转和平移),但这不能很好地与预定义的插值器一起工作。

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

在这种情况下,您的树将包含多个节点:

 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方法实际上是在做同样的事情!其中一个是指行星,并且其中一个是指翻译星球。所以对于一个真正的应用程序,它们可以组合成一个方法。我希望在目前的形式中,组装几个的想法节点可能会变得更清晰)


Edit: Extended based on the comments

为了添加另一个围绕现有对象旋转的对象(月亮)(它自己已经在旋转并围绕太阳运行),您必须将一个新分支附加到适当的现有场景图节点。根据上面的(“ASCII-Art”)图像,您必须将此节点附加到“TTG”。新节点本身将包含一个轨道节点、一个平移节点和一个旋转节点(其中包含实际的月球物体)。

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


}

关于Java3D 球体轨道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30234453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com