Post

Replies

Boosts

Views

Activity

Reply to Rotate an entity in VisionPro
Thanks a lot!!!!! Is it possible to rotate in both axes? I tried to duplicate the movement for both axes (changing .x and .y where appropriate) but it only seems to rotate in the second axis I'm sure I made a mistake but I cannot find it: .gesture( DragGesture() .targetedToAnyEntity() .onChanged { value in let entity = value.entity let orientation = Rotation3D(entity.orientation(relativeTo: nil)) let newOrientationX: Rotation3D let newOrientationY: Rotation3D if (value.location.x >= lastGestureValueX) { newOrientationX = orientation.rotated(by: .init(angle: .degrees(2.0), axis: .y)) } else { newOrientationX = orientation.rotated(by: .init(angle: .degrees(-2.0), axis: .y)) } entity.setOrientation(.init(newOrientationX), relativeTo: nil) lastGestureValueX = value.location.x if (value.location.y >= lastGestureValueY) { newOrientationY = orientation.rotated(by: .init(angle: .degrees(2.0), axis: .x)) } else { newOrientationY = orientation.rotated(by: .init(angle: .degrees(-2.0), axis: .x)) } entity.setOrientation(.init(newOrientationY), relativeTo: nil) lastGestureValueY = value.location.y } )
Mar ’24
Reply to Rotate an entity in VisionPro
I found the problem. The orientation needed to be a var and reset to the current orientation between moving the second axis. DragGesture() .targetedToAnyEntity() .onChanged { value in let entity = value.entity var orientation = Rotation3D(entity.orientation(relativeTo: nil)) var newOrientation: Rotation3D // let newOrientationY: Rotation3D if (value.location.x >= lastGestureValueX) { newOrientation = orientation.rotated(by: .init(angle: .degrees(1.0), axis: .y)) } else { newOrientation = orientation.rotated(by: .init(angle: .degrees(-1.0), axis: .y)) } entity.setOrientation(.init(newOrientation), relativeTo: nil) lastGestureValueX = value.location.x orientation = Rotation3D(entity.orientation(relativeTo: nil)) if (value.location.y >= lastGestureValueY) { newOrientation = orientation.rotated(by: .init(angle: .degrees(1.0), axis: .x)) } else { newOrientation = orientation.rotated(by: .init(angle: .degrees(-1.0), axis: .x)) } entity.setOrientation(.init(newOrientation), relativeTo: nil) lastGestureValueY = value.location.y } )
Mar ’24
Reply to Rotate an entity in VisionPro
Hi, Yes, I did. I checked with the debugger, and it goes through that line for each attachment. Note that if I use my "old" gesture code, it rotates along some other axis (I'm not sure which one the attachment moves> Here is the other code, which rotates the whole thing including the attachments Baffling .gesture( DragGesture(minimumDistance: 0.0) .targetedToAnyEntity() .onChanged {value in let location3d = value.convert(value.location3D, from: .local, to: .scene) let startLocation = value.convert(value.startLocation3D, from: .local, to: .scene) let delta = location3d - startLocation rotateByX = Double(atan(delta.x * 0.1)) rotateByY = Double(atan(delta.y * 0.1)) currentrotateByX = currentrotateByX + rotateByX currentrotateByY = currentrotateByY + rotateByY } )
Mar ’24
Reply to Rotate an entity with the attachments
Hi, Here is my code. I attach it with scene.addChild(Left_Hemisphere) Should I use a different rotation method? The rotation of the main entity works well. But the attachment does not move. I used this rotation code (got it from another post here https://developer.apple.com/forums/thread/748192). It works well and it rotates the entity over its axes. @State private var lastGestureValueX = CGFloat(0) @State private var lastGestureValueY = CGFloat(0) var body: some View { RealityView { content, attachments in // Add the initial RealityKit content if let scene = try? await Entity(named: "Immersive", in: realityKitContentBundle) { content.add(scene) scene.generateCollisionShapes (recursive: true) scene.components.set(InputTargetComponent()) if let Left_Hemisphere = attachments.entity(for: "Left_Hemisphere") { //4. Position the Attachment and add it to the RealityViewContent Left_Hemisphere.position = [-0.5, 1, 0] scene.addChild(Left_Hemisphere) } } } attachments: { Attachment(id: "Left_Hemisphere") { //2. Define the SwiftUI View Text("Left_Hemisphere") .font(.extraLargeTitle) .padding() .glassBackgroundEffect() } } .gesture( DragGesture() .targetedToAnyEntity() .onChanged { value in let entity = value.entity var orientation = Rotation3D(entity.orientation(relativeTo: nil)) var newOrientation: Rotation3D if (value.location.x >= lastGestureValueX) { newOrientation = orientation.rotated(by: .init(angle: .degrees(0.5), axis: .y)) } else { newOrientation = orientation.rotated(by: .init(angle: .degrees(-0.5), axis: .y)) } entity.setOrientation(.init(newOrientation), relativeTo: nil) lastGestureValueX = value.location.x orientation = Rotation3D(entity.orientation(relativeTo: nil)) if (value.location.y >= lastGestureValueY) { newOrientation = orientation.rotated(by: .init(angle: .degrees(0.5), axis: .x)) } else { newOrientation = orientation.rotated(by: .init(angle: .degrees(-0.5), axis: .x)) } entity.setOrientation(.init(newOrientation), relativeTo: nil) lastGestureValueY = value.location.y } ) } }
Mar ’24