Problem copying global rotation and position of an object to another world
Asked Answered
C

13

0

I am having a problem where I have the following objecst in a world3d:

Node3D: ( rotated in Y 90 degrees )
-> Several child nodes3D with diferent rotations and positions

I want to send the rotation and positions to another world3D ( an minimap )
Before I have rotation in the parent Node3D everything works normal, but when I add rotation to the node3D, for some reason the objects in the minimap world3d dont get its rotation and move, insted of from left to rign, to up and down.
I am sending the information to the new world as a signal, with the vectors :

signal Sig_UpdateMiniMap( objs_pos: Array[ TVector3 ] , obs_rot: Array[ TVector3 ] )

I am feeding it with this :

	for node in get_tree().root.get_child(0).get_children():
		if( utils.mySubstr(node.name,1,4)=="Obj_" ):
			var carmesh: Node3D = node.get_node("ObjMesh")
			pos:Vector3 = carmesh.transform.origin
			rot:Vector3 = carmesh.global_transform.basis.get_euler()			
			arraypos.append( pos )
			arrayrot.append( rot )

And setting in the minimap world3d with :

newobj.global_transform.basis.from_euler( obj.rot )
newobj.transform.origin = obj.pos

Nothing I tried worked... What I am doing wrong ?

Cathcart answered 30/5, 2023 at 22:35 Comment(0)
P
0

Cathcart I cant take the parent position because its fixed, it dont move in my case, about the global space, I though I was doing that with the global_transform, there is another way to get the globals ?

I didn't say to take parent's position, but to take position in parent's (or global) space.

Your code takes rotation in global space, but position in local. Take both in global space.

Instead:

pos:Vector3 = carmesh.transform.origin

Try:

pos:Vector3 = carmesh.global_transform.origin

If you need more specific answer post your exact scene structure, so we can see where in the scene hierarchy are source and target coordinate spaces located.

Philippians answered 31/5, 2023 at 7:16 Comment(0)
P
0

Cathcart Take the position in Node3D's parent space or global space.

Philippians answered 30/5, 2023 at 23:25 Comment(0)
C
0

Philippians I cant take the parent position because its fixed, it dont move in my case, about the global space, I though I was doing that with the global_transform, there is another way to get the globals ?

Cathcart answered 31/5, 2023 at 2:9 Comment(0)
C
0

Just to give a bit more context/detail, I have the following structure :
ParentNode3D -> This have a initial y rotation for map placement
/ RigidBody3D -> this move arround
/ Mesh -> I change the position and rotation of this mesh with the rigidbody pos and rot.

I cant also put the initial rotate in the mesh because if I do it I need to rotate also all the other meshes and rigidbodys manually.

The ParentNode keeps in the same rest position all the time, since is the rigidbody that moves.
The odd is that if the parentnode has no rotation the minimap shows fine, and if it has, for example 180 rotation in Y, then objs in minimap moves in the opposite direction that it should.

Cathcart answered 31/5, 2023 at 2:23 Comment(0)
P
0

It's better to work with local rotations / positions and do the transformation yourself. Then you can get whatever you want.

For example, if you pull the Basis from the children most object (it's local rotation) you can take a blank basis and multiply it to reconstruct the original. If it is nested, and each level has a different rotation, then you need to save all those rotations (the basis) and multiply them in order to reconstruct the whole one. Translation is easier, as it is just addition of vector 3 values.

Portuguese answered 31/5, 2023 at 2:37 Comment(0)
C
0

Portuguese I am not sure I understood, you said to create an blank basis, then multiply the rotation from the parentnode, then multiply it by the local rotation from the obj ?
Another aprouch I was thinking is to use PhysicsServer3D to at the first frame, rotate the physicbody to the rotation of the parentnode, and then set the parentnode to 0,0,0 , is it a bad idea ?

Cathcart answered 31/5, 2023 at 3:18 Comment(0)
P
0

You shouldn't move / rotate physics bodies manually. It will break the physics engine.

Portuguese answered 31/5, 2023 at 3:23 Comment(0)
P
0

Cathcart I cant take the parent position because its fixed, it dont move in my case, about the global space, I though I was doing that with the global_transform, there is another way to get the globals ?

I didn't say to take parent's position, but to take position in parent's (or global) space.

Your code takes rotation in global space, but position in local. Take both in global space.

Instead:

pos:Vector3 = carmesh.transform.origin

Try:

pos:Vector3 = carmesh.global_transform.origin

If you need more specific answer post your exact scene structure, so we can see where in the scene hierarchy are source and target coordinate spaces located.

Philippians answered 31/5, 2023 at 7:16 Comment(0)
P
0

Note, even with the global transform, that is the global transform of that scene, not the other one. So some transform may still be needed.

Portuguese answered 31/5, 2023 at 7:58 Comment(0)
P
0

It's not that complicated, but you have to understand how coordinate spaces work. This was the simplest thing I could find.

https://tarini.di.unimi.it/teaching/masterGameDev2019/02_transforms.pdf

Portuguese answered 31/5, 2023 at 8:2 Comment(0)
P
0

Portuguese Note, even with the global transform, that is the global transform of that scene, not the other one. So some transform may still be needed.

Yeah, but it really depends on the setup. If transform is taken from global space and assigned inside map's local space, it may be ok since map's local space could implicitly be doing needed transforms. Hard to tell precisely without seeing the scene.

Philippians answered 31/5, 2023 at 8:11 Comment(0)
C
0

Philippians Thanks it works now, Its kind counterintuitive, but it works, when I change the transform to also global_transform in the position as well. Thanks for also for Portuguese for the documentation, I will do some reading !

Cathcart answered 31/5, 2023 at 16:57 Comment(0)
P
0

Cathcart Thanks it works now, Its kind counterintuitive

Why do you think it's counterintuitive? Transformation is taken in the global space (so, as it appears in the world), and transferred to another space. It's quite intuitive. I'm curious why, in the first place, you took rotation and translation from different transforms, one from global and other from local?

There's also no need to separate translation and rotation or go to/from euler for rotations. You can simply send object's whole global transform to the map, and let the map apply it to the corresponding object.

Philippians answered 31/5, 2023 at 18:15 Comment(0)
C
0

Philippians I tried diferent things, and I guess the code I copy here was the last try, I had the impression I had tried the global_transform but it seems somehow I didnt. I guess I am just spent too much hours on this at the weekend ...

Cathcart answered 31/5, 2023 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.