I have a node that have a constraint; to be more specific, it has an SCNLookAtConstraint :
let constraint = SCNLookAtConstraint(target: scene.retriveNodeFromScene(cameraNodeName))
constraint.gimbalLockEnabled = false
self.constraints = [constraint]
But when I want to move node to other coordinates it just doesn't move. I don't receive any error, just nothing happens. I have made some tests, and if I remove constraint, everything is ok, node moves with no problem. So, what should I do? Is there any solution for my problem or should I find another way, equivalent with my constraint, but also compatible with SCNAction?
code for action:
let timeForAction = calculateTimerForSecoundAction()
let action = SCNAction.moveTo(calculateEndingLocation(), duration: timeForAction)
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC * timeForAction))
self.runAction(action)
explodeAfterReachingFinalPosition(timeForAction)
you could create a new intermediate node that you'll use for the action, and have your current node as a child node with the lookAt constraint.
Same issue as the original poster.
I solved it with MNAUGES suggestion.
The 'LookAt' constraint breaks/prevents the SCNActions from happening in the view. Interestingly, the camera node WAS moving - according to the console - just not updating in the view. I could also move the camera node in the view with a change to cameraNode.position = newPosition - but without the nice animated transition.
Parent your cameraNode to an emptyNode. Assign the lookAt constraint to the cameraNode and the moveTo SCNAction to the emptyNode.
Related
I'm currently programming an AR-App, which starts with placing a SCNNode (table). First the app searches for a horizontal plane and whenever a surface was found, the object is shown, but not placed yet. While it is not placed, I want the object to face the camera at all times, but I'm having trouble to find the current position of the camera and also updating the object every frame.
Currently, the object is facing the world coordinates. So when I start the app and a horizontal plane was found - the object appears and faces to the starting point of the world coordinates (wherever I started the app)
Can anybody help me, how to get the vector from the camera position and make the object updates it's direction every frame?
var tableNode : SCNNode! // Node of the actual table
var trackingPosition = SCNVector3Make(0.0, 0.0, 0.0)
let trans = SCNMatrix4(hitTest.worldTransform)
self.trackingPosition = SCNVector3Make(trans.m41, trans.m42, trans.m43)
self.tableNode.position = self.trackingPosition
The functionality you are trying to implement is called billboarding. To achieve this effect you need to set the SCNLookAtConstraint using the scene's .pointOfView as the target.
you can remove
self.trackingPosition = SCNVector3Make(trans.m41, trans.m42, trans.m43)
self.tableNode.position = self.trackingPosition
and use:
// set constraint to force the node to always face camera
let constraint = SCNLookAtConstraint(target:sceneView.pointOfView)
constraint.isGimbalLockEnabled = true
self.tableNode.constraints = [constraint] // constraint can cause node appear flipped over X axis
You should implement ARSCNViewDelegate and use the renderer method renderer(_:updateAtTime:) where you every frame get the camera position from sceneView.pointOfView. This allows you to then use the SCNNode method look(at:) to transform the nodes coordinate system towards the camera.
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
if let camera = sceneView.pointOfView {
tableNode.look(at: camera.worldPosition)
}
}
I'm familiar with swift but I've started dabbling my hand in spritekit. I've been following a tutorial about created an endless runner. The approach taken by the author is to create a single SKNode that contains all the children. This container node is then moved rather than moving all the child Nodes individually. But what's got me stumped is that the container node doesn't have a size associated with it so I'm a bit confused as to how/why this works and the author doesn't really explain it.
So we have
let containerNode = SKNode()
let thePlayer = Player("image":"player") //SKSpriteNode
let inc = 0
override func didMoveToView(){
self.anchorPoint = CGPointMake(0.5,0)
addChild(containerNode )
containerNode.addChild(player)
moveWorld()
}
func moveWorld(){
let moveWorldAction = SKAction.moveByX(-screenWidth, y:0, duration:6)
let block = SKAction.runBlock(movedWorld)
let seq = SKAction.sequence([moveWorldAction,block])
let repeatAction = SKAction.repeatActionForever(seq)
containerNode.runAction(repeatAction)
}
func movedWorld() {
inc = inc + 1
addObjects()
}
func addObjects() {
let obj = Object()
containerNode.addChild(obj)
let ranX = arc4random_uniform(screenWidth)
let ranY = arc4random_uniform(screenHeight)
obj.position = CGPointMake(screenWidth * (inc + 1) + ranX, ranY)
}
There's some conversion that I've omitted in the code above from int to float but it's not necessary for the point I want to understand.
I get why when new objects are created we do the multiple by the increment, but what I don't get is the containerNode doesn't have a size, so why do it's children show? Is this the most efficient way to do this?
I'm assuming that it's just convenience rather than moving all the other objects individually but the fact that it doesn't have a size is confusing me.
Since an SKNode isn't rendered, it doesn't need (or have) a size property. Only SKNode subclasses (SKLabelNode, SKShapeNode, etc.) that are visible require a size (declared explicitly or calculated implicitly). For example, SpriteKit needs to know the size of an SKSpriteNode with a 20 x 20 texture to correctly render it. You can use SKNode's calculateAccumulatedFrame method to determine the total size (a rectangle) of all of the descendants (other than other SKNodes) in its node tree.
If I understand the documentation for SKNode correctly this little section should answer your question:
Every node in a node tree provides a coordinate system to its
children. After a child is added to the node tree, it is positioned
inside its parent’s coordinate system by setting its position
properties. A node’s coordinate system can be scaled and rotated by
changing its xScale, yScale, and zRotation properties. When a node’s
coordinate system is scaled or rotated, this transformation is applied
both to the node’s own content and to that of its descendants.
Source
Again, if I'm reading this correctly, it seems that when a node is added to a node tree it inherits it's parent coordinate system. So by default containerNode is the same size as self in this case.
Also according to the frame property of a SKNode:
The frame is the smallest rectangle that contains the node’s content,
taking into account the node’s xScale, yScale, and zRotation
properties. Source
Which again sounds like if a frame isn't set it takes the smallest rectangle (self in this case or it might be understood that it's the children of containerNode I'm not sure) as it's own frame.
Whatever the case is, it's inheriting it from another SKNode.
Hey im simply trying to figure out how to remove a SCNTransformConstraint from its node
I can't find anything and Apple's Developer website doesn't help is it possible to remove a constraint so the now physics and other forces/Impulse/Gravity can act on the node as normal.
Instead of the node being stuck to the constraint in definite
let constraint = SCNTransformConstraint(inWorldSpace: true, withBlock: {
node, matrix in
let newPosition = User.transform
return newPosition
})
Ball.constraints = [constraint]
// Error here
Ball.removeAllConstraints()
I have a (moving) sprite node.
I'd like to detect other (moving) sprite nodes within a certain range of this node. Once one is detected, it should execute an action.
The playing an action part is no problem for me but I can't seem to figure out the within-range detection. Does have any ideas how to go about this?
A simple, but effective way to do this is comparing the position's in your scene's didEvaluateActions method. didEvaluateActions gets called once a frame (after actions have been evaluated but before physics simulation calculations are run). Any new actions you trigger will start evaluating on the next frame.
Since calculating the true distance requires a square root operation (this can be costly), we can write our own squaredDistance and skip that step. As long as our range/radius of detect is also squared, our comparisons will work out as expected. This example shows detect with a "true range" of 25.
// calculated the squared distance to avoid costly sqrt operation
func squaredDistance(p1: CGPoint, p2: CGPoint) -> CGFloat {
return pow(p2.x - p1.x, 2) + pow(p2.x - p1.x, 2)
}
// override the didEvaluateActions function of your scene
public override func didEvaluateActions() {
// assumes main node is called nodeToTest and
// all the nodes to check are in the array nodesToDetect
let squaredRadius: CGFloat = 25 * 25
for node in nodesToDetect {
if squareDistance(nodeToTest.position, p2: node.position) < squaredRadius {
// trigger action
}
}
}
If the action should only trigger once, you'll need to break out of the loop after the first detection and add some sort of check so it does not get triggered again on the next update without the proper cool down period. You may also need to convert the positions to the correct coordinate system.
Also, take a look at the documentation for SKScene. Depending on your setup, didEvaluateActions might not be the best choice for you. For example, if your game also relies on physics to move your nodes, it might be best to move this logic to didFinishUpdate (final callback before scene is rendered, called after all actions, physics simulations and constraints are applied for the frame).
Easiest way I can think of without killing performance is to add a child SKNode with an SKPhysicsBody for the range you want to hit, and use this new nodes contactBitMask to determine if they are in the range.
Something like this (pseudo code):
//Somewhere inside of setup of node
let node = SKNode()
node.physicsBody = SKPhysicsBody(circleOfRadius: 100)
node.categoryBitMask = DistanceCategory
node.contactBitMask = EnemyCategory
sprite.addNode(node)
//GameScene
func didBeginContact(...)
{
if body1 contactacted body2
{
do something with body1.node.parent
//we want parent because the contact is going to test the bigger node
}
}
I'm making a game in Xcode 6 using spriteKit and swift. I have a plane on scene, and to make it look like its moving, I'm create clouds off the scene to the left of the screen. I then us an SKAction to move the cloud to the right side of the screen. This works great. You then click to jump off the plane, and the plane moves up off the scene. I then have it start making the clouds on the bottom of the scene, then they move up off the top of the scene, but the problem is, the already existing clouds still have to move to the right side of the screen. My question is, how do I make it so all of the existing clouds stop their action that moves them to the right, then begin to move up exactly where they are? How do I access the group of existing clouds all at the same time once they have been created? I also want the clouds to slow down after you have jumped when you tap the screen to open your parachute, but this should be able to be done by ending the SKAction that moves the clouds, then using another SKAction on them that moves them up slower, but I don't know how to access a group of SKSpriteNodes.
Here is the code that I have to make the clouds:
//This is how the cloud is first declared at the top of the .swift file
var cloud = SKSpriteNode()
//This is the function that runs every certain interval through an NSTimer
func createCloud()
{
cloud = SKSpriteNode(imageNamed: "cloud")
cloud.xScale = 1.25
cloud.yScale = cloud.xScale/2
cloud.zPosition = -1
self.addChild(cloud)
if personDidJump == false
{
let moveRight = SKAction.moveToX(CGRectGetWidth(self.frame) + CGRectGetWidth(cloud.frame), duration: cloudSpeed)
var apple = CGRectGetWidth(self.frame)
var randomNumber:CGFloat = CGFloat(arc4random_uniform(UInt32(apple)))
cloud.position = CGPointMake(-CGRectGetWidth(cloud.frame), randomNumber)
cloud.runAction(moveRight)
}
if personDidJump == true
{
let moveUp = SKAction.moveToY(CGRectGetHeight(self.frame) + CGRectGetWidth(cloud.frame), duration: cloudSpeed)
var apple = CGRectGetWidth(self.frame)
var randomNumber:CGFloat = CGFloat(arc4random_uniform(UInt32(apple)))
cloud.position = CGPointMake(randomNumber, -CGRectGetHeight(cloud.frame))
cloud.runAction(moveUp)
}
}
Also, should I be worried about deleting the clouds when they move off the scene? Or can I just leave them there because you can't see them, and from what I've seen, they don't lag you.
Any help would be greatly appreciated. Thank you.
-Callum-
Put your clouds in an array. When your player jumps (or whenever you need to move them) run through the array and run your action on each cloud.
stackoverflow.com/a/24213529/3402095
^ That is where I found my answere ^
When you make a node give it a name:
myNode.name = "A Node Name"
self.addChild(myNode)
If there are a lot of these nodes, later you can change properties or preform SKAcions on these nodes or do whatever you want to do by using enumerateChildNodesWithName:
self.enumerateChildNodesWithName("A Node Name")
{
myNode, stop in
myNode.runAction(SKAction.moveToY(CGRectGetHeight(self.Frame), duration: 1)
}
I hope this is useful to whoever may need it.