Hallo,
Ich möchte eine Kamera haben die sich innerhalb meines Sonnensystems mit einem Planeten mitbewegt. Das heißt sie soll "hinter" dem Planeten installiert sein und in richtig Sonne schauen.
Verwende ich den folgenden Quellcode so ist die Kamera beim Start des Programms korrekt hinter dem Planeten, doch bewegt sich nicht mit dem Planeten mit, kann mir einer erklären warum?
[highlight=Java]public class Planet
{
////////////////////////////////////////////////////////////////////////////////////
// MEMBERS
// Planeten eigenschaften
private float size = 0; // Größe des Planeten
private float ownRotDuration = 0; // EigenRotationsdauer
private float distanceToCenter = 0; // Entfernung zum Zentralgestirn
protected float centerRotDuration = 0; // Rotationsdauer um Zentralgestirn
private float ownNeigung = 0;
// Planet selbst
private Sphere sphere = null; // Die Kugel selbst
// Oberflächeneigenschaften
private String texString = "";
private boolean texActivated = true;
// Eigenlicht
//private AmbientLight ownALight = new AmbientLight(new Color3f(1f, 1f, 1f));
//private DirectionalLight DLgt = new DirectionalLight( new Color3f(1f, 1f, 1f),
// new Vector3f(-0.5f,-0.5f,-1f));
// BranchGroup (Planetensystem)
public BranchGroup RootBG = new BranchGroup(); // Root Objekt
// Transformationsgruppe (eigene Rotation, Neigung)
private TransformGroup SphereTG = new TransformGroup(); // Transformationen für die Kugel
private Transform3D SphereT3D = new Transform3D(); // Transformierungen für die Kugel
//Rotationswerkzeuge für den Planeten (Eigenrotation)
private TransformGroup ownRotTG = new TransformGroup(); // Transformationen für die Rotation
private RotationInterpolator ownRotator;
private Alpha ownRotationAlpha;
// Transformationsgruppe (Rotation ums Zentralgestirn)
protected TransformGroup CenterTG = new TransformGroup();
private Transform3D CenterT3D = new Transform3D(); // Transformierungen für die Kugel
//Rotationswerkzeuge für den Planeten (Rotation um Zentralgestirn)
private TransformGroup centerRotTG = new TransformGroup(); // Transformationen für die Rotation
private RotationInterpolator centerRotator;
private Alpha centerRotationAlpha;
// Umlaufbahn der PlanetenKamera
private TransformGroup CamTG = new TransformGroup(); // Transformationen für die Kamera
private Transform3D CamT3D = new Transform3D(); // Transformierungen für die Kamera
static int i = 0;
////////////////////////////////////////////////////////////////////////////////////
// Konstruktoren
public Planet(float size, float ownRotDuration, float distanceToCenter,
float centerRotDuration, float ownNeig, String tex)
{
this.size = size;
this.ownRotDuration = ownRotDuration;
this.distanceToCenter = distanceToCenter;
this.centerRotDuration = centerRotDuration;
this.ownNeigung = ownNeig;
this.texString = tex;
this.initObject(); // Erscheinungsbild festlegen
}
////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
private void initObject()
{
this.initRotations();
// Objekt anlegen
sphere = new Sphere(size, Sphere.GENERATE_NORMALS
| Sphere.GENERATE_TEXTURE_COORDS, 64,
new PlanetAppearance(texString));
// festgelegte Rotation(zb. Erdachsenneigung)
if (ownNeigung != 0.0) {
SphereT3D.setRotation(new AxisAngle4f(1f, 0f, 0f, (float) Math
.toRadians(-ownNeigung)));
}
// Position festlegen
SphereT3D.setTranslation(new Vector3f(0.0f, 0f, -distanceToCenter));
SphereTG.setTransform(SphereT3D);
// Position der Kamera festlegen
CamT3D.setTranslation(new Vector3f(0.0f, 0f, -distanceToCenter - 20));
CamTG.setTransform(CamT3D);
//CamTG.addChild(centerRotator);
// TODO: wieder entfernen wenn Planeten Kamera funktioniert
CamTG.addChild(new Sphere(0.2f, Sphere.GENERATE_NORMALS
| Sphere.GENERATE_TEXTURE_COORDS, 64, new Appearance()));
CamTG.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
CamTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
CamTG.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
CamTG.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
// eigene Rotation festlegen
SphereTG.addChild(ownRotTG);
SphereTG.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
ownRotTG.addChild(sphere);
ownRotTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
ownRotTG.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
ownRotTG.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
//ownRotTG.setCapability(TransformGroup.);
SphereTG.addChild(ownRotator);
// Rotation ums Zentralgestirn
CenterTG.addChild(centerRotTG);
centerRotTG.addChild(SphereTG);
centerRotTG.addChild(CamTG); // Kamera um ZG rotieren
centerRotTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
CenterTG.addChild(centerRotator);
CenterTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
CenterTG.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
CenterTG.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
// Oribts entfernen/ hinzufügen
CenterTG.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
}
///////////////////////////////////////////////////////////////////////////////////////////////
private void initRotations()
{
// Eigenrotation
ownRotationAlpha = new Alpha(-1, // loopCount -1 = endlos
/*Alpha.DECREASING_ENABLE|*/Alpha.INCREASING_ENABLE, // mode, welche Parts sollen durchlaufen werden
0, // triggerTime, wann soll Rotation beginnen 0=sofort
0, // phaseDelayDuration, wann soll Rotation beginnen 0=sofort
//increasing Part
(int) ownRotDuration, // increasingAlphaDuration in msec (Rotationsgeschwindigkeit)
0, // increasingAlphaRampDuration Beschleunigung (brauch ich net)
0, // alphaAtOneDuration
// decreasing Part / wird nicht betrachtet wenn nur INCREASING_ENABLE gesetzt
9000, // decreasingAlphaDuration
3000, // decreasingAlphaRampDuration
500); // alphaAtZeroDuration
ownRotator = new RotationInterpolator(ownRotationAlpha, ownRotTG);
ownRotator.setMinimumAngle(0f);
ownRotator.setMaximumAngle((float) Math.toRadians(360));
ownRotator.setSchedulingBounds(Universe.BigBounds);
// Rotation ums Zentralgestirn
centerRotationAlpha = new Alpha(-1, // loopCount -1 = endlos
//Alpha.DECREASING_ENABLE|
Alpha.INCREASING_ENABLE, // mode, welche Parts sollen durchlaufen werden
0, // triggerTime, wann soll Rotation beginnen 0=sofort
0, // phaseDelayDuration, wann soll Rotation beginnen 0=sofort
//increasing Part
(int) centerRotDuration, // increasingAlphaDuration in msec (Rotationsgeschwindigkeit)
0, // increasingAlphaRampDuration Beschleunigung (brauch ich net)
0, // alphaAtOneDuration
// decreasing Part / wird nicht betrachtet wenn nur INCREASING_ENABLE gesetzt
9000, // decreasingAlphaDuration
3000, // decreasingAlphaRampDuration
500); // alphaAtZeroDuration
centerRotator = new RotationInterpolator(centerRotationAlpha, centerRotTG);
centerRotator.setMinimumAngle(0f);
centerRotator.setMaximumAngle((float) Math.toRadians(360));
//TODO: ist hier bigbounds nötig?, wofür steht schedulingBounds?
centerRotator.setSchedulingBounds(Universe.BigBounds);
}
///////////////////////////////////////////////////////////////////////////////////////////////
public void addChild(Planet planet)
{
SphereTG.addChild(planet.createSceneGraph());
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Erstellung des Planeten
public BranchGroup createSceneGraph()
{
System.out.println("createPlanet: " + (i++));
RootBG.addChild(CenterTG);
RootBG.compile();
return RootBG;
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
public TransformGroup getCamTG()
{
return CamTG;
}
}
[/highlight]
Die folgendene Klasse stellt meine Kamera da. im Konstruktor wird die ViewPlatformTransform übergeben
---->u.getViewingPlatform().getViewPlatformTransform()
[highlight=Java]
public class Camera extends BranchGroup
{
private TransformGroup transformGroup;
private Transform3D ViewT3D = new Transform3D();
/////////////////////////////////////////////////////////////////////////////////
public Camera(TransformGroup tg)
{
transformGroup = tg;
enablePlanetCamera(ObjectManager.planetErde);
}
/////////////////////////////////////////////////////////////////////////////////
public void enablePlanetCamera(Planet currentPlanet)
{
// aktuelle Transformation des Planeten holen
TransformGroup ViewTG = currentPlanet.getCamTG();
ViewTG.getTransform(ViewT3D);
// Rotation der Kamera um 180 grad auf die Sonne
ViewT3D.setRotation(new AxisAngle4f(0, -1, 0, (float) Math.toRadians(180)));
transformGroup.setTransform(ViewT3D);
}
}
[/highlight]
hoffe das erschlägt euch nicht und es ist einigermaßen verständlich :bahnhof:
Ich möchte eine Kamera haben die sich innerhalb meines Sonnensystems mit einem Planeten mitbewegt. Das heißt sie soll "hinter" dem Planeten installiert sein und in richtig Sonne schauen.
Verwende ich den folgenden Quellcode so ist die Kamera beim Start des Programms korrekt hinter dem Planeten, doch bewegt sich nicht mit dem Planeten mit, kann mir einer erklären warum?
[highlight=Java]public class Planet
{
////////////////////////////////////////////////////////////////////////////////////
// MEMBERS
// Planeten eigenschaften
private float size = 0; // Größe des Planeten
private float ownRotDuration = 0; // EigenRotationsdauer
private float distanceToCenter = 0; // Entfernung zum Zentralgestirn
protected float centerRotDuration = 0; // Rotationsdauer um Zentralgestirn
private float ownNeigung = 0;
// Planet selbst
private Sphere sphere = null; // Die Kugel selbst
// Oberflächeneigenschaften
private String texString = "";
private boolean texActivated = true;
// Eigenlicht
//private AmbientLight ownALight = new AmbientLight(new Color3f(1f, 1f, 1f));
//private DirectionalLight DLgt = new DirectionalLight( new Color3f(1f, 1f, 1f),
// new Vector3f(-0.5f,-0.5f,-1f));
// BranchGroup (Planetensystem)
public BranchGroup RootBG = new BranchGroup(); // Root Objekt
// Transformationsgruppe (eigene Rotation, Neigung)
private TransformGroup SphereTG = new TransformGroup(); // Transformationen für die Kugel
private Transform3D SphereT3D = new Transform3D(); // Transformierungen für die Kugel
//Rotationswerkzeuge für den Planeten (Eigenrotation)
private TransformGroup ownRotTG = new TransformGroup(); // Transformationen für die Rotation
private RotationInterpolator ownRotator;
private Alpha ownRotationAlpha;
// Transformationsgruppe (Rotation ums Zentralgestirn)
protected TransformGroup CenterTG = new TransformGroup();
private Transform3D CenterT3D = new Transform3D(); // Transformierungen für die Kugel
//Rotationswerkzeuge für den Planeten (Rotation um Zentralgestirn)
private TransformGroup centerRotTG = new TransformGroup(); // Transformationen für die Rotation
private RotationInterpolator centerRotator;
private Alpha centerRotationAlpha;
// Umlaufbahn der PlanetenKamera
private TransformGroup CamTG = new TransformGroup(); // Transformationen für die Kamera
private Transform3D CamT3D = new Transform3D(); // Transformierungen für die Kamera
static int i = 0;
////////////////////////////////////////////////////////////////////////////////////
// Konstruktoren
public Planet(float size, float ownRotDuration, float distanceToCenter,
float centerRotDuration, float ownNeig, String tex)
{
this.size = size;
this.ownRotDuration = ownRotDuration;
this.distanceToCenter = distanceToCenter;
this.centerRotDuration = centerRotDuration;
this.ownNeigung = ownNeig;
this.texString = tex;
this.initObject(); // Erscheinungsbild festlegen
}
////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
private void initObject()
{
this.initRotations();
// Objekt anlegen
sphere = new Sphere(size, Sphere.GENERATE_NORMALS
| Sphere.GENERATE_TEXTURE_COORDS, 64,
new PlanetAppearance(texString));
// festgelegte Rotation(zb. Erdachsenneigung)
if (ownNeigung != 0.0) {
SphereT3D.setRotation(new AxisAngle4f(1f, 0f, 0f, (float) Math
.toRadians(-ownNeigung)));
}
// Position festlegen
SphereT3D.setTranslation(new Vector3f(0.0f, 0f, -distanceToCenter));
SphereTG.setTransform(SphereT3D);
// Position der Kamera festlegen
CamT3D.setTranslation(new Vector3f(0.0f, 0f, -distanceToCenter - 20));
CamTG.setTransform(CamT3D);
//CamTG.addChild(centerRotator);
// TODO: wieder entfernen wenn Planeten Kamera funktioniert
CamTG.addChild(new Sphere(0.2f, Sphere.GENERATE_NORMALS
| Sphere.GENERATE_TEXTURE_COORDS, 64, new Appearance()));
CamTG.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
CamTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
CamTG.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
CamTG.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
// eigene Rotation festlegen
SphereTG.addChild(ownRotTG);
SphereTG.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
ownRotTG.addChild(sphere);
ownRotTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
ownRotTG.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
ownRotTG.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
//ownRotTG.setCapability(TransformGroup.);
SphereTG.addChild(ownRotator);
// Rotation ums Zentralgestirn
CenterTG.addChild(centerRotTG);
centerRotTG.addChild(SphereTG);
centerRotTG.addChild(CamTG); // Kamera um ZG rotieren
centerRotTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
CenterTG.addChild(centerRotator);
CenterTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
CenterTG.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
CenterTG.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
// Oribts entfernen/ hinzufügen
CenterTG.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
}
///////////////////////////////////////////////////////////////////////////////////////////////
private void initRotations()
{
// Eigenrotation
ownRotationAlpha = new Alpha(-1, // loopCount -1 = endlos
/*Alpha.DECREASING_ENABLE|*/Alpha.INCREASING_ENABLE, // mode, welche Parts sollen durchlaufen werden
0, // triggerTime, wann soll Rotation beginnen 0=sofort
0, // phaseDelayDuration, wann soll Rotation beginnen 0=sofort
//increasing Part
(int) ownRotDuration, // increasingAlphaDuration in msec (Rotationsgeschwindigkeit)
0, // increasingAlphaRampDuration Beschleunigung (brauch ich net)
0, // alphaAtOneDuration
// decreasing Part / wird nicht betrachtet wenn nur INCREASING_ENABLE gesetzt
9000, // decreasingAlphaDuration
3000, // decreasingAlphaRampDuration
500); // alphaAtZeroDuration
ownRotator = new RotationInterpolator(ownRotationAlpha, ownRotTG);
ownRotator.setMinimumAngle(0f);
ownRotator.setMaximumAngle((float) Math.toRadians(360));
ownRotator.setSchedulingBounds(Universe.BigBounds);
// Rotation ums Zentralgestirn
centerRotationAlpha = new Alpha(-1, // loopCount -1 = endlos
//Alpha.DECREASING_ENABLE|
Alpha.INCREASING_ENABLE, // mode, welche Parts sollen durchlaufen werden
0, // triggerTime, wann soll Rotation beginnen 0=sofort
0, // phaseDelayDuration, wann soll Rotation beginnen 0=sofort
//increasing Part
(int) centerRotDuration, // increasingAlphaDuration in msec (Rotationsgeschwindigkeit)
0, // increasingAlphaRampDuration Beschleunigung (brauch ich net)
0, // alphaAtOneDuration
// decreasing Part / wird nicht betrachtet wenn nur INCREASING_ENABLE gesetzt
9000, // decreasingAlphaDuration
3000, // decreasingAlphaRampDuration
500); // alphaAtZeroDuration
centerRotator = new RotationInterpolator(centerRotationAlpha, centerRotTG);
centerRotator.setMinimumAngle(0f);
centerRotator.setMaximumAngle((float) Math.toRadians(360));
//TODO: ist hier bigbounds nötig?, wofür steht schedulingBounds?
centerRotator.setSchedulingBounds(Universe.BigBounds);
}
///////////////////////////////////////////////////////////////////////////////////////////////
public void addChild(Planet planet)
{
SphereTG.addChild(planet.createSceneGraph());
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Erstellung des Planeten
public BranchGroup createSceneGraph()
{
System.out.println("createPlanet: " + (i++));
RootBG.addChild(CenterTG);
RootBG.compile();
return RootBG;
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
public TransformGroup getCamTG()
{
return CamTG;
}
}
[/highlight]
Die folgendene Klasse stellt meine Kamera da. im Konstruktor wird die ViewPlatformTransform übergeben
---->u.getViewingPlatform().getViewPlatformTransform()
[highlight=Java]
public class Camera extends BranchGroup
{
private TransformGroup transformGroup;
private Transform3D ViewT3D = new Transform3D();
/////////////////////////////////////////////////////////////////////////////////
public Camera(TransformGroup tg)
{
transformGroup = tg;
enablePlanetCamera(ObjectManager.planetErde);
}
/////////////////////////////////////////////////////////////////////////////////
public void enablePlanetCamera(Planet currentPlanet)
{
// aktuelle Transformation des Planeten holen
TransformGroup ViewTG = currentPlanet.getCamTG();
ViewTG.getTransform(ViewT3D);
// Rotation der Kamera um 180 grad auf die Sonne
ViewT3D.setRotation(new AxisAngle4f(0, -1, 0, (float) Math.toRadians(180)));
transformGroup.setTransform(ViewT3D);
}
}
[/highlight]
hoffe das erschlägt euch nicht und es ist einigermaßen verständlich :bahnhof:
Zuletzt bearbeitet von einem Moderator: