如何在 C# 中为旋转立方体设置动画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/352771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:13:38  来源:igfitidea点击:

How to animate rotating cube in C#?

c#animation3dcube

提问by Tomasz Smykowski

I would like to do something like this: a rotating cube on a form. I don't want to use any external library or dll, just pure .NET 3.5 (without directx). And a cube build with lines only. Could you please tell me how to do this?

我想做这样的事情:表格上的旋转立方体。我不想使用任何外部库或 dll,只是纯 .NET 3.5(没有 Directx)。一个只用线条构建的立方体。你能告诉我怎么做吗?

I don't want to use external libraries because I don't need > 100 MB library to do this thing right? I want only to animate a rotating cube made with lines.

我不想使用外部库,因为我不需要 > 100 MB 的库来做这件事,对吗?我只想为一个用线条制作的旋转立方体制作动画。

采纳答案by Hath

This is how you go about making a Cube in GDI+

这就是你在 GDI+ 中制作立方体的方法

C# 3D Drawing with GDI+ Euler Rotation

使用 GDI+ Euler 旋转的 C# 3D 绘图

http://www.vcskicks.com/3d-graphics-improved.html

http://www.vcskicks.com/3d-graphics-improved.html

C# 3D-Drawing Cube with Shading

C# 3D-Drawing Cube with Shading

http://www.vcskicks.com/3d_gdiplus_drawing.html

http://www.vcskicks.com/3d_gdiplus_drawing.html

回答by Scott

You might try using WPF and the 3D Tools source code released by the WPF team.

您可以尝试使用 WPF 和 WPF 团队发布的 3D 工具源代码。

3DTools

3D工具

回答by Vilx-

Study assignment? This can be done with some simple 3D maths. You just need to understand the basics of matrix algebra, 3D transformations, and 3D->2D view transformation. The DirectX tutorialcovers this, but you can google for it and you'll get plenty of other tutorials.

学习任务?这可以通过一些简单的 3D 数学来完成。您只需要了解矩阵代数、3D 变换和 3D->2D 视图变换的基础知识。在DirectX的教程涉及这一点,但你可以谷歌它,你会得到很多其他教程。

Added:Just to clarify - I'm not suggesting to use DirectX or anything. You can do this with standard System.Drawing tools. You just need to understand the math, and that's explained in the DirectX tutorials.

补充:只是为了澄清 - 我不建议使用 DirectX 或任何东西。您可以使用标准 System.Drawing 工具执行此操作。您只需要了解数学,这在 DirectX 教程中进行了解释。

回答by sindre j

Look into WPF in general, it will help you do this with a few measly lines of code. You can also host a WPF window in Forms.

总的来说,看看 WPF,它会帮助你用几行微不足道的代码来做到这一点。您还可以在 Forms 中托管 WPF 窗口。

http://msdn.microsoft.com/en-us/library/aa970268.aspx

http://msdn.microsoft.com/en-us/library/aa970268.aspx

回答by NoizWaves

Assuming you are using WPF for your GUI:

假设您在 GUI 中使用 WPF:

  1. Make an animated PNG of the cube using a graphics program.
  2. Use the APNG WPF Controlto insert the image into your GUI.
  1. 使用图形程序制作立方体的动画 PNG。
  2. 使用APNG WPF 控件将图像插入到您的 GUI 中。

This will yield a small assembly size and transparent background if needed.

如果需要,这将产生小的装配尺寸和透明背景。

回答by NoizWaves

You need a way to represent 3d points. There is no ready struct for that in .NET unless you use directx or WPF.

您需要一种表示 3d 点的方法。除非您使用 directx 或 WPF,否则 .NET 中没有现成的结构。

Then with a standard euler rotation matrix applied to the points you get the transformed points. If you only do rotations you can get away with 3x3 matrix, but if you want translation you better use 4x4 matrices and homogenous points.

然后将标准欧拉旋转矩阵应用于点,您将获得转换点。如果您只进行旋转,则可以使用 3x3 矩阵,但如果您想要平移,则最好使用 4x4 矩阵和同质点。

After this you need a way to project those 3d points to the 2d canvas. Depending whether you are using perspective or orthographic projection the projection matrix will look a bit different.

在此之后,您需要一种将这些 3d 点投影到 2d 画布的方法。根据您使用的是透视投影还是正交投影,投影矩阵看起来会有些不同。