6

This is my 2nd Question on 3D cubes in iphone using CALayer, Core Animation framework, written in Objective-c. For my first question please visit here 3D Cube Problem! Part 1.

I am using Brad Larsons code to rotate my 3D cube from this link

http://www.sunsetlakesoftware.com/2008/10/22/3-d-rotation-without-trackball

The problem is my cube is rotating in x axis along the pink line shown in the figure.

enter image description here

But I want to rotate it around x axis along the black line shown in the figure.
Now in my code I dont have any pink line or black line drawn on my view so can some one please help me with this.

If it helps here is the code for rotating my cube in touchesMoved: method

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    CGPoint location = [[touches anyObject] locationInView:self];
    CATransform3D currentTransform = currentLayer.sublayerTransform;
    CGFloat displacementInX = location.x - previousLocation.x;
    CGFloat displacementInY = previousLocation.y - location.y;
    CGFloat totalRotation = sqrt(displacementInX * displacementInX + displacementInY * displacementInY);
    CGFloat x = (displacementInX/totalRotation) * currentTransform.m12 + (displacementInY/totalRotation) * currentTransform.m11;
    CATransform3D rotationalTransform = CATransform3DRotate(currentTransform, totalRotation * M_PI / 180.0, x, y, 0);
    currentLayer.sublayerTransform = rotationalTransform;
}

previousLocation is a CGPoint initialized in touchesBegan: method, and currentLayer is CALayer where I have created this cube.

Thanks for your help.

PS. If you want to know how I created this cube then let me know

Community
  • 1
  • 1
Robin
  • 10,011
  • 5
  • 49
  • 75
  • I'd love to know how you created the cube. I'm trying to do a similar thing to you. Any progress so far? – Jeremy Jul 21 '11 at 23:59
  • 1
    i have scraped the project but i think i could give you the code to make a cube in quartz core. – Robin Jul 22 '11 at 04:39
  • Hey, I got a very similar code, rotating `CALayer` in 3D and all, but I solved the problem by changing the `anchorPoint` as Simon Lee suggested. You must have done something wrong with it. Please share layer construction code, and I'll solve it... Or, I can do my own cube if required. – Mazyod Nov 25 '12 at 17:07
  • thats not true i have tried it and everything still the results are same. May be there is something else i am missing out. – Robin Nov 25 '12 at 17:13
  • So, are you gonna share the code, or should I build a 3D cube and rotate it? – Mazyod Nov 25 '12 at 17:28
  • I will do that 2day as i dont have it with me at the office. Thanks for your help. – Robin Nov 26 '12 at 03:43
  • How did u solved this Robin? can u share the code for transformation? – Hassy Mar 25 '14 at 06:14

2 Answers2

4

Looks like you need to translate the layers first prior to rotating them.

Instead of picking a pivot/anchor point you can use CATransform3DTranslate first to translate the layers away from their natural origin, and use CATransform3DRotate after that.

It would help to take a look at how you build your cube in order to pinpoint how it can be implemented.

Ron Bakker
  • 424
  • 3
  • 6
3

Set the andchorPoint to the centre; if dealing with a CALayer then...

[myLayer setAnchorPoint:CGPointMake(0.5, 0.5)];
Simon Lee
  • 22,304
  • 4
  • 41
  • 45