0

I was wondering if anyone knew if you perform a SCNAction on a node, is there a way while that node is executing the action, where you could find out what position it's in the scenes coordinate system?

Lets say I have two SCNNodes A and B...

SCNNode *A, *B;

and I want to run a SCNAction for A

A.position = SCNVector3Make(0.0,0.0,0.0);
[A runAction:[SCNAction moveTo:SCNVector3Make(10.0,0.0,0.0)]];

and while A was running its action, have B follow it in real time?

[B runAction:[SCNAction repeatActionForever:[SCNAction moveTo:A.position]]];

So every time B repeats its given a new updated position of A to move to.

The problem I'm getting is, even if I set the presentationNode of A to the SCNAction above, It's still returning the initial input that I set (which makes sense) but how do I get the updated position of A?

Side Note: When I take A.presentationNode.position I get the default Zero Vector

SOLVED QUESTION

After further research, I found out how to use the SCNSceneRendererDelegate and then created a (void) method which updated the position of node A and then within the same method I had a SCNAction which moved B to the update position of A.

If anyone had a similar issue or would just like to see the code, let me know and I'll post it!

Chris
  • 21
  • 4

2 Answers2

0

A.position is evaluated once and only once, when you create the action:

[SCNAction moveTo:A.position]

There is no block involved here that gives you a chance to customize the action as time passes.

mnuages
  • 13,049
  • 2
  • 23
  • 40
  • So, would it be possible to create a customized action block to be able to compute what position the node is at, at any given moment in time? – Chris May 04 '15 at 19:36
0

I know this is an old question, but you could have used a SCNDistanceConstraint with a distance of zero.

You could also simply have created a property on B that you assign A to. Then, for B, use a custom action to access that. e.g.

class BNode : SCNNode
   var ANodeInstance : SCNNode? = nil

   init(withANode: SCNNode) {
     super.init()
     self.ANodeInstance = withANode
   }
}

B.runAction(SCNAction.repeatActionForever(
    SCNAction.customAction(duration: duration, action: { (node, timeElapsed) in
        let selfAsBNode = node as! BNode
        selfAsBNode.position = selfAsBNode.ANodeInstance.position
    })

Sorry, it's in Swift not Objective-C but it's pretty easy to translate back.

This sort of thing is ugly as blazes, but Apple haven't really provided any clean way to communicate state through a chain of actions.

PKCLsoft
  • 1,359
  • 2
  • 26
  • 35