首页 > 编程语言 >SYSU-SSE 3D游戏编程与设计 学习笔记(2)--空间与运动

SYSU-SSE 3D游戏编程与设计 学习笔记(2)--空间与运动

时间:2022-10-24 17:35:10浏览次数:75  
标签:Time SYSU Vector3 deltaTime -- position SSE new speed

前言

中山大学软件工程学院 3D游戏编程与设计课程学习记录博客
游戏代码: 游戏代码

简答题

  1. 游戏对象运动的本质是什么
  • 游戏对象的运动过程本质上就是游戏对象transform属性中的空间位置(Position)、旋转角度(Rotation)、大小(Scale)三个属性随着时间在做某种特定的变化。
  1. 请用三种方法以上方法,实现物体的抛物线运动
  • 使用两个Script修改 this.transform.position ,一个负责物体的自由加速下落,一个负责物体垂直重力方向的匀速运动

    物体向右匀速运动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Moveright : MonoBehaviour
{
    private int speed = 10;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.position += Vector3.right * Time.deltaTime * speed;
        //this.transform.position += Vector3.up * Time.deltaTime;
    }
}

物体自由下落

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fall : MonoBehaviour
{
    private float g = 9.8F;
    private float speed;
    // Start is called before the first frame update
    void Start()
    {
        speed = 0;
    }

    // Update is called once per frame
    void Update()
    {
        speed += g * Time.deltaTime;
        this.transform.position += Vector3.down * Time.deltaTime * speed;
    }
}
  • 使用一个Script调用 this.transform.Translate
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Parabola : MonoBehaviour
{
    private float g = 9.8F;
    private float speed;
    private float fallingSpeed;
    // Start is called before the first frame update
    void Start()
    {
        speed = 10;
        fallingSpeed = 0;
    }

    // Update is called once per frame
    void Update()
    {
        fallingSpeed += g * Time.deltaTime;
        this.transform.Translate(Vector3.left * speed * Time.deltaTime);
        this.transform.Translate(Vector3.down * fallingSpeed * Time.deltaTime);
    }
}
  • 调用 Mathf.MoveTowards 获取下一个位置的坐标并赋值给 this.transform.position
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fall3 : MonoBehaviour
{
    private float g = 9.8F;
    private float speed;
    private float falling_speed;
    // Start is called before the first frame update
    void Start()
    {
        speed = 10;
        falling_speed = 0;
    }

    // Update is called once per frame
    void Update()
    {
        falling_speed += g * Time.deltaTime;
        float target_y = Mathf.MoveTowards(this.transform.position.y, this.transform.position.y - falling_speed * Time.deltaTime, speed * Time.deltaTime);
        float target_x = Mathf.MoveTowards(this.transform.position.x, this.transform.position.x + speed * Time.deltaTime, falling_speed * Time.deltaTime);
        this.transform.position = new Vector3(target_x, target_y, this.transform.position.z);
    }
}
  1. 写一个程序,实现一个完整的的太阳系,其他星球围绕太阳的转速必须不一样,且不在一个法平面
  • 脚本代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SolarSystem : MonoBehaviour
{
    public Transform Sun;
    public Transform Mercury;
    public Transform Venus;
    public Transform Earth;
    public Transform Moon;
    public Transform Mars;
    public Transform Jupiter;
    public Transform Saturn;
    public Transform Uranus;
    public Transform Neptune;
    // Start is called before the first frame update
    void Start()
    {
        init_planetScale();
        init_planetPosition();
    }

    // Update is called once per frame
    void Update()
    {
        Mercury.RotateAround(Sun.position, new Vector3(0.3f, 1, 0), 10 * 365 / 87.7f * Time.deltaTime);
        Venus.RotateAround(Sun.position, new Vector3(0.2f, 1, 0), 10 * 365 / 224.7f * Time.deltaTime);
        Earth.RotateAround(Sun.position, Vector3.up, 10 * Time.deltaTime);
        Earth.Rotate(Vector3.up * 30 * Time.deltaTime);
        Moon.RotateAround(Earth.position, Vector3.up, 365 * Time.deltaTime);
        Mars.RotateAround(Sun.position, new Vector3(0.5f, 1, 0), 10 * 365 / 686.98f * Time.deltaTime);
        Jupiter.RotateAround(Sun.position, new Vector3(0.5f, 1, 0), 10 * 1 / 11.8f * Time.deltaTime);
        Saturn.RotateAround(Sun.position, new Vector3(0.6f, 1, 0), 10 * 1 / 29.5f * Time.deltaTime);
        Uranus.RotateAround(Sun.position, new Vector3(0.23f, 1, 0), 10 * 1 / 80.4f * Time.deltaTime);
        Neptune.RotateAround(Sun.position, new Vector3(0.17f, 1, 0), 10 * 1 / 164.8f * Time.deltaTime);
    }

    void init_planetScale()
    {
        this.Sun.localScale = new Vector3(20, 20, 20);
        this.Mercury.localScale = new Vector3(1.5F, 1.5F, 1.5F);
        this.Venus.localScale = new Vector3(3.6F, 3.6F, 3.6F);
        this.Earth.localScale = new Vector3(3.6F, 3.6F, 3.6F);
        this.Moon.localScale = new Vector3(0.9F, 0.9F, 0.9F);
        this.Mars.localScale = new Vector3(2.1F, 2.1F, 2.1F);
        this.Jupiter.localScale = new Vector3(14, 14, 14);
        this.Saturn.localScale = new Vector3(12, 12, 12);
        this.Uranus.localScale = new Vector3(5, 5, 5);
        this.Neptune.localScale = new Vector3(5, 5, 5);
    }
    void init_planetPosition()
    {
        this.Sun.position = Vector3.zero;
        this.Mercury.position = new Vector3(24, 0.2f, 0.02f);
        this.Venus.position = new Vector3(32, 0.03f, 0.3f);
        this.Earth.position = new Vector3(40, 0.07f, 0.7f);
        this.Moon.position = new Vector3(45, 0.07f, 0.7f);
        this.Mars.position = new Vector3(60, 0.09f, 0.9f);
        this.Jupiter.position = new Vector3(80, 0.8f, 0.08f);
        this.Saturn.position = new Vector3(100, 0.06f, 0.6f);
        this.Uranus.position = new Vector3(120, 0.3f, 0.39f);
        this.Neptune.position = new Vector3(140, 0.7f, 0.79f);
    }
}
  • 在菜单栏选择 GameObject ,选择 Create Empty, 将脚本搭载到新建的游戏对象中,再使用 GameObject3D Object 中的 Sphere 新建多个球体来代表行星,并在Unity的Asset Store下载了一个 行星的资源包,并将行星搭载到脚本上
  • 运行结果

编程实践

标签:Time,SYSU,Vector3,deltaTime,--,position,SSE,new,speed
From: https://www.cnblogs.com/joshf/p/16817539.html

相关文章

  • MUI调用照片以及裁剪和图库照片上传到服务器【后端部分Flask+MUI】
    MUI调用照片以及裁剪和图库照片上传到服务器【后端部分】涉及技术:前端:MUI后端:Flask数据库:MongoDB一.MUI后端接受照片1.1主要的py文件先设置固定接受照片路由,用于......
  • C++ 实现随机数生成(Windows、Linux)
    文章目录​​1、简介​​​​2、windows随机数​​​​2.1随机数范围计算公式​​​​2.2rand()​​​​2.3srand()​​​​2.4c++11<random>​​​​2.4.1随机数生成......
  • 3
    T1简单红题,不懈于写。锐评:镜子反射出来的竟然没有镜像一下。T2坑人东西调了2h。类似于round1的T4。线性\(\Theta(n)\)过。T3T4......
  • 序列化器的嵌套
    序列化器的嵌套(1)新建一个应用pythonmanage.pystartappschool(2)注册应用INSTALLED_APPS=[#'django.contrib.admin','django.contrib.auth',......
  • 为什么说C++太复杂?复杂的必要性是为什么?
    1常见观点可以轻易的找出许多文献说明C++太复杂了,例如学习C++的书籍的厚度。这样以至于C++的设计者Bjarne都曾怀疑具有类的C是不是已经太庞大了。因为,总有大量对语言的......
  • OpenAPI 接口幂等实现
    OpenAPI接口幂等实现1、幂等性是啥?进行一次接口调用与进行多次相同的接口调用都能得到与预期相符的结果。通俗的讲,创建资源或更新资源的操作在多次调用后只生效一次。......
  • 面试 个人摸底监测 考察JavaScript基础 (第三天)
    01,如何开启JS严格模式?JS严格模式有什么特点?两种方式全局开启在js开头加上'usestrict'局部开启,在作用域开头加上functionfn(){'usestrict'}特点:1,全局变量必须......
  • Java并发编程学习10-任务执行与Executor框架
    任务执行何为任务?任务通常是一些抽象且离散的工作单元。大多数并发应用程序都是围绕着“任务执行”来构造的。而围绕着“任务执行”来设计应用程序结构时,首先要做的......
  • Codeforces Round #829 (Div. 2)/CodeForces1754
    CodeForces1754注:所有代码均为场上所书TechnicalSupport解析:题目大意给定一个只包含大写字母\(\texttt{Q}\)和\(\texttt{A}\)的字符串,如果字符串里的每一个\(\t......
  • JavaScript 设计模式之策略模式
    什么是设计模式?为什么需要学习设计模式?学习设计模式的目的是:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。设计模式使代码编写真正工程化;设计模式是软件工......