Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3D Cube problem, Part 2

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

like image 803
Robin Avatar asked Jun 08 '11 12:06

Robin


People also ask

What is 3D shapes with examples?

In geometry, three-dimensional shapes or 3D shapes are solids that have three dimensions such as length, width and height. Whereas 2d shapes have only two dimensions, i.e. length and width. Examples of three-dimensional objects can be seen in our daily life such as cone-shaped ice cream, cubical box, a ball, etc.

How many 3D shapes are there?

The common names of these shapes are cube, cuboid, cone, cylinder and sphere. 3D shapes are defined by their respective properties such as edges, faces, vertices, curved surfaces, lateral surfaces and volume.


2 Answers

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.

like image 115
Ron Bakker Avatar answered Oct 12 '22 07:10

Ron Bakker


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

[myLayer setAnchorPoint:CGPointMake(0.5, 0.5)];
like image 37
Simon Lee Avatar answered Oct 12 '22 07:10

Simon Lee