Search code examples
cocos2d-iphonecocos3d

How to set the location of a CC3Node in a layer?


I've not been able to find this in the docs.

I've got a CC3Node (myNode) and my layer has the callbacks for touchDown & touchMoved events all of which works, so far. I'm trying to drag myNode around the screen, preferably using screen coordinates.

How do I set myNode's location relative to screen (layer) coordinates?


Solution

  • Ok, I finally tracked this down, although the pieces were a bit scattered around in multiple posts. My final solution looked something like this:

    MyWorld.m:

    // Error checking and misc removed
    - (void) addBackgroundForProjection
    {
        // background plane supports touch events
        background = [CC3PlaneNode nodeWithName: BACKGROUND_PROJECTION_PLANE_NAME];
        [background populateAsCenteredRectangleWithSize: CGSizeMake(10.0, 10.0)
                                            withTexture: [CC3Texture textureFromFile: @"transparent1x1.png"]
                                          invertTexture: YES];
        [background setIsOpaque: NO];
        [background retainVertexLocations];
        [background setLocation: cc3v(0, 0, 0.001)];
    
        [self addChild: background];
    }
    
    // ...
    
    - (void) moveNode: (CC3Node*) node toScreenLocation: (CGPoint) point
    {
        // update node on screen
        CC3Plane groundPlane = self.background.plane;
        CC3Vector4 touchLoc = [self.activeCamera unprojectPoint: point ontoPlane: groundPlane];
        CC3Vector newLoc = cc3v(touchLoc.x,touchLoc.y,touchLoc.z);
        [node setLocation: newLoc];
    }