moving object relative to rotation

Hello, I have a problem, I have no idea how to move an object relative to its rotation. I rotate an object using "SCNAction.rotateBy" and move it "SCNAction.move". I need to make it so that after rotation at a certain angle, the object moves in a given direction of rotation. Example: We have a spacecraft in 3 dimensional dimensions, I turn it to a certain angle "a" (suppose 18 degrees), after which I press the button and it starts moving in the direction of this ship. I ask for your help.

var YRotate = CGFloat(0 * Double.pi)
    func Rotate(){ // rotate ship
        if stopRotate == true{
          YRotate = CGFloat(2 * Double.pi)
        }else{
          YRotate = CGFloat(-2 * Double.pi)
        }
       let action = SCNAction.rotateBy(x: 0, y: YRotate, z: 0, duration: 10)
       let repAction = SCNAction.repeatForever(action)
        Swift.print(ShipSpaceNode.rotation)
        ShipSpaceNode.runAction(repAction)
        }


 var x = 0 

    var xy = 0
    func Click(){ // moving ship
        if stop == true{
        x = 1
        xy = 1
        } else{
        x = 0
        xy = -1
        }
        Swift.print(ShipSpaceNode.position)
        let koord = SCNVector3(Float(xy), 0 , 0) 

      let moveUp = SCNAction.move(by: koord, duration: 5)
       let hoverSequence = SCNAction.sequence([moveUp]) 

      let loopSequence = SCNAction.repeatForever(hoverSequence) 

        
      ShipSpaceNode.runAction(loopSequence) 
           }

Replies

Hello,


The question here is not entirely clear to me, but it sounds like you need to go over the fundamentals of 3D graphics (i.e. transformation matrices, trigonometry, linear algebra, quaternions, vectors), there are a ton of resources out there on the web about this topic!

You did not provode much information to allow analysis:


var YRotate = CGFloat(0 * Double.pi)

func Rotate(){ // rotate ship
    if stopRotate == true{
        YRotate = CGFloat(2 * Double.pi)
    }else{
        YRotate = CGFloat(-2 * Double.pi)
    }
    let action = SCNAction.rotateBy(x: 0, y: YRotate, z: 0, duration: 10)
    let repAction = SCNAction.repeatForever(action)
    Swift.print(ShipSpaceNode.rotation)
    ShipSpaceNode.runAction(repAction)
}

var x = 0
var xy = 0

func Click() { // moving ship
    if stop == true{
        x = 1
        xy = 1
    } else{
        x = 0
        xy = -1
    }
    Swift.print(ShipSpaceNode.position)
    let koord = SCNVector3(Float(xy), 0 , 0)
   
    let moveUp = SCNAction.move(by: koord, duration: 5)
    let hoverSequence = SCNAction.sequence([moveUp])
   
    let loopSequence = SCNAction.repeatForever(hoverSequence)
   
    ShipSpaceNode.runAction(loopSequence)
}


Where is Rotate() called ?

You call for 2π rotations ; do you want to perform a complete 360° rotation ?


Note: var names (YRotate) as well as func names (Rotate()) should start with lowercase, per Swift conventions.