本科课程【虚拟现实引擎Unity3D】实验1 - ChaosBall(物理引擎应用)

x33g5p2x  于2022-05-13 转载在 其他  
字(3.1k)|赞(0)|评价(0)|浏览(388)

大家好,我是【1+1=王】, 热爱java的计算机(人工智能)渣硕研究生在读。
如果你也对java、人工智能等技术感兴趣,欢迎关注,抱团交流进大厂!!!
Good better best, never let it rest, until good is better, and better best.

近期会把自己本科阶段的一些课程设计、实验报告等分享出来,供大家参考,希望对大家有帮助。

博客更新至专栏【课程设计实验报告】:https://blog.csdn.net/weixin_43598687/category_11640051.html

一、 实验目的

  1. 掌握钢体的应用及相关控制方法、事件编程。

二、 实验内容

1. 实验任务

自己搭建一个类似台球桌面的场景,四个角设定为不同的颜色,场景中有四个和角颜色对应的大球和若干个小的干扰球(颜色可相同或不同),场景中还有一个可以四处移动并转运的挡板,可以用其改变球的运动方向。游戏开始后,各种球以不同的速度和方向移动并和台球桌边、其它球及挡板相碰,在球碰撞到同颜色的角时停下不再移动。当四个大球都停在角上时,显示游戏结束提示和完成时间并可结束或重新开始游戏。可根据需要增加其他内容。

2. 程序设计

1) 数据输入
均为初始化方式输入

2) 数据存储(输入数据在内存中的存储)
Plane存储地面;
Cube1-4存储场景的四边框
Sphere1-4存储运动的小球
Cylinder1-4存储四个角的圆柱体
3) 数据处理
(1)
新建一个Plane(10x10)地面,四个Cube(1x1x10),四个Spyder(0.5x0.5x0.5),四个cylinde(1x1x1);
建立五个材质球分别赋给不同的对象;
调整不同物体至对应位置

(2)
给所有Cube,Spyder,cylinde添加刚体组件。
禁用cude的重力属性,再freeze冻结所有cude的x,y,z坐标,使它们在运动过程中保持静止。
新建一个Physic Material,初始化所有阻力系数都为0

将新建的physic Material 拖动赋值给所有物体,使所有物体的运动阻力位0;

4) 数据输出

三、 实验环境

  1. 操作系统:WINDOWS 10
  2. 开发工具:Unity3D
  3. 实验设备:PC

源代码

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class Controller : MonoBehaviour {
  6. public Text text;
  7. private int count = 0;
  8. private bool isClick = false;
  9. // Use this for initialization
  10. void Start () {
  11. text.text = "white";
  12. text.color = Color.white;
  13. }
  14. // Update is called once per frame
  15. void Update () {
  16. System.Threading.Thread.Sleep(2000);
  17. if (isClick == false)
  18. {
  19. shunxuanzhuan();
  20. }
  21. else
  22. {
  23. nixuanzhuan();
  24. }
  25. }
  26. void shunxuanzhuan()
  27. {
  28. count++;
  29. if(count !=0&&count%4 == 0)
  30. {
  31. text.text = "white";
  32. text.color = Color.white;
  33. text.transform.position = new Vector2(text.transform.position.x, text.transform.position.y +50);
  34. }
  35. if (count % 4 == 1)
  36. {
  37. text.text = "green";
  38. text.color = Color.green;
  39. text.transform.position = new Vector2(text.transform.position.x+50, text.transform.position.y);
  40. }
  41. if (count % 4 == 2)
  42. {
  43. text.text = "yellow";
  44. text.color = Color.yellow;
  45. text.transform.position = new Vector2(text.transform.position.x, text.transform.position.y-50);
  46. }
  47. if (count % 4 == 3)
  48. {
  49. text.text = "red";
  50. text.color = Color.red;
  51. text.transform.position = new Vector2(text.transform.position.x - 50, text.transform.position.y);
  52. }
  53. }
  54. void nixuanzhuan()
  55. {
  56. count--;
  57. if (count != 0 && count % 4 == 0)
  58. {
  59. text.text = "white";
  60. text.color = Color.white;
  61. text.transform.position = new Vector2(text.transform.position.x - 50, text.transform.position.y);
  62. }
  63. if (count % 4 == 1)
  64. {
  65. text.text = "green";
  66. text.color = Color.green;
  67. text.transform.position = new Vector2(text.transform.position.x, text.transform.position.y-50);
  68. }
  69. if (count % 4 == 2)
  70. {
  71. text.text = "yellow";
  72. text.color = Color.yellow;
  73. text.transform.position = new Vector2(text.transform.position.x + 50, text.transform.position.y);
  74. }
  75. if (count % 4 == 3)
  76. {
  77. text.text = "red";
  78. text.color = Color.red;
  79. text.transform.position = new Vector2(text.transform.position.x, text.transform.position.y + 50);
  80. }
  81. }
  82. private void OnGUI()
  83. {
  84. if(GUI.Button(new Rect(0, 0, 50, 30), "Button"))
  85. {
  86. isClick = !isClick;
  87. }
  88. }
  89. }
  90. using System.Collections;
  91. using System.Collections.Generic;
  92. using UnityEngine;
  93. public class sport : MonoBehaviour {
  94. // Use this for initialization
  95. void Start () {
  96. GetComponent<Rigidbody>().AddForce(400, 0, 333);
  97. }
  98. // Update is called once per frame
  99. void Update () {
  100. }
  101. }

博客更新至专栏【课程设计实验报告】:https://blog.csdn.net/weixin_43598687/category_11640051.html

相关文章