C# XNA Matrices - 使用矩阵计算月球绕轴的旋转

发布于 2024-11-13 00:24:46 字数 662 浏览 3 评论 0原文

我正在构建 3D 模拟。

我正在使用一个类来显示太阳、行星和行星。具有不同大小、纹理、速度和轨道的月亮。这是我的代码,它执行计算旋转+旋转的神奇工作

world = Matrix.CreateScale(size,size,size) * Matrix.CreateTranslation(0,0,0) * 
 Matrix.CreateRotationY(rotation) * 
 Matrix.CreateTranslation(position) * 
 Matrix.CreateRotationY(revolution); 
  • 尺寸是我的变量,用于单独缩放对象
  • 旋转是我的变量,用于围绕其自己的轴旋转对象
  • >position 是我将行星定位在轨道上的变量,new Vector3(240,0,0)
  • revolution 是我将行星围绕原点旋转的变量

,现在我想要显示卫星围绕地球和其他行星,但我只是不知道该怎么做。就矩阵乘法而言。

复杂的是月球绕着一颗行星公转,而这颗行星不在原点并且总是在变化。

怎么做?

I am building a 3D simulation.

I am using one class to display sun, planets & moon with different size, texture, speed and orbit. Here's my code which does the magic job of calculating rotation + revolution

world = Matrix.CreateScale(size,size,size) * Matrix.CreateTranslation(0,0,0) * 
 Matrix.CreateRotationY(rotation) * 
 Matrix.CreateTranslation(position) * 
 Matrix.CreateRotationY(revolution); 
  • size is my variable to scale the object individually
  • rotation is my variable to rotate the object around its own axis
  • position is my variable to position the planet on orbit, new Vector3(240,0,0)
  • revolution is my variable to revolve the planet around the origin

now i want the display the moons around earth and around other planets but i just can't figure out how to do it. In terms of Matrix Multiplication.

The complication is the moon's revolution is around a planet which is not on origin and is always changing.

How to do it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹苦笑 2024-11-20 00:24:46

在您的 Draw 方法中,在绘制了您想要绕其运行的行星/月球之后:

Matrix satellite = Matrix.Identity
                 * Matrix.CreateScale(orbitScale)
                 * Matrix.CreateRotationY(orbitSpin)
                 * Matrix.CreateTranslation(0f, orbitDistance, 0f)
                 * Matrix.CreateFromAxisAngle(orbitAxis, orbitRotation);
effect.View = satellite * camera.View;

因此,在您的 Update 方法中,您可以执行以下操作

orbitRotation += MathHelper.PiOver2 * (float)gameTime.ElapsedGameTime.TotalSeconds;
orbitRotation = MathHelper.WrapAngle(orbitRotation);
orbitSpin += MathHelper.TwoPi * (float)gameTime.ElapsedGameTime.TotalSeconds;
orbitSpin = MathHelper.WrapAngle(orbitSpin);

:将orbitAxis定义为Vector3orbitDistanceorbitRotationorbitScaleorbitSpin 作为 float

In your Draw method, after you've drawn the planet/moon that you want to orbit around:

Matrix satellite = Matrix.Identity
                 * Matrix.CreateScale(orbitScale)
                 * Matrix.CreateRotationY(orbitSpin)
                 * Matrix.CreateTranslation(0f, orbitDistance, 0f)
                 * Matrix.CreateFromAxisAngle(orbitAxis, orbitRotation);
effect.View = satellite * camera.View;

So, in your Update method, you can then do:

orbitRotation += MathHelper.PiOver2 * (float)gameTime.ElapsedGameTime.TotalSeconds;
orbitRotation = MathHelper.WrapAngle(orbitRotation);
orbitSpin += MathHelper.TwoPi * (float)gameTime.ElapsedGameTime.TotalSeconds;
orbitSpin = MathHelper.WrapAngle(orbitSpin);

I've defined orbitAxis as a Vector3. orbitDistance, orbitRotation, orbitScale and orbitSpin as float's.

梦明 2024-11-20 00:24:46

我想通了...

将原点翻译为母行星的当前位置

并在更新方法中继续执行

world = Matrix.CreateScale(size,size,size)
        * Matrix.CreateTranslation(0,0,0) 
        * Matrix.CreateRotationY(rotation)
        * Matrix.CreateTranslation(position)
        * Matrix.CreateRotationY(revolution); 

world *= Matrix.CreateTranslation( parent.getPosition() );

我在这里所做的是让月球围绕原点(太阳)旋转,但是我将其起源翻译为任何行星的当前位置......这对我有用。

i figured it out...

Translate The Origin to the Parent planet's Current Location

and keep doing it in the update method

world = Matrix.CreateScale(size,size,size)
        * Matrix.CreateTranslation(0,0,0) 
        * Matrix.CreateRotationY(rotation)
        * Matrix.CreateTranslation(position)
        * Matrix.CreateRotationY(revolution); 

world *= Matrix.CreateTranslation( parent.getPosition() );

what i am doing here is that i m revolving the moon around the origin (Sun) but i translate its origin to any planet's current location... this worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文