How to determine how far a 3D object is from the edge of the screen

I rotated the view 50 degrees around the y axis using Core Animation. I want the edge of the image to touch the edge of the screen. How can i do this?

I know the length of the view and the number of degrees the view has rotated (50 degrees), so at first, I decided that I could determine the distance between the view and the edge using trigonometry. However, the camera's perspective affects the m34 property of the m34 structure. How can I determine the distance that I need to move the view to align it with the edge of the screen?

 CATransform3D rotation = CATransform3DIdentity; rotation.m34 = -1.0/500.0; rotation = CATransform3DRotate(rotation, 50.0 * M_PI / 180, 0.0, 1.0, 0.0); view.layer.transform = rotation; view.layer.zPosition = 200; 

enter image description here

+6
source share
2 answers

If you understand correctly, you want something like the following:

Final view

To make calculations easier, you need to play with anchorPoint CALayer . anchorPoint is where transformations are applied, and its effects are especially noticeable when cornering. We will make CALayer rotate around one of its points, and not the center (by default).

This is my loadView :

 UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; view.backgroundColor = [UIColor whiteColor]; CGRect frame = CGRectMake(view.frame.size.width - 100.f, view.frame.size.height / 2.f - 50.f, 100.f, 100.f); UIView *red = [[UIView alloc] initWithFrame:frame]; red.backgroundColor = [UIColor redColor]; [view addSubview:red]; CATransform3D rotation = CATransform3DIdentity; rotation.m34 = -1.0/500.0; // Since the anchorPoint is displaced from the center, the position will move with // it, so we have to counteract it by translating the layer half its width. rotation = CATransform3DTranslate(rotation, 50.f, 0, 0); rotation = CATransform3DRotate(rotation, 50.0 * M_PI / 180, 0.0, 1.0, 0.0); // We tell the anchorPoint to be on the right side of the layer (the anchor point // position is specified between 0.0-1.0 for both axis). red.layer.anchorPoint = CGPointMake(1.f, 0.5f); red.layer.transform = rotation; red.layer.zPosition = 200; 

And I get the image you see above.

Hope this helps!

+1
source

You want to apply your final transformation matrix (including the M34 change) to the endpoints of the edge of your view, the distance you are trying to calculate. This will give you the final location of your point in the projected space.

IOS 5 adds GLKit, which includes the entire library of functions for performing matrix and vector calculations. For example, look at GLKMatrix4MultiplyVector3. This function takes a 3-part vector and multiplies it by a 4x4 matrix and returns the resulting three-component vector.

You may need GLKMatrix4MultiplyVector4, because if I remember correctly, you want your vector to have 1 more component than the number of axes (so for a 3D vector you want to use a 4-component vector.) Vector math is not my strong suit, therefore I have to look at this material when I need to use it.

0
source

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


All Articles