none
ModelVisual3D Transform null when trying to move a object RRS feed

  • Pergunta

  • Hello,
    I am trying to move a cube clicking on it and tracking the mouse. My cube is implemented like this:
    App.xaml
    <MeshGeometry3D x:Key="solidCube" 
    Positions="2, 2, 2 -2, 2, 2 -2, -2, 2 2, -2, 2 2, 2, -2 -2, 2, -2 -2, -2, -2 2, -2, -2"
    TriangleIndices="0,1,2 2,3,0 3,4,0 7,4,3 5,4,7 7,6,5 1,5,6 6,2,1 2,6,7 7,3,2 1,0,4 4,5,1"/>
    MainWindow.xaml
    <ModelVisual3D.Content>
    	<GeometryModel3D x:Name="solidCubeGeometryModel3D" Geometry="{StaticResource solidCube}">
            	<GeometryModel3D.Material>
                    	<DiffuseMaterial>
                            	<DiffuseMaterial.Brush>
                                    	<SolidColorBrush Color="Red" Opacity="1.0"/>
                                    </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                    </GeometryModel3D.Material>
                    <GeometryModel3D.Transform>
                    	<TranslateTransform3D x:Name="myTranslateTransform3D" OffsetX="0" OffsetY="0" OffsetZ="0" />
                    </GeometryModel3D.Transform>
    	</GeometryModel3D>
    </ModelVisual3D.Content>
    I have a TrackballDecorator around my viewport, which holds the events:
    <custom:TrackballDecorator x:Name="trackballDecorator" Grid.Column="1"
    MouseLeftButtonDown="OnMouseLeftButtonDown" MouseMove="OnMouseMove" MouseWheel="OnMouseWheel" MouseLeftButtonUp="OnMouseLeftButtonUp" PreviewTextInput="OnPreviewTextInput" LostMouseCapture="OnLostMouseCapture" Width="600" Height="600"> <custom:Interactive3DDecorator Grid.Column="1"> /// viewport etc...

    And my first event, OnMouseLeftButtonDown, I took from http://www.charlespetzold.com/3D/, example Mouse Tracking, which is:
     protected void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs args)
            {
                base.OnMouseLeftButtonDown(args);
    
                Point ptMouse = args.GetPosition(myViewport);
                HitTestResult result = VisualTreeHelper.HitTest(myViewport, ptMouse);
    
                // We're only interested in 3D hits.
                RayMeshGeometry3DHitTestResult result3d =
                                    result as RayMeshGeometry3DHitTestResult;
                if (result3d == null)
                    return;
    
                // We're only interested in ModelVisual3D hits.
                ModelVisual3D vis3d = result3d.VisualHit as ModelVisual3D;
                if (vis3d == null)
                    return;
    
                // We're only interested in visuals with translate transforms.
                transTracking = vis3d.Transform as TranslateTransform3D; //NULL !!!!!!!!!!!
                if (transTracking == null)
                    return;
    
                LineRange range;
                ViewportInfo.Point2DtoPoint3D(myViewport, ptMouse, out range);
                pointOriginal = range.PointFromZ(transTracking.OffsetZ);
                transOriginal = transTracking.Clone();
                isTracking = true;
                CaptureMouse();
                Focus();
    
                args.Handled = true;
            }

    I updated it, removing the overrides and put a object sender in the parameters.
    Is there something very obvious which I am missing for my ModelVisual3D Transform being null?
    Thanks in advance, if you need any more information to help me, just ask :D

    Pedro Martins Dusso Computer Science Student @ UFRGS www.inf.ufrgs.br/~pmdusso
    • Editado Pedro Dusso quinta-feira, 10 de novembro de 2011 10:34
    quinta-feira, 10 de novembro de 2011 10:34

Respostas

  • Hi, I just changed the
     
                    <GeometryModel3D.Transform>
                    	<TranslateTransform3D x:Name="myTranslateTransform3D" OffsetX="0" OffsetY="0" OffsetZ="0" />
                    </GeometryModel3D.Transform>
    

    for
     
                            <ModelVisual3D.Transform>
                                <TranslateTransform3D OffsetX="-2" />
                            </ModelVisual3D.Transform>

    and it worked.
    The thing is that my TrackballDecorator and the Interactive3DDecorator make my movement not work. Any tip way? Maybe they hold the events first...

    Pedro Martins Dusso Computer Science Student @ UFRGS www.inf.ufrgs.br/~pmdusso
    • Marcado como Resposta Pedro Dusso quarta-feira, 30 de novembro de 2011 07:35
    quinta-feira, 10 de novembro de 2011 14:08

Todas as Respostas

  • Hi, I just changed the
     
                    <GeometryModel3D.Transform>
                    	<TranslateTransform3D x:Name="myTranslateTransform3D" OffsetX="0" OffsetY="0" OffsetZ="0" />
                    </GeometryModel3D.Transform>
    

    for
     
                            <ModelVisual3D.Transform>
                                <TranslateTransform3D OffsetX="-2" />
                            </ModelVisual3D.Transform>

    and it worked.
    The thing is that my TrackballDecorator and the Interactive3DDecorator make my movement not work. Any tip way? Maybe they hold the events first...

    Pedro Martins Dusso Computer Science Student @ UFRGS www.inf.ufrgs.br/~pmdusso
    • Marcado como Resposta Pedro Dusso quarta-feira, 30 de novembro de 2011 07:35
    quinta-feira, 10 de novembro de 2011 14:08
  • Pedro,

    Favor postar somente questões em português nos fóruns da MSDN / TechNet em PT-BR...

    Obrigado pelo apoio...


    André Alves de Lima
    Microsoft MVP - Client App Dev
    Visite o meu site: http://www.andrealveslima.com.br
    Me siga no Twitter: @andrealveslima
    quinta-feira, 10 de novembro de 2011 20:21
    Moderador
  • Olá, não tinha notado que o fórum era em PT-BR... achei que estava no Inglês.
    Pedro Martins Dusso Computer Science Student @ UFRGS www.inf.ufrgs.br/~pmdusso
    domingo, 13 de novembro de 2011 16:08