tags:

views:

401

answers:

2

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>
A: 

Hi Cooper, How did you limit it to Z axis only then? I'm trying to do the same thing.

(edit - sorry, meant to post this as a comment, it's not an answer)

Alan
try SMART_n's code.
Cooper.Wu
A: 

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;
    }
SMART_n
Great, work very well.
Cooper.Wu