生成物理对象
We need some geometric primitives that the player can move around the Play Area and shoot at. Let's generate them via code.我们需要一些几何元素,玩家可以移动游戏区域和射击。让我们通过代码生成它们。
A primitive should have a body and a shape to collide with the Player Character and be affected by gravity. We have implemented interaction with bullets via Intersections at the previous step, so we should set the BulletIntersection bit for the object's Intersection mask.一个基元应该有一个 body 和一个 shape 以与玩家角色发生碰撞并受重力影响。 我们在上一步已经通过 Intersections 实现了与子弹的交互,因此我们应该为对象的 Intersection 掩码设置 BulletIntersection 位。
When the robot moves fast and the application framerate is low, the character will be teleported each frame and may go through physical objects due to discrete collision detection. To avoid this we are going to use continuous collision detection.当机器人快速移动且应用程序帧率较低时,角色将每帧被传送,并且由于离散碰撞检测可能会穿过物理对象。 为了避免这种情况,我们将使用 连续碰撞检测。
Create a new C# component and call it ObjectGenerator. Copy the following code and save it in your IDE to ensure it's automatic compilation on switching back to UnigineEditor. This component generates the physical objects and places them in the world.创建一个新的 c#组件,命名为ObjectGenerator。复制以下代码并将其保存到IDE中,以确保在切换回UnigineEditor时自动编译。此组件生成物理对象并将其放置在世界中。
using System; using System.Collections; using System.Collections.Generic; using Unigine; [Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component public class ObjectGenerator : Component { private void Init() { // cube ObjectMeshDynamic box = Primitives.CreateBox(new vec3(1.0f)); box.TriggerInteractionEnabled = true; box.SetIntersection(true, 0); box.SetIntersectionMask(0x00000080, 0); // check the BulletIntersection bit box.WorldTransform = MathLib.Translate(new vec3(0.5f, 7.5f, 1.0f)); box.SetMaterialPath("materials/mesh_base_0.mat", "*"); box.Name = "box"; BodyRigid bodyBox = new BodyRigid(box); ShapeBox shapeBox = new ShapeBox(bodyBox, new vec3(1.0f)); new ShapeSphere(bodyBox, 0.5f); bodyBox.ShapeBased = false; bodyBox.Mass = 2.0f; // sphere ObjectMeshDynamic sphere = Primitives.CreateSphere(0.5f, 9, 32); sphere.TriggerInteractionEnabled = true; sphere.SetIntersection(true, 0); sphere.SetIntersectionMask(0x00000080, 0); // check the BulletIntersection bit sphere.WorldTransform = MathLib.Translate(new vec3(4.5f, 5.5f, 1.0f)); sphere.SetMaterialPath("materials/mesh_base_1.mat", "*"); sphere.Name = "sphere"; BodyRigid bodySphere = new BodyRigid(sphere); new ShapeSphere(bodySphere, 0.5f); bodySphere.ShapeBased = false; bodySphere.Mass = 2.0f; // capsule ObjectMeshDynamic capsule = Primitives.CreateCapsule(0.5f, 1.0f, 9, 32); capsule.TriggerInteractionEnabled = true; capsule.SetIntersection(true, 0); capsule.SetIntersectionMask(0x00000080, 0); // check the BulletIntersection bit capsule.WorldTransform = MathLib.Translate(new vec3(4.5f, 0.5f, 3.0f)); capsule.SetMaterialPath("materials/mesh_base_2.mat", "*"); capsule.Name = "capsule"; BodyRigid bodyCapsule = new BodyRigid(capsule); new ShapeCapsule(bodyCapsule, 0.5f, 1.0f); bodyCapsule.ShapeBased = false; bodyCapsule.Mass = 2.0f; } }
- Switch back to the UnigineEditor and create a new Dummy Node, rename it to "object_generator", and place it somewhere in the world.切换回UnigineEditor并创建一个新的Dummy Node,重命名它为"object_generator",它在世界上的地位。
- Attach the created ObjectGenerator component to the "object_generator" node to make it generate physical objects on the initialization.把ObjectGenerator组件创建"object_generator"节点生成物理对象的初始化。
-
Create 3 new materials in the UnigineEditor by choosing Create->Create Material in the Asset Browser. Make them children of the mesh_base material, give them names as specified in the previous code snippet (mesh_base_*), and move them to the data/materials folder of your project (create this folder if it doesn't exist).通过在 Asset Browser 中选择 Create->Create Material 在 UnigineEditor 中创建 3 个新材质。 将它们设为 mesh_base 材质的子级,按照前面代码片段 (mesh_base_*) 中指定的方式为其命名,然后将它们移动到 data/materials 项目的文件夹(如果该文件夹不存在,则创建该文件夹)。
-
Select materials one by one in the Materials window and in the Parameters window switch to the Parameters tab and click on the color field near Albedo to choose different colors for the objects via the color picker.在Materials窗口中一一选择材质,在Parameters窗口切换到Parameters选项卡点击Albedo附近的色域进行选择 通过颜色选择器为对象设置不同的颜色。
- Save changes to the world via Ctrl + S.通过Ctrl + S拯救世界的变化。
- Run the project via the UnigineEditor to see the spawned physical objects in the world.运行该项目通过UnigineEditor看到了世界上物理对象。