开发者

3D Cube problem, Part 2

开发者 https://www.devze.com 2023-03-12 00:17 出处:网络
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.开发者_开发技巧

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.

3D Cube problem, Part 2

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


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.


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

[myLayer setAnchorPoint:CGPointMake(0.5, 0.5)];
0

精彩评论

暂无评论...
验证码 换一张
取 消