Advertisement

python模拟太阳系_用Unity3D实现太阳系仿真

阅读量:

用Unity3D模拟太阳系仿真

模拟要求

编写一段代码来构建一个完整的太阳系模型,在该模型中每个行星的运行速度各不相同,并且其轨道平面也不在同一平面上。

操作步骤

1.创建如下结构 sun 里包括8大行星, 并且设置好距离和大小

建立结构

d21b8bf393386540851eb5ba9b588b9a.png

建议用2D显示来直观设置距离

4b4cf96460080bd533b556deb2b61373.png

2.在网上找到相应贴图 添加到assets

贴图网址

网址失效 素材更新链接如下

链接: https://pan.baidu.com/s/1w0xSMt_0HgBLbVhco9b6GA 提取码: ead5

e378127c41c965101c9d10566e1bdcc5.png

而且将对应行星的贴图图片放置至对应的球体上(为白色小球上色)得到如下结果

a06feb87824ea79be27e6e939f245817.png

3.创建c#脚本 使每个行星绕太阳转

创建plantMove.cs文件 代码如下

把此脚本拖拽到 sun 体即可;代码就是在 sun 中查找各个行星,并启动运行它们的轨道参数。

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class plantMove : MonoBehaviour

{

// Use this for initialization

void Start()

{

}

// Update is called once per frame

void Update()

{

GameObject.Find("Sun").transform.Rotate(Vector3.up * Time.deltaTime * 5 );

Object FindInstance = GameObject.FindInstance("Mercury"); Vector3 rotationAxis = new Vector3(0.1f, 1f, 0f); transform_property.RotateAround(Vector3.zero, rotationAxis, Time.deltaTime *60);

//设置公转的方向和速度 方向轴为(0, 1, 0) 速度为 60

GameObject.Find("Mercury").transform.Rotate(Vector3.up * Time.deltaTime * 10000 / 58);

//设置自转 自转速度为10000/58 58是水星的自传周期 倒数就是时间 下同

GameObject.Receive("Venus").access(invoke("RotateAround", Vector3.zero, new Vector3(0, 1, -0.1f), Multiply(55 * Time.deltaTime)));

GameObject.Find("Venus").transform.Rotate(Vector3.up * Time.deltaTime * 10000 / 243);

查找"Earth" GameObject的位置并绕Y轴旋转一定角度数。

GameObject.Find("Earth").transform.Rotate(Vector3.up * Time.deltaTime * 10000);

Unity引擎中使用GameObject查找名为‘Moon’的组件,并对其进行变换操作以实现绕Y轴旋转的方式

Unity引擎中使用.Find("Moon")并对其.transform执行.Rotation.Around(Vector3.zero,new Vector3(0,1.0f,0.0f),5.0f*Time.deltaTime)的操作

GameObject.Find("Moon").transform.Rotate(Vector3.up * Time.deltaTime * 10000/27);

<GameObjectObject Locator("火星")>.position = GameObjectLocator.position;
object.position = object.position + object.position * (Vector3.new(0.2f, 1, 0) * Time.deltaTime * 45);

GameObject.Find("Mars").transform.Rotate(Vector3.up * Time.deltaTime * 10000);

查找实例"Jupiter"的网格变换绕其位置进行旋转

GameObject.Find("Jupiter").transform.Rotate(Vector3.up * Time.deltaTime * 10000 / 0.3f);

GameObject Find("Saturn") 的 transform 应用绕Vector3零轴的新Vector3(0,1,0.2f)旋转20倍的时间增量。

GameObject.Find("Saturn").transform.Rotate(Vector3.up * Time.deltaTime * 10000 / 0.4f);

Use GameObject to locate "Uranus", then Position the object to OrbitAround Vector3.zero with a new Vector3(0, 2, 0.1f) and rotate it by 15 multiplied by Time.deltaTime each frame.

GameObject.Find("Uranus").transform.Rotate(Vector3.up * Time.deltaTime * 10000 / 0.6f);

<GameObject.Find("Neptune")>.Locate celestial body named Neptune from GameObject and adjust its Position by Orbit Around Zero Vector with a rotation of (-0.1, 1, -0.1) multiplied by ten times Current Time Step.

GameObject.Find("Neptune").transform.Rotate(Vector3.up * Time.deltaTime * 10000 / 0.7f);

}

}

代码解释:

该系统利用GameObject.Find("value")的方式定位各球体,并采用RotateAround()方法完成公转轨道设置;同时使用Rotate()动作实现自传动作。

4. 进一步思考

在运行过程中发现月球绕地球的轨道运转存在不协调之处,在这种情况下分析认为其主要原因在于地球自转对月球公转产生了显著影响。具体来说,在一个正在自转的地球上观察到的现象是:相对于一个正在自转的地球而言,月球围绕其公转的过程中还叠加了一种因地球自身转动带来的影响因素。

所以怎么去处理这个问题呢? 怎么让月球公转和地球自转不相关呢?

这里有一个解决方案

将一个命名为EarthClone的对象添加到原始的Sun中。该克隆行星的尺寸与地球完全一致。随后我为该克隆行星配置为与地球相同的轨道运行但不具备自转。为了实现这一目标我将月球放置于该克隆行星上并使其围绕此克隆行星运行以解决现有问题。

文件结构:

c8caa819ee7ca7e25cad63b1a2648ca8.png

加入和更改下列代码

void Update()

{

GameObject.Find("Sun").transform.Rotate(Vector3.up * Time.deltaTime * 5 );

Locate "Mercury" using GameObjects component. Position the object by rotating it around the zero vector with a new Vector3 (x=0.1f, y=1, z=0) for an angle equal to 60 multiplied by Time.deltaTime.

GameObject.Find("Mercury").transform.Rotate(Vector3.up * Time.deltaTime * 1 / 58);

通过名称查找名为'Venus'的 GameObject,并将其变换绕该轴旋转一定角度。具体来说,在该变换中使用了一个向量(0,1,-0.1),用于定义旋转轴,并将每秒的时间片乘以55个单位来控制旋转速度。

GameObject.Find("Venus").transform.Rotate(Vector3.up * Time.deltaTime * 10 / 243);

GameObject Instantiate Earth. Then Execute the following command on its transform: CalculateRotationAround ZeroVector with ConstructVector(0,1,0) as the axis and MultiplyScalarByTimeDelta(50.0f) as the rotation speed.

GameObject.Find("Earth").transform.Rotate(Vector3.up * Time.deltaTime * 10);

//只设置公转 不设置自传

<GameObject.DNS查找("EarthClone")>.transform.RotateAroundLocal(Vector3.zero, Vector3(0, 0, 0), Vector3.right * (50 * Time.deltaTime));

//令Moon相对与不自转的EarthClone公转

物体locate("Moon").position.RotateAround(物体locate("EarthClone")的位置, 创建一个新的Vector3实例(0, 1, 0), 250乘以当前帧的时间间隔);

GameObject.Find("Moon").transform.Rotate(Vector3.up * Time.deltaTime * 10/27);

Objectbodies.Locate("Mars").transform.ApplyAxisRotation(zero, new Vector3(0.2f, 1, 0), 45 * Time.deltaTime);

GameObject.Find("Mars").transform.Rotate(Vector3.up * Time.deltaTime * 10);

This code retrieves the "Jupiter" object and performs a rotation operation on its position using the RotateAround method. The rotation is executed around a vector defined by (-0.1f, 2, 0) with an angle of 35 multiplied by Time.deltaTime.

GameObject.Find("Jupiter").transform.Rotate(Vector3.up * Time.deltaTime * 10 / 0.3f);

Object Locator "Saturn" to obtain its position and rotate it around the Y-axis by a calculated angle using the RotateAround method.

GameObject.Find("Saturn").transform.Rotate(Vector3.up * Time.deltaTime * 10 / 0.4f);

A GameObject to retrieve Uranus from the Unity scene will have its position set to rotate around an axis defined by a new Vector3 with components (0, 2, 0.1f) during each time step multiplied by fifteen.

GameObject.Find("Uranus").transform.Rotate(Vector3.up * Time.deltaTime * 10 / 0.6f);

Object located using the Find method in order to position the object in space in order to achieve orbit around a central point using a specific vector for rotation and a time-based multiplier.

GameObject.Find("Neptune").transform.Rotate(Vector3.up * Time.deltaTime * 10 / 0.7f);

}

5.结果显示

把cs脚本拖到Sun对象里 运行查看结果

6bed892b25b60c25415cf61be4f4687b.png

全部评论 (0)

还没有任何评论哟~