English 中文(简体)
How to rotate 3d object on Z axis only?
原标题:
  • 时间:2009-11-16 07:13:24
  •  标签:
  • wpf
  • 3d

We used 3DTools (http://3dtools.codeplex.com) to draw a 3d line, it allows user rotate it by mouse. I have question, how to limit user can rotate it on Z axis only? or on X axis, Y axis only?


<tool:TrackballDecorator >
    <tool:Interactive3DDecorator 
         ContainsInk="True">
        <Viewport3D>
            <Viewport3D.Camera>
                <PerspectiveCamera x:Name="camera1" Position="4.89,-11,5" LookDirection="-4.89,11,-5"
                                   FieldOfView="45" UpDirection="-4,9,-1"/>
            </Viewport3D.Camera>
            <ModelVisual3D x:Name="modelVisual3D">
                <ModelVisual3D.Children>
                    <tool:ScreenSpaceLines3D x:Name="axisX" Color="Cyan"
                        Thickness="2.0"
                        Points="0, 0, 0, 5, 0, 0" />
                    <tool:ScreenSpaceLines3D  Color="LightCyan"
                        Thickness="2.0"
                        Points="-5, 0, 0, 0, 0, 0" />
                    <tool:ScreenSpaceLines3D x:Name="axisY" Color="Green"
                        Thickness="2.0"
                        Points="0,0,0, 0,5,0"/>
                    <tool:ScreenSpaceLines3D  Color="LightGreen"
                        Thickness="2.0"
                        Points="0,-5,0, 0,0,0"/>

                    <tool:ScreenSpaceLines3D x:Name="axisZ" Color="Red"
                        Thickness="2.0"
                        Points="0,0,0, 0,0,5"/>
                    <tool:ScreenSpaceLines3D  Color="LightPink"
                        Thickness="2.0"
                        Points="0,0,-5, 0,0,0"/>
                </ModelVisual3D.Children>
            </ModelVisual3D>
        </Viewport3D>
    </tool:Interactive3DDecorator>
</tool:TrackballDecorator>
最佳回答

Replace Trackball.Track() method in Trackball.cs with this one:

private void Track(Point currentPosition)
    {
        Vector3D currentPosition3D = ProjectToTrackball(
            EventSource.ActualWidth, EventSource.ActualHeight, currentPosition);

        Vector3D axisToRotate = new Vector3D(0, 1, 0); // Rotation around Y only

        Vector3D currProjected = Vector3D.CrossProduct(axisToRotate, currentPosition3D);
        Vector3D prevProjected = Vector3D.CrossProduct(axisToRotate, _previousPosition3D);
        double angle = Vector3D.AngleBetween(currProjected, prevProjected);            

        int sign = Math.Sign(Vector3D.DotProduct(
            axisToRotate, 
            Vector3D.CrossProduct(_previousPosition3D, currentPosition3D)));

        if (sign != 0)
        {
            Quaternion delta = new Quaternion(axisToRotate * sign, -angle);

            AxisAngleRotation3D r = _rotation;
            Quaternion q = new Quaternion(_rotation.Axis, _rotation.Angle);

            q *= delta;

            _rotation.Axis = q.Axis;
            _rotation.Angle = q.Angle;
        }

        _previousPosition3D = currentPosition3D;
    }
问题回答

暂无回答




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签