Skip to main content

Model parts position problem

No replies
Baly
Offline
Joined: 2011-08-20
Points: 0

Hi there,

I am loading .obj model, which I scatter to his parts and I add each part into one TransformGroup and when all parts have their TransformGroup I put all the TransformGroups into one, so I can move them all at the same time if I want, but can move only one aswell. It all works fine but when I want to rotate parts of the model it gets bad. The problem is that I cannot put the parts into 0,0,0 position, because it somehow has deffault position as it makes the whole model. If I get position of one parts it says it is 0,0,0 , but it is clearly not as it is positioned somewhere to make the model look as it was designed. I simply try to be able to position all the parts of the model on one place (make it a pile of parts). I was investigating where could be the problem and I got to conclusion that it does the Geometry of each part (shape). Because when I created new Shape3D and made its Geometry same as the model part's, it had the same positioning as the model part but without the Appearance. That might be a problem because what I read, there is no chance in changing the geometry. Am I right or is it doing something else? Can it be done somehow?

Here is the important part of my code:

import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.filechooser.FileFilter;
import javax.swing.*;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

group.detach();
model = file;
modelobject = new ObjectFile(ObjectFile.RESIZE);
try {
modelscene = modelobject.load(model.getPath());
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(frame, "File not found!", "Error", JOptionPane.ERROR_MESSAGE);
} catch (IncorrectFormatException ex) {
JOptionPane.showMessageDialog(frame, "Problem with file format!", "Error", JOptionPane.ERROR_MESSAGE);
} catch (ParsingErrorException ex) {
JOptionPane.showMessageDialog(frame, "Problem with file loading!", "Error", JOptionPane.ERROR_MESSAGE);
}
Transform3D modeltrans = new Transform3D();
Vector3d modelvec = new Vector3d(0.0,0.0,-2.0);
modeltrans.setTranslation(modelvec);
modelmap = modelscene.getNamedObjects();
Transform3D temptrans = new Transform3D();
temptrans.setTranslation(new Vector3d());

for (String name : modelmap.keySet()) {
modeltransmap.put(name, new TransformGroup());
((BranchGroup)modelmap.get(name).getParent()).removeChild(modelmap.get(name));
((TransformGroup)modeltransmap.get(name)).setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
((TransformGroup)modeltransmap.get(name)).setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
((TransformGroup)modeltransmap.get(name)).setTransform(temptrans);
((TransformGroup)modeltransmap.get(name)).addChild(modelmap.get(name));
modeltransg.addChild(((TransformGroup)modeltransmap.get(name)));

}

modeltransg.setTransform(modeltrans);

group.addChild(modeltransg);

universe.addBranchGraph(group);