I have posted a similar question to this
here , but this is different in that it deals with eular angles.
Given the setup that I had in the other post. A simple board that is on the screen, and a camera that is looking at it, I want to rotate the camera. For simplicity I am doing the entire camera in code. For context, per the other question and answer I have established that the board runs long on the Z axis, Shorter on the X axis, and the height is the Y axis.
Adding this code to the scene we can see my board running on the Z axis. I have raised the camera up on the Y axis a little to get a better view. My end goal is to get the board running longways accross the camera.
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 5, 0);
cameraNode.eulerAngles = SCNVector3Make(0, 0, 0);
Gives
Great start. Then I try to rotate the camera so it is looking from the top, down on the board. My understanding it that I would need to rotate around the X-axis to accomplish this. I did this by trying the following.
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 50, 0);
cameraNode.eulerAngles = SCNVector3Make(0, 0, -M_PI/2);
The iOS documentation states that the rotation in eulars is set with (z, y, x). This did not work, however, and only gave a blank screen. I then started experimenting and found that rotation around the Z axis would get me in the right direction.
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 50, 0);
cameraNode.eulerAngles = SCNVector3Make(-M_PI/2, 0, 0);
This rendered this screen.
This didn't make sense but I went ahead with it and eventually found that by also Rotating around the Y axis I could get my desired screen.
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 50, 0);
cameraNode.eulerAngles = SCNVector3Make(-M_PI/2, -M_PI/2, 0);
The y rotation was also a little baffling because I would have expected to have to rotate around the Z axis.
While this code works I have a similar question to my other post. Why do I have to rotate around the Z axis instead of the X axis for my first rotation. I think I might just not understand eular angles that well.
Thanks in advance
Edit:
as #mnuages pointed out, the documentation online states that this vector is in (x,y,z). This is contrary to the header documentation which I would normally use as can be seen here
It would appear that this is just a bug/typo in Apple's code. Thanks for clearing it up.
[Comment does not fit in a comment]
Please note that the documentation states that
The order of components in this vector matches the axes of rotation:
Pitch (the x component) is the rotation about the node’s x-axis.
Yaw (the y component) is the rotation about the node’s y-axis.
Roll (the z component) is the rotation about the node’s z-axis.
Related
I have a scene in which a human body is displayed. I want to zoom in to a specific body part when a user taps on it.
I changed the position of the camera to the position of Node but it points not exactly on it.
Also I need to keep the selected part in center of the screen when zoom in.
How can I accomplish zoom in / out?
I solved my problem by moving the camera instead of scaling the Model. I got the tap point by Gesture Recognizer and similarly the point of touch.
Now I converted the View-Coordinates to Scene Coordinates
CGPoint p = [gestureRecognize locationInView:scnView];
NSArray *hitResults = [scnView hitTest:p options:nil];
SCNVector3 projectedOrigin = [scnView projectPoint:SCNVector3Zero];
SCNVector3 vector = SCNVector3Make(p.x, p.y, projectedOrigin.z);
SCNVector3 worldPoint = [scnView unprojectPoint:vector];
and then positioned the Camera to the worldPoint.
To reposition it in a Z-axis you want to multiply the currents node matrix with the new matrix.
var node = childNode.transform
var translation = SCNMatrix4MakeTranslation(1.0, 1.0, adjustedZValue)
var newTrans = SCNMatrix4Mult(node, translation)
childNode.transform = newTrans
Edit: Had some names mixed up
a bit cleaned up and more "swifty":
let transform = childNode.transform
let adjustedZValue = Float32(3)
let translation = SCNMatrix4MakeTranslation(1.0, 1.0, adjustedZValue)
let newTrans = SCNMatrix4Mult(transform, translation)
childNode.transform = newTrans
I am using the following method to determine which SCNNodes are visible by the camera.
[self.scnView nodesInsideFrustumWithPointOfView:cameraNode];
However the returned array is always empty.
I set up the scene up as follows:
-(void)setupScene{
scene = [SCNScene scene];
cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 0, 0);
[scene.rootNode addChildNode:cameraNode];
self.scnView.scene = scene;
self.scnView.showsStatistics = YES;
self.scnView.backgroundColor = [UIColor clearColor];
}
At a random time, after the scene is created, I add a SCNNode to the scene:
testnode = [Testnode createNode];
testnode.position = SCNVector3Make(0, 0, -10);
[self.scnView.scene.rootNode addChildNode:testnode];
On my device, the node "testnode" is visible on my screen yet nodesInsideFrustumWithPointOfView: returns nothing.
EDIT: I tried changing the point of view to a spot light object and test whether "testnode" is inside its frustum. Here is what I see on screen: http://imgur.com/a/C3XGu Yet the array still returns empty. The testnode is the white cube.
Adding this for better visibility. Thank Crashalot for this.
Basically, if the SCNNode you are trying to detect using [scnView nodesInsideFrustumWithPointOfView:] and [scnView isNodeInsideFrustum: withPointOfView:] is the child of a node with empty geometry, it won't be detected.
In my case, I added a plane geometry and set the material to transparent:
SCNNode *emptynode = [SCNNode node];
node.geometry = [SCNPlane planeWithWidth:1.0f height:2.0f];
node.geometry.firstMaterial.transparency = 0.0f;
In my app I load models from different files (format is the same) and they have different geometry: big, small, wide, etc. I have object and camera position hard coded and for some cases I don't see anything because camera not point to object.
Maybe there is a way to normalise model before adding it to scene.
Update.
With Moustach answer I came up to following solution:
// import object from file
SCNNode *object = [importer load:path];
object.position = SCNVector3Make(0, 0, 0);
[scene.rootNode addChildNode:object];
// create and add a camera to the scene
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
// to avoid view clipping
cameraNode.camera.automaticallyAdjustsZRange = YES;
// set camera position to front of object
SCNVector3 sphereCenter;
CGFloat sphereRadius;
[object getBoundingSphereCenter:&sphereCenter radius:&sphereRadius];
cameraNode.position = SCNVector3Make(sphereCenter.x, sphereCenter.y, sphereCenter.z + 2 * sphereRadius);
[scene.rootNode addChildNode:cameraNode];
Works well for me.
You can calculate the bounding box of your mesh, and scale it based on the number you get to make it the same size as other objects.
My scene contains;
A static body at the root of the scene
_firstNode = [[SCNNode alloc] init];
_firstNode.position = SCNVector3Make(0, 0, 0);
_firstNode.geometry = [SCNSphere sphereWithRadius:950];
_firstNode.geometry.firstMaterial.diffuse.contents = [UIColor greenColor];
_firstNode.physicsBody = [SCNPhysicsBody staticBody];
[[scene rootNode] addChildNode:_firstNode];
A dynamic body with it's pivot property at (0,-1200,0)
_secondNode = [[SCNNode alloc] init];
_secondNode.position = SCNVector3Make(0, 0, 0);
_secondNode.pivot = SCNMatrix4MakeTranslation(0, -1200, 0);
_secondNode.physicsBody = [SCNPhysicsBody dynamicBody];
_secondNode.physicsBody.physicsShape = [SCNPhysicsShape shapeWithGeometry:[SCNSphere sphereWithRadius:2] options:nil];
[[scene rootNode] addChildNode:_secondNode];
Since both nodes are children of the rootNode the pivot is located in the centre of the static node. The idea is to run a SCNAction that rotates _secondNode around the _firstNode about it's X axis and applyTorque as needed to rotate about it's Y axis.
That code is fine for the moment, the problem is that while _firstNode remains static, it is pushing _secondNode.pivot to the surface of _firstNode's geometry. However simply taking around _firstNode's physicsBody away doesn't solve the problem either.
Sounds like it is getting too much friction as it is trying to spin. Try setting your angularDamping down to 0. Its default is 0.1. The angularDamping simulates the amount of friction as an object spins. A value of 0 simulates no friction (spin for ever). A value of 1.0 simulates 100% friction or static basically.
Set it like so...
yourNode.physicsBody.angularDamping = 0
I am trying to draw a basic ground to the game for my sprite to run on.
But it seems that the ground is too short although it is suppose to take up 1/3 of the height of the screen.
My GameScene.sks is already changed to 568x320 (landscape, iPhone 5/5S)
this is my current code
func initMainGround() {
let gSize = CGSizeMake(self.size.width/4*3*2, 120);
let ground = SKSpriteNode(color: SKColor.brownColor(), size: gSize);
ground.name = gName; //Ground
ground.position = CGPointMake(0, 0);
ground.physicsBody = SKPhysicsBody(rectangleOfSize: gSize);
ground.physicsBody.restitution = 0.0;
ground.physicsBody.friction = 0.0;
ground.physicsBody.angularDamping = 0.0;
ground.physicsBody.linearDamping = 0.0;
ground.physicsBody.allowsRotation = false;
ground.physicsBody.usesPreciseCollisionDetection = true; //accurate collision
ground.physicsBody.affectedByGravity = false;
ground.physicsBody.dynamic = false;
ground.physicsBody.categoryBitMask = gBitmask; // 0x1 << 0
ground.physicsBody.collisionBitMask = pBitmask; //0x1 << 1 playerCategoryBitmask
self.addChild(ground);
}
NSLog(String(self.size.height)) return 320.0 which is perfectly fine.
But why is it that the SKSpriteNode is draw wrongly?
Setting the height of the ground to 320 only fills up half of the screen although the height of the screen in landscape is 320.
Like Jon said, this is a placement issue not a size issue. The default anchor point of any given node is in its center, so you have two options here:
1) set ground.position to CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
(or even better yet, capture that as an ivar, because you'll be referring to it a whole lot when adding things to your screen, and there's no real reason to do the calculations dozens of times)
2) change the anchor point of the ground node. This is done as a CGPoint, but is interpreted as a percentage of the size of the node in question, with the default (center) being (0.5, 0.5). ground.anchorPoint = CGPointZero (which is just a shortcut for CGPointMake(0, 0)) will set the node's anchor point to its lower-left corner, at which point setting its position to (0,0) will correctly place it starting at the lower-left corner of your scene (or its parent node, in any event).