Entity not returning to original position

Hello,

I am making a RealityKit app where a ball goes and hits some stones, and then goes back to its original position.

However, in moving back the ball always tilts a little and sometimes even goes backwards or in other directions!

I add my entities in these lines of code:

            stoneEntity!.scale = [5, 5, 5]
            anchor.addChild(stoneEntity!)
            stoneEntity!.transform.translation = SIMD3<Float>(anchor.position.x, anchor.position.y, anchor.position.z)
            stoneX = stoneEntity!.position.x
            stoneY = stoneEntity!.position.y
            stoneZ = stoneEntity!.position.z

            stoneEntity?.generateCollisionShapes(recursive: true)

          // Add ball entity  
            anchor.addChild(ballEntity)
            ballEntity.transform.translation = SIMD3<Float>(anchor.position.x, anchor.position.y, anchor.position.z + 1)
            ballEntity.generateCollisionShapes(recursive: true)
            ballEntity.physicsBody = PhysicsBodyComponent(massProperties: PhysicsMassProperties.default, material: PhysicsMaterialResource.default, mode: .kinematic)
            ballEntity.collision = CollisionComponent(shapes: [.generateSphere(radius: 0.1)], mode: .default, filter: .default)
            ballEntity.physicsBody?.isTranslationLocked = (true, true, false)

view.scene.addAnchor(anchor)

And move my ball back in the code:

ballEntity.transform.translation = SIMD3<Float>(0, 0, 1)

If anyone has any ideas please could you let me know ASAP?

Thank you!

Accepted Reply

For anyone who is having the same issue, I fixed it by using ballEntity.move() instead and putting nothing as the rotation -

let newTranslation = SIMD3<Float>(0, 0, 1)
let moveController = ballEntity.move(to: Transform(scale: simd_make_float3(1, 1, 1), translation: newTranslation), relativeTo: ballEntity.self, duration: 0.2)

whereas previously (not working) I was doing this:

let newTranslation = SIMD3<Float>(0, 0, 1)
let moveController = ballEntity.move(to: Transform(scale: simd_make_float3(1, 1, 1), rotation: anchor.orientation, translation: newTranslation), relativeTo: ballEntity.self, duration: 0.2)

Removing the rotation: anchor.orientation helped as my anchor was a bit tilted or moving.

Replies

For anyone who is having the same issue, I fixed it by using ballEntity.move() instead and putting nothing as the rotation -

let newTranslation = SIMD3<Float>(0, 0, 1)
let moveController = ballEntity.move(to: Transform(scale: simd_make_float3(1, 1, 1), translation: newTranslation), relativeTo: ballEntity.self, duration: 0.2)

whereas previously (not working) I was doing this:

let newTranslation = SIMD3<Float>(0, 0, 1)
let moveController = ballEntity.move(to: Transform(scale: simd_make_float3(1, 1, 1), rotation: anchor.orientation, translation: newTranslation), relativeTo: ballEntity.self, duration: 0.2)

Removing the rotation: anchor.orientation helped as my anchor was a bit tilted or moving.