WPF 3D translation model + Animation

Source: Internet
Author: User

There are many examples of rotation on the Internet about WPF 3D, but there are not many examples of translation. This article is not about WPF 3D literacy. Therefore, you must have a certain understanding of WPF 3D. At least you must know the data structures such as viewport, perspectivecamera, and modelvisual3d. For more information about WPF 3D, see msdn: http://msdn.microsoft.com/zh-cn/library/ms747437.aspx.

 

1. Camera translation or Object Translation:

The WPF scenario mainly consists of these two parts: Camera and object. Imagine taking an object with a camera.

There are two ways to translate an object. One is to translate the camera, and the other is to translate the object. Camera translation is relatively complex and efficient; Object Translation efficiency is low, and implementation is simple.

This article implements Object Translation. If you want to translate the camera, you can stop reading it.

 

2. Operation Process for translation in this project: 

1.ProgramThe entire 3D scenario can be seen at startup, similar to a sand table. operations such as rotation will be centered around the Sand Table center;

2. Double-click a place in the sand table to move the Sand Table center to the double-click place. operations such as rotation will be performed around the new sand table center.

 

3. TranslatedAlgorithm:

1. Get the point corresponding to the mouse behind the double-click screen in 3D, mainly using the principle of Ray and 3D collision.CodeThe main application is the visualtreehelper. hittest function, which will pass the result into a callback function. Here we are htresultcenter.

Void bridgevisual_mousedoubleclick (Object sender, mousebuttoneventargs ARGs ){  

Point mouseposition = args. getposition (viewport );

Movecenter (mouseposition );

}

 

Public void movecenter (point mouseposition) {pointhittestparameters pointparams = new pointhittestparameters (mouseposition); visualtreehelper. hittest (viewport, null, htresultcenter, pointparams );

}

 

Private hittestresultbehavior htresultcenter (hittestresult result) {rayhittestresult rayresult = result as rayhittestresult; If (rayresult! = NULL) {// This is the coordinate var hitpoint = rayresult. pointhit ;...

}

}

 

2. obtain the point projected by the camera in 3D Based on the camera position + the Projection Direction of the camera. postion is the camera's position in the 3D world, camera. lookdirection is the camera's viewing direction. The two can be used together to obtain the position after the camera is projected.

 

/// /Camera location
VaR Camerapostion = camera. position;

/// /Orientation of the camera
VaR Lookdirection = camera. lookdirection;

///Obtains the point of the camera projected in 3D.
VaRX = camerapostion. x + lookdirection. X;
VaRY = camerapostion. Y + lookdirection. Y;
VaRZ = camerapostion. Z + lookdirection. Z;

 

 

3. You can use the camera projection position-double-click the position to obtain the amount of the object to be offset.Transform3dThis translation,
The animation doubleanimation is applied here, so there are a lot of code.

Doubleanimation doubleanimationx = New Doubleanimation ();
Doubleanimationx. begintime = New Timespan ( 0 , 0 , 0 );
Doubleanimationx. Duration = timespan. frommilliseconds ( 500 );
Doubleanimationx. From = transform3d. offsetx;
Doubleanimationx. To = x-hitpoint. X;

doubleanimation doubleanimationy = New doubleanimation ();
doubleanimationy. begintime = New timespan ( 0 , 0 , 0 );
doubleanimationy. duration = timespan. frommilliseconds ( 500 );
doubleanimationy. from = _ transform3d. offsety;
doubleanimationy. to = Y-hitpoint. y;

Doubleanimation doubleanimationz =NewDoubleanimation ();
Doubleanimationz. begintime =NewTimespan (0,0,0);
Doubleanimationz. Duration = timespan. frommilliseconds (500);
Doubleanimationz. From = transform3d. offsetz;
Doubleanimationz. To = z-hitpoint. Z;

Transform3d. beginanimation (translatetransform3d. offsetxproperty, doubleanimationx );
Transform3d. beginanimation (translatetransform3d. offsetyproperty, doubleanimationy );

Transform3d. beginanimation (translatetransform3d. offsetzproperty, doubleanimationz );

 

 

4. Final Results:

1. Before Translation:

 

2. After Translation:

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.