空间与运动

以下是 3D 游戏编程与设计课程中空间与运动章节的作业。

简答并用程序验证

  1. 游戏对象运动的本质是什么?

    游戏运动本质就是使用矩阵变换(平移、旋转、缩放)改变游戏对象的空间属性。

  2. 请用三种以上的方法,实现物体的抛物线运动。

    假设某个质量为 1 kg 的物体具有 x 轴方向上的初始速度,并且仅受 y 轴方向的重力影响,那么可以用以下的方法来模拟物体的抛物线运动:

    • 分别在 x 轴方向和 y 轴方向修改 Transform 的位置属性。
     using UnityEngine;
    
     public class CubeMove : MonoBehaviour {
         private float xSpeed;
         private float ySpeed;
         private float g; // gravity acceleration
    
         void Start () {
             xSpeed = 10f;
             ySpeed = 0f;
             g = 9.80665f;
         }
    
         void Update () {
             ySpeed += g * Time.deltaTime;
             transform.position += Vector3.right * Time.deltaTime * xSpeed;
             transform.position += Vector3.down * Time.deltaTime * ySpeed;
         }
     }
    
    • 直接使用 Vector3 来修改 Transform 的位置属性。
     using UnityEngine;
    
     public class CubeMove : MonoBehaviour {
         private Vector3 speed;
         private Vector3 gravity;
    
         void Start () {
             speed = new Vector3(10f, 0f, 0f);
             gravity = new Vector3(0f, -9.80665f, 0f);
         }
    
         void Update () {
             speed += gravity * Time.deltaTime;
             transform.position += speed * Time.deltaTime;
         }
     }
    
    • 利用 Transform 的 Translate 方法来修改物体的位置。
     using UnityEngine;
    
     public class CubeMove : MonoBehaviour {
         private Vector3 speed;
         private Vector3 gravity;
    
         void Start () {
             speed = new Vector3(10f, 0f, 0f);
             gravity = new Vector3(0f, -9.80665f, 0f);
         }
    
         void Update () {
             speed += gravity * Time.deltaTime;
             Vector3 translation = speed * Time.deltaTime;
             transform.Translate(translation);
         }
     }
    
    • 为物体添加刚体(Rigidbody)元素,并调用 AddForce 方法向物体施加重力。
     using UnityEngine;
    
     public class CubeMove : MonoBehaviour {
         private Rigidbody rb;
         private float g; // gravity acceleration
    
         void Start () {
             rb = GetComponent<Rigidbody>();
             rb.mass = 1;
             rb.velocity = new Vector3(10f, 0f, 0f);
             g = 9.80665f;
         }
    
         void FixedUpdate() {
             rb.AddForce(Vector3.down * g);
         }
     }
    
  3. 写一个程序,实现一个完整的太阳系,其他星球围绕太阳的转速必须不一样,且不在一个法平面上。

    首先,在 planetpixelemporium.com 上下载太阳与八大行星的图片:

    然后将这些图片转换为材质(Material):

    创建 9 个 Sphere,表示太阳和八大行星。然后将制作好的材质加入对应的星球中:

    为了模拟公转,需要创建脚本 Revolution.cs 并组合于各个行星之中,用 Transform 的 RotateAround 方法使得行星能够围绕太阳。

     using UnityEngine;
    
     public class Revolution : MonoBehaviour {
         public Transform sun;
         private float speed;
         private Vector3 axis;
    
         void Start() {
             speed = Random.Range(10f, 30f);
             axis = new Vector3(Random.Range(-30f, 30f), Random.Range(-30f, 30f), 0f);
         }
    
         void Update() {
             // revolution around the Sun
             transform.RotateAround(sun.position, axis, speed * Time.deltaTime);
         }
     }
    

    为了模拟自转,需要创建脚本 Rotation.cs 并组合于太阳和八大行星之中,用 Transform 的 RotateAround 方法使得太阳和行星能够旋转。

     using UnityEngine;
    
     public class Rotation : MonoBehaviour {
         void Update() {
             transform.RotateAround(transform.position, Vector3.up, Random.Range(1f, 5f));
         }
     }
    

    最后,在各个行星中加入 Trail Renderer 元素,方便观察行星的运行轨迹。

编程实践

阅读以下游戏脚本

Priests and Devils

Priests and Devils is a puzzle game in which you will help the Priests and Devils to cross the river within the time limit. There are 3 priests and 3 devils at one side of the river. They all want to get to the other side of this river, but there is only one boat and this boat can only carry two persons each time. And there must be one person steering the boat from one side to the other side. In the flash game, you can click on them to move them and click the go button to move the boat to the other direction. If the priests are out numbered by the devils on either side of the river, they get killed and the game is over. You can try it in many > ways. Keep all priests alive! Good luck!

  1. 游戏中提及的事物

    • 牧师

    • 恶魔

    • 河流

  2. 玩家动作

    • 点击牧师或恶魔

      • 如果牧师或恶魔在船只停靠的岸上,并且船上有空位,则将牧师或恶魔移动到船上。

      • 如果牧师或恶魔在船上,则将牧师或恶魔移动到当前船只停靠的岸上。

    • 点击船只

      • 将船只移动到对岸。
  3. 规则

    • 如果任意一岸的恶魔数大于牧师数且牧师数不为零,则游戏失败

    • 如果牧师和恶魔全部到达对岸,则游戏胜利

Updated: