Incorrect position in attaching spatial


#1

Node vehicle as root of vehicle;
car model (spatial) load and attached to vehicle node.
I used some Node (as seats) to determine positions of seats and in constructor of vehicle class I initialized their positions by setLocalTranslation().
when a character attached to seat (get on vehicle) have correct position if vehicle rotation is zero.
if vehicle have rotation then all seats moved some unit in attaching character spatial to it.

I dont know how to fix it…


#2

Hello @yn97, can you provide us with a before and after screenshot of the issue, I am having trouble visualizing the problem, but I think I understand what you are saying.

Are you changing the position of the nodes after rotating?


#3

attaching without rotate vehicle: that is correct

attaching new characters to vehicel after rotating: that have incorrect positions

no, I set the node positions in constructor method.


#4

Are the character nodes attached to the vehicle node? It looks like the vehicle node was rotated, but the other nodes are not attached to it, thus did not get rotated.


#5

character nodes attached to seat nodes, seat nodes attached to vehicle.


#6

Hmm, I see that the characters have rotated along their center axis, but the vehicle itself is not rotated along its center axis? Without seeing the code this will be hard to trouble shoot.


#7

the problem is not rotation, is position.


#8

There is a trick to this. I remember this issue when I was adding joints to a car. When you add a node it acts as if the car has not rotated. I can’t remember if it was worldToLocal(Vector3f) or something, but there is a method that calculates this for you. If I remember i’ll post it.


#9

it was not a rotation problem.
I found that when a node have parent(root Node) attached to another node it keeps last local Translation.
i don’t know it is a bug or feature? and how can I fix it?


#10

Spatials remembering their local translation is a feature. When parenting in the way you are describing be sure to set the new local translation you wish to use at the time you assign the new parent. Setting local translation before or after giving the spatial a parent is very common practice and is something you should plan to utilize given situations where you may want to “prep” a group of spatials before adding them to a scene.


#11

Just going to point these out in case they are relevant here:
https://javadoc.jmonkeyengine.org/com/jme3/scene/Spatial.html#worldToLocal-com.jme3.math.Vector3f-com.jme3.math.Vector3f-
https://javadoc.jmonkeyengine.org/com/jme3/scene/Spatial.html#localToWorld-com.jme3.math.Vector3f-com.jme3.math.Vector3f-


#12

is it a bad way if I set character position according to seat position every frame instead of attach them to seat node?


#13

You can ignore problems if you like. It’s your game. Only you care about the quality of your code.


#14

That being said, if, for example, you game was structured with the physics engine on the server, you’d effectively be setting the world position of the riders every frame on the client. Not much difference.

But if your spatials are you game objects (as I suspect they are) then it is a little hacky and may lead to some weird timing bugs down the road as you forget which thing is updated when.


#15

is attaching to a node and move with it More optimal than setting position every frame?


#16

Yes. But not because of the manual setting, but because it may cause unforetold problems as you progress.


#17

Respectfully, it may also be a learning opportunity to at least work through getting the attachment working.

This whole thread is a sign that there is some (maybe small?) confusion about how scene graphs are supposed to work. Working through the issue may teach some valuable skills for the next problem.

Being able to convert to and from local reference reference frames is a very helpful thing to be able to do in general.