I figured out that if you want to use physics in Sprite Kit you have to specify anchor points in world coordinates. Say you have a big node tree and there is a car here and a wheel there. You know their coordinates in their parent nodes only. You want to define a spring joint between car and wheel but you have to tell Sprite Kit the anchors in coordinate space of your scene.
How would you then convert the spring anchor point on the car and the spring anchor point on the wheel center into world coordinates?
In UIKit there are conversion methods like convertPoint:toWindow: etc.
How do you do this in Sprite Kit?
You should be able to use SKNode convertPoint:toNode:.
[wheelNode convertPoint:CGPointMake(0,0) toNode:carNode];
[carNode convertPoint:CGPointMake(0,0) toNode:treeNode];
Something like that...
Related
So I have a camera in my SceneKit project that is able to rotate and move around freely, and I have an object that, in some cases, needs to stay at a constant distance away from the camera and always be in the center of its view no matter how the camera rotates. Unfortunately, I'm new to SceneKit and don't know how to accomplish this.
So the key things I'm looking for are:
How to have the object always at the same distance from the camera
How to have the object always be in the center of the camera's view no matter what direction it's looking
At the moment, both the camera and the object (an SCNNode with a box geometry) are children of the same scene.
I'm coding in swift so I'd prefer an answer using that, but if you have a solution in objective-c, that works too.
Thanks a bunch!
Think about how you might solve this in the real world. Grab a two by four of the appropriate length. Use duct tape to attach the ball to one end, and to attach the camera (aimed at the ball) to the other end.
Now you can carry that rig around with you. The ball will always be in the center of the camera view, and will be a constant distance away from the camera.
You can build the same rig, virtually, in SceneKit. Create a new SCNNode to be the rig (taking the place of the two by four). Add the ball as a child node, at (0, 0, 0). Add the camera as a child node too, at (0, 0, 5) (camera looks down the -Z axis, so this position should put the ball in the center of the view). Now you can move the rig node anywhere in the scene you want, and you'll have a consistent ball position.
I would like to move the camera around a SCNNode nodeA.
What I'have done so far is:
self.cameraNode.constraints = [SCNLookAtConstraint(target: nodeA)]
now if I increment the position.x of the camera the camera still look toward the nodeA. How to move the camera around the nodeA (like the moon around the hearth)?
The camera should move along a circular orbit (just a circle, I'm not interested in elliptical orbit)
Add the moon as the child node of the earth. That way translating the earth won't affect the moon's relative position. You can find an example in the SceneKit Slides for WWDC 2014 sample code.
I am trying to convert normal uikit coordinates (top left origin, width and height go right and down) to Sprite Kit coordinates (origin is at center of node, left and down are negative, right and up are positive).
Does anyone know how to do this? Unfortunately I can't work around this to my knowledge because I am using QuartzCore for drawing on a SKNode. Without converting, I am getting really off drawings.
According to the docs, SKView has methods for Converting Between View and Scene Coordinates.
Remember the scene is itself an SKNode, so if you need to get from scene coordinates to a particular node coordinate system there's also the methods in Converting to and from the Node’s Coordinate System.
I'm creating a semi-circle node when i try to develop a simple sprite kit game, but i wish the physics body of the node just cover the circle object's edge, and the physics body rotates with the circle(to change the physics body programatically), what should i do?
You can do complex shapes using SKPhysicsBody -bodyWithPolygonFromPath:
There are even tools that let you draw your shape and generate the code for you. See this question for examples: SpriteKit's SKPhysicsBody with polygon helper tool
I am new to Objective-C and I am trying to make a simple spriteKit game which contains a circle and a ball inside it. I want to move the ball around in this circle as well on the iPhone movement. So far I have created the circle and am able to put the ball on the screen.
My problem is that I am not sure the ball is actually inside the circle or on top of it. When I attach a physics body on the ball, it drops out of the screen and does not stop on the circle's bottom edge. I am using SKShapeNode for both the circle and ball.
Please help me to provide the right documentation to go through or a little piece of code that can resolve this.
You need to assign appropriate physics bodies to your nodes. See Simulating Physics in the Sprite Kit Programming Guide.
From your description, it sounds like you need to create the outter circle's physics body with the bodyWithEdgeLoopFromPath: method (and provide a path representing the circle). Note that an edge loop physics body is not dynamic and can only collide with volume-based bodies.
The inner balls physics body can be created with bodyWithCircleOfRadius:.