3D cube problem, part 2

This is my second question about 3D cubes in iphone using CALayer, Core Animation framework, written in Objective-c. For my first question, please visit the 3D Cube Problem! Part 1

I am using Brad Larson's code to rotate my 3D cube from this link.

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

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

enter image description here

But I want to rotate it around the x axis along the black line shown in the figure.
Now in my code, I don't have any pink line or black line drawn in my view, so someone can help me with this.

If this helps here - this is the code to rotate my cube in the touchesMoved: method touchesMoved:

 - (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 the touchesBegan: method, and currentLayer is CALayer , where I created this cube.

Thanks for your help.

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

+6
source share
2 answers

It looks like you need to translate the layers first before rotating them.

Instead of choosing a pivot / snap point, you can first use CATransform3DTranslate to move the layers away from their natural origin, and then use CATransform3DRotate.

This will help take a look at how you create your cube to determine exactly how it can be implemented.

+3
source

Set andchorPoint to the center; if you deal with CALayer, then ...

 [myLayer setAnchorPoint:CGPointMake(0.5, 0.5)]; 
+3
source

Source: https://habr.com/ru/post/890051/


All Articles