#How to spawn another entity as if it were a child on another entity without making it a child

15 messages · Page 1 of 1 (latest)

iron bluff
#

The question is worded pretty bad but what I want to do is pretty simple, I just can't figure it out. I have a vehicle entity and I want to spawn its wheels. I have 4 offset vec3 to tell me where to spawn the wheel, previously I was just spawning the wheel at the vehicles translation plus the offset rotation. But this doesn't account for the vehicle's rotation. I can't make the wheels children of the vehicle because it would mess with the physics. How can I achieve this?

bitter kindle
#

This isn't currently achievable atm, transform propagation is mandatory for parent/child. This will be solved more with relations 🌈 in the future but for now I just use a separate component to mark Attach::Translation(Entity) personally

iron bluff
#

The physics joints handle moving the wheel with the vehicle I just can't figure out the math behind where to spawn the wheel relative to the vehicle when there is a rotation invloved

bitter kindle
#

if you are using rapier the joints should be relative to the collider you are attaching to

#

so you shouldn't need to worry about rotation in that case

#

like in my project I have

    let mut hand_joint = SphericalJointBuilder::new()
        .local_anchor2(Vec3::new(0.0, arm_radius, 0.0))
#

you specify the anchors on each physics objects own local orientation

iron bluff
#

If the wheels aren't where they are ment to be the joint launches them into place making all the vehicles get launched

wooden parcel
#

Is the vehicle already existing? Or is the entire thing spawned at once?

#

If it's spawned at once, you could spawn it as a child with inactive physics and a marker, and then remove the parent-child relationship the next frame along with removing the marker and activating physics

#

If it exists already, you can use the global transform on the vehicle to transform a point in "vehicle" space to world space

#

(note that if the vehicle doesn't have a parent, you can do the same with a normal transform component, and then you can spawn them together using a clone of the original transform)

iron bluff
#

This works but if there is a better way let me know

transform.right() * wheel.position.x
   + transform.up() * wheel.position.y
   + transform.back() * wheel.position.z
   + transform.translation,

The resulting transform is what I assign to the wheel entity and the transform variable in the code belongs to the vehicle

wooden parcel
#

That method is the inverse of what you need - it takes something from global space to local space