Correcting the coordinate origin of a loaded model?

Hi there!
(First post, yey! :D)

So, I have a .j3o object, where the actual geometry is not located at the coordinate origin (0/0/0) but rather far away from that (like 20/15/27). Between the actual geometry inside the j3o and the coordinate origin, there is void.

The problem now with rotating the Spatial on y-axis obviously: not only the model was rotated, but moved all along the scene.

I searched for a fix about an hour (some kind of calculating the hitbox of the object and then move it to the coordinate origin), but I found, that I only need to attach the Spatial to a Node:

				Node n = new Node();
				n.attachChild(loadedModel);
				loadedModel.addControl(new C_Spinner());
				rootNode.attachChild(loadedModel);

So, by just adding the Spatial to a Node, the Model appeared exactly at 0/0/0 and rotated accordingly.

My question is:
how is this achieved? Is this intended or just a side-effect?
I tried to dig deep into the ā€œattachChild(Spatial)ā€ method, but couldnt find the answer.
Thanks for your answers in advance :slight_smile:
Greetings from Germany!

1 Like

You can edit the .j3o in the scene composer and change it, or attach it to a parent node and then move the parent node.

In my project (an android application) a user can load ANY model for showing it augmented in the live-camera picture. The problem here is, that I have to fix the off-center coordinate-issue in code automaticaly.
But as I said in my first post, I found the solution by simply adding the Spatial (Model) to a Node, without any further adjustments or movements.
Iā€™d like to understand, why this fixes the problem.

@breytex

Well because the default position of the model when its added to the node is 0,0,0.

Itā€™s position is local to itā€™s parent node. The default being 0,0,0.

Take a look at boundingbox and sphere,

get the one, and center it based on that from your models main spatial.

@Big bob
Please reread my first post.
When importing the Model, the scene looks like this:
wrong behaviour
The Model itself obtains empty space so the imported model is at 0/0/0, but the ā€œrealā€-Model (the chair) is not.

When using the code from post #1, the result looks like this:
correct bahivour

This has nothing todo with the model being added to 0/0/0. There must be some line of code in the attachChild which trims the empty-space in the Modelā€¦
Thats my question. How is this implemented?

1 Like

The children of the loaded node must be offset. You canā€™t just get rid of this space because then all of the children might be on top of each other (ie: the back of the chair may be separate elevated from the chair, etc.) you donā€™t know which ā€˜spaceā€™ is supposed to be there and which was a mistake when the model was made.

Putting it into a node and reoffsetting is all you can do, reallyā€¦ other than making your users make good models to begin with.

But his question was, how comes that the model is chentered, even if you only wraps it in a parent node without adding a manual local translation? What magic does the node do with its children that the furniture piece suddenly rotates arount its correct center?

				Node n = new Node();
				n.attachChild(loadedModel);
				loadedModel.addControl(new C_Spinner());
				rootNode.attachChild(loadedModel);

what do you do with the created node n?

it is not attached to the scene at all, am I right?

but you add your loaded model to itā€¦

sooo

could it be a sideffect of your control?

greetings from germany tooā€¦

@simon.heinen said: But his question was, how comes that the model is chentered, even if you only wraps it in a parent node without adding a manual local translation? What magic does the node do with its children that the furniture piece suddenly rotates arount its correct center?

Yeah, wellā€¦ we donā€™t see enough code then. We donā€™t know where loadedModel comes from but the only way attaching it to a node effects anything is if it used to have a different parent, ie: it wasnā€™t the root of the model.

re-reading the question and looking at the code, I realize a mistake in my statement.

basically you attach the model to n and then attach it to the scene.

thus should indeed change nothingā€¦

I would suggest debugging the code and investigate world and local transformā€¦