AB4D Forum
Rotate and move transform - Printable Version

+- AB4D Forum (https://forum.ab4d.com)
+-- Forum: Products Forums (https://forum.ab4d.com/forumdisplay.php?fid=4)
+--- Forum: Ab3d.PowerToys (https://forum.ab4d.com/forumdisplay.php?fid=9)
+--- Thread: Rotate and move transform (/showthread.php?tid=4200)

Rotate and move transform - kreativasas - 06-22-2020

Hi I need an hand.

I'm using a modelmover and a model rotator to move my meshes.

here is the code for modelrotator and modelmover:
AddHandler mymodelrotator(numdente).ModelRotateStarted, AddressOf started
AddHandler mymodelrotator(numdente).ModelRotated, AddressOf rotated
AddHandler mymodelrotator(numdente).ModelRotateEnded, AddressOf endrotate
'handel for modelrotator
 Sub started(sender As Object, args As ModelRotatedEventArgs)
       rotationvector = args.RotationAxis
       _startRotate = New AxisAngleRotation3D(rotationvector, 0)
       Dim rotatetransform As New RotateTransform3D(_startRotate)
'create new transformation on modelhit
       addtransform(dentehit.modelhit, rotatetransform)
    End Sub
Sub rotated(sender As Object, args As ModelRotatedEventArgs)

       _startRotate.Angle = args.RotationAngle

   End Sub
Sub addtransform(myvisual As Model3D, mytransform As RotateTransform3D)
       If myvisual.Transform Is Nothing Then
           myvisual.Transform = mytransform
       End If

       Dim transformgroup As New Transform3DGroup

       myvisual.Transform = transformgroup
   End Sub

'handler for modelmover

  AddHandler mymodelmover(numdente).ModelMoveStarted, AddressOf startedmove
  AddHandler mymodelmover(numdente).ModelMoved, AddressOf rotatedmove
   AddHandler mymodelmover(numdente).ModelMoveEnded, AddressOf endrotatemove

Sub startedmove(sender As Object, args As EventArgs)

       translate = New Vector3D
       Dim translatetransform As New TranslateTransform3D(translate)
     ' create new transformation on modelhit
       addtransform1(dentehit.modelhit, translatetransform)

   End Sub
Sub rotatedmove(sender As Object, args As Ab3d.Common.ModelMovedEventArgs)
       translate = args.MoveVector3D

   End Sub
Sub addtransform1(myvisual As Model3D, mytransform As TranslateTransform3D)
       If myvisual.Transform Is Nothing Then
           myvisual.Transform = mytransform
       End If

       Dim transformgroup1 As New Transform3DGroup

       myvisual.Transform = transformgroup1

   End Sub

For modelrotator i use rotatetransform3d and everything works perfetctly, but with model mover i use translatetransform3d with the same parameters but nothing change!
Where i'm wrong?

RE: Rotate and move transform - abenedik - 06-23-2020

I have quickly checked the code and did not find a reason why the translation would not be shown. However, the way you apply transformations is not correct - if you have multiple transformations, they should be apply with first applying scale, then rotation and finally translation. If you translate the object before rotating it, the object will be moved away from the center of rotation (0,0,0) and this will create a different rotation then it would be if the objects would be first rotated and then translated.

To solve that I would advise you that for each 3D object you create a Transform3DGroup and add a RotateTransform3D and a TranslateTransform3D to the group - then assign a new instance of that Transform3DGroup to each 3D object in your scene. When you have a rotation or movement then just adjust the already applied translation or rotation. This way you will be also able to read the already applied rotation and when starting a rotation you will be able to rotate the ModelRotator to already rotated angles.

Another option is to use StandardTransform3D object from Ab3d.Utilities namespace. It provides TranslateX, TranslateY, TranslateZ, ScaleX, ScaleY, ScaleZ, RotateX, RotateY and RotateZ properrties. Note that because it is not possible to derive from Transform3D, this object cannot be assigned to Transform property. Instead the StandardTransform3D provides a public Transform of type MatrixTransform3D that can be assigned to the object's Transform property. 

The StandardTransform3D can be assigned to the Tag property (when using objects from BaseVisual3D). If you are working on Model3D objects or ModelVisual3D objects that doe not provide Tag property, you will need to define a Disctionary<Model3D,StandardTransform3D> that will link the Model3D and StandardTransform3D.

RE: Rotate and move transform - kreativasas - 08-04-2020

I'm getting crazy!!!

I don't know why the translatetransform doesn't work. 
No matter if i have multiple transformation, they are used in different part of the code, i simply need to understand why it doesn't work.

I simply create a translatetransform3d(public var myvector) and then modify values of myvector during the ModelMoved event.

I use the same code with a rotatetransform3d and it works!! why!!

Can someone help me? is very important!


RE: Rotate and move transform - abenedik - 08-05-2020

I assume that your object was already transformed by some other transformation or its parent was transformed. For example, if the parent is rotated by 90 degrees around y axis and you then add a translation along x axis, because of the parent rotation the object will be actually translated along z axis.

You can check your scene with stoping at a breakpoint, then opening Immediate Windows in Visual Studio and execute the following command there:

This will display the hierarchy of your 3D scene with details about each object. The transformations are displayed as matrices - if there is only translation applied (no rotation and no scale), then the first part of the matrix is: 1 0 0 0   0 1 0 0   0 0 1 0   - after that there is translation part: OffestX OffestY OffsetY 1

So check if the object that you are moving or any of the parent objects have some rotation or other type of non-translation transformation applied.

RE: Rotate and move transform - kreativasas - 08-05-2020

No other transformation!
To be sure i wrote anoter simple code with only a modelmover but nothing! where i'm wrong
Here's the code
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

       mygrid.Background = System.Windows.Media.Brushes.White

       _targetPositionCamera.TargetPosition = New Point3D(0, 30, 0)
       _targetPositionCamera.Distance = 100
       _targetPositionCamera.Heading = 0
       _targetPositionCamera.Attitude = 0
       _targetPositionCamera.ShowCameraLight = ShowCameraLightType.Always
       _targetPositionCamera.TargetViewport3D = myview

       Dim _mouseCameraController = New Ab3d.Controls.MouseCameraController()

       _mouseCameraController.RotateCameraConditions = MouseCameraController.MouseAndKeyboardConditions.RightMouseButtonPressed
       _mouseCameraController.MoveCameraConditions = (MouseCameraController.MouseAndKeyboardConditions.RightMouseButtonPressed Or MouseCameraController.MouseAndKeyboardConditions.ControlKey)
       _mouseCameraController.EventsSourceElement = mygrid
       _mouseCameraController.TargetCamera = _targetPositionCamera



       ElementHost1.Child = mygrid

       AddHandler mymodelmover.ModelMoveStarted, AddressOf startedmove
       AddHandler mymodelmover.ModelMoved, AddressOf rotatedmove


   End Sub
   Sub startedmove(sender As Object, args As EventArgs)

       _offsetx = 0
       _offsety = 0
       _offsetz = 0
       first_move = True
       Dim translatetransform As New TranslateTransform3D(_offsetx, _offsety, _offsetz)
       mymodelmover.Transform = translatetransform

   End Sub
   Sub rotatedmove(sender As Object, args As Ab3d.Common.ModelMovedEventArgs)
       _offsetx = args.MoveVector3D.X
       _offsety = args.MoveVector3D.Y
       _offsetz = args.MoveVector3D.Z

   End Sub

RE: Rotate and move transform - abenedik - 08-05-2020

Could you please attach vb and xaml file so I can take a closer look at the whole code. Or use feedback page (https://www.ab4d.com/Feedback.aspx) and send the sample files in a zip.

RE: Rotate and move transform - kreativasas - 08-10-2020

I add transformation on modelmoved handler and not on modemovedstarted and it works.
Here is the code
 Sub rotatedmove(ssender As Object, args As ModelMovedEventArgs)

       _offsetx = args.MoveVector3D.X - _offsetx_prec
       _offsety = args.MoveVector3D.Y - _offsety_prec
       _offsetz = args.MoveVector3D.Z - _offsetz_prec
       Dim rotatetransform As New TranslateTransform3D(_offsetx, _offsety, _offsetz)
       Dim mygroup As New Transform3DGroup
       If mymodelmover.Transform Is Nothing Then

       End If


       mymodelmover.Transform = mygroup
       ' Dim translatetransform As New TranslateTransform3D(_offsetx, _offsety, _offsetz)
       _offsetx_prec = args.MoveVector3D.X
       _offsety_prec = args.MoveVector3D.Y
       _offsetz_prec = args.MoveVector3D.Z
   End Sub