0

I have a plane with four vertices. It can be rotate around z-axis (0, 0,1).(achieve using model matrix in metal).Model matrix is changed base on rotation gesture.

So what I need to do is rotate plane around z-axis through arbitrary (x,y) where x,y not equal to zero.It means rotate plane around an axis which is perpendicular to xy plane an going through (x,y) point.

Any sugestion please?

  • 1
    This is typically achieved by translating by (-x, -y) to move your desired point to (0, 0), rotating, then translating back by (x, y). You can, of course, combine the matrices to make a single transform matrix that accomplishes all of those operations together. – Ken Thomases Jul 30 '18 at 13:36
  • I hope it will work.But is there any way to achieve smooth translation? I mean translate this much amount makes unsmooth translation – Kasun Palihakkara Aug 01 '18 at 05:22
  • I don't understand what you mean. "Smooth" between frames? This should all happen in one frame. The user should never see the translation. – Ken Thomases Aug 01 '18 at 14:28
  • First of all what I thought was wrong. Lately I figured out what you suggest. It works perfectly. Thanks again. – Kasun Palihakkara Aug 02 '18 at 04:24

1 Answers1

0

This works for me.Here dragCanvas method change translation in model matix while rotateCanvas change its rotation.You may implement your own which does the same. Metod convertCoodinates maps coordinate system to suit as describe in https://developer.apple.com/documentation/metal/hello_triangle

@objc func rotate(rotateGesture: UIRotationGestureRecognizer){

            guard rotateGesture.view != nil else { return }

            let location = rotateGesture.location(in: self.view)
            var rotatingAnchorPoint = convertCoodinates(tapx:location.x  , tapy:location.y )

            if rotateGesture.state == UIGestureRecognizerState.changed {
                print("rotation:\(rotateGesture.rotation)")

                renderer?.dargCanvas(axis:float3(Float(rotatingAnchorPoint.x) ,Float(rotatingAnchorPoint.y ),0))
                renderer?.rotateCanvas(rotation:Float(rotateGesture.rotation))
                renderer?.dargCanvas(axis:float3(Float(-rotatingAnchorPoint.x ) ,Float(-rotatingAnchorPoint.y  ),0))



                rotateGesture.rotation = 0
            } else if rotateGesture.state == UIGestureRecognizerState.began {

            }else if rotateGesture.state == UIGestureRecognizerState.ended{

            }
        }