How to rotate and navigate viewport through RegionView3d, Quaternions and Matrix

Hi,

I am working on an addon that uses external application’s touch gesture to send information to a socket, which then is used to navigate the viewport through Python’s api and timer modal. I think I am almost there but I just need to understand how some of the math works . Just one thing I need to fix to get it working exactly the way I intended.

So first I have the rotational values that is changing the viewport/RegionView3d:

#self.defq is the initial rotation of the viewport before timer starts...r,rx,ry are all values from socket
quat_r = mathutils.Quaternion((0, 0,-1*srvrprop.rtouch_speed), math.radians(r))
quat_x = mathutils.Quaternion((1*srvrprop.rtouch_speed, 0, 0), math.radians(rx))
quat_y = mathutils.Quaternion((0, 1*srvrprop.rtouch_speed ,0), math.radians(ry))
quat = self.defq @ quat_r @ quat_x @ quat_y                            
self.region.view_matrix = quat

If I put it though the view_rotation. It works as intended. It starts rotating from user’s perspective

I also have the positional value that is directly being put through the copy of the view_matrix:

#self.matx is the initial view_region's copy before timer starts. lr,ud,ss are all values from socket
self.matx.col[3][0] = lr
self.matx.col[3][1] = ud
self.matx.col[3][2] = ss

Initially it was just using “view_location” to change position but then I rewrote it to directly change matrix values so that it would change the position from user perspective and not the Cartesian value of the grid.

My current problem is that I need to put them together . So far

self.region.view_matrix = self.matx @ quat.to_matrix().to_4x4()
  1. if I multiply them and put it into a view matrix, it resets the view port to top view before any changes being made to the view port.
self.region.view_matrix = quatm
self.region.view_rotation = self.defq @ quat_r @ quat_x @ quat_y
  1. If I put them separately, It starts being more laggy…and the location change works like a cartesian grid instead of going forward from the user/viewports perspective
self.region.view_matrix = mathutils.Matrix.LocRotScale(vec2,quat,None)
  1. If I use LocRotScale, the “r” value only orbits at the centre instead of rotating from the user.

I am sure there is an easy answer to this that I don’t know about since I am not familiar with math. But any help would be appreciated.