2

I have a CALayer added and transformed the following way. I would assume that translating the layer by any constant before rotation should not have an effect on rotation around Y axis but it seems to make a difference, what am I missing here ?

- (void)viewDidAppear:(BOOL)animated
{
    CALayer* layer = [CALayer layer];
    layer.bounds = self.img1.bounds;
    layer.backgroundColor = [[UIColor grayColor] CGColor];
    layer.anchorPoint = CGPointMake(0, 0.5);
    layer.position = CGPointMake(0, CGRectGetMidY(self.view.bounds));
    layer.contents = (id)[self.img1.image CGImage];
    layer.transform = [self transformforView1];
    [self.view.layer addSublayer:layer];
}

Rotation without any translation and output:

- (CATransform3D)transformforView1
{
    CATransform3D t = CATransform3DIdentity;
    t.m34 = 1.0/-700;
    t = CATransform3DTranslate(t, 0, 0, 0);
    t = CATransform3DRotate(t, degToRad(45), 0, 1, 0);
    return t;
}

Rotation without any translation and output

Same rotation with translation of 500 and output:

- (CATransform3D)transformforView1
{
    CATransform3D t = CATransform3DIdentity;
    t.m34 = 1.0/-700;
    t = CATransform3DTranslate(t, 500, 0, 0);
    t = CATransform3DRotate(t, degToRad(45), 0, 1, 0);
    return t;
}

Same rotation with translation of 500 and output

2cupsOfTech
  • 5,953
  • 4
  • 34
  • 57

3 Answers3

3

This is actually a very good question. The confusion comes from the fact that Model View Matrix and Projection Matrix are usually separate.

Read this Question for reference:

The purpose of Model View Projection Matrix

I'd suggest you separate your Model View Matrix and Projection Matrix.

So your CATransform3D t is effectively the concatenation of the two matrices, you could add another translation to move your model in the view or then create another transformation (your Model View Matrix). This is what I've done here:

CATransform3D modelViewTransform = CATransform3DMakeTranslation([UIScreen mainScreen].bounds.size.height / 2.0, 0, 0);
CATransform3D t = CATransform3DIdentity;
t.m34 = 1.0/-700;
t = CATransform3DTranslate(t, 500, 0, 0);
t = CATransform3DRotate(t, degToRad(45), 0, 1, 0);
return CATransform3DConcat(t, modelViewTransform);

This will move your model to the center of the screen instead of it being at the edge of the screen.

Community
  • 1
  • 1
Seppo
  • 565
  • 1
  • 5
  • 10
0

what am I missing here ?

You are missing how transform matrices work and how they are concatenated, more precisely that order matters.

When you say:

I would assume that translating the layer by any constant before rotation should not have an effect on rotation around Y axis

You probably base that assumption on that translating an already rotated layer shouldn't affect the rotation. The problem is that there is a difference between translating first and translating last. Matrices aren't commutative, which means that A*B != B*A

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • I understand the commutative property you are talking about wrt to matrices but I think I am doing it in the order I want, I want the image to shift right and then rotate 45 degrees around y axis – 2cupsOfTech Mar 04 '14 at 21:51
  • Around what point do you think that it rotates with that configuration? – David Rönnqvist Mar 04 '14 at 21:58
  • As I said I think I am first translating t and then rotating t, if you see any problem in the code please point out, thanks. I'm pretty much a beginner in 3D animation – 2cupsOfTech Mar 04 '14 at 22:07
  • btw if I take another approach and concatenate the translation and rotation matrices using CATransform3DConcat I get the same result – 2cupsOfTech Mar 06 '14 at 16:50
-1

Your code is rotating around the y axis, not the x axis. Why do you talk about rotating around the x axis if you're rotating around the x axis?

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I did not say anywhere that I'm rotating around x-axis, I'm trying to translate on x-axis and then rotate around y-axis – 2cupsOfTech Mar 05 '14 at 03:28