Rotation around z- axis through arbitary (x,y) point in metal

307 Views Asked by At

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

There are 1 best solutions below

0
On

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{

            }
        }