Capsule with rigid body
This code example demonstrates how to initialize a game, set up a basic 3D scene, add a skybox, create a 3D capsule entity, set its position, and add it to the scene using the extensions provided by the toolkit. The Create3DPrimitive()
method, a part of the toolkit, automatically equips the capsule entity with a rigid body and a collider. This example serves as a simple starting point for building a game with Stride, leveraging the utilities provided by the toolkit to simplify common game development tasks.
Note
This example requires the additional NuGet package Stride.CommunityToolkit.Skyboxes
. Make sure to install it before running the code.
View on GitHub.
using Stride.CommunityToolkit.Engine;
using Stride.CommunityToolkit.Rendering.ProceduralModels;
using Stride.CommunityToolkit.Skyboxes;
using Stride.Core.Mathematics;
using Stride.Engine;
using var game = new Game();
game.Run(start: (Scene rootScene) =>
{
game.SetupBase3DScene();
game.AddSkybox();
var entity = game.Create3DPrimitive(PrimitiveModelType.Capsule);
entity.Transform.Position = new Vector3(0, 8, 0);
entity.Scene = rootScene;
});
using var game = new Game();
This line of code creates a new instance of theGame
class. TheGame
class is the central part of the Stride engine, managing the overall game loop, the scenes, and the updates to the entities. Theusing
keyword ensures that theDispose()
method is called on thegame
object when it goes out of scope, ensuring that any resources it uses are properly cleaned upgame.Run(start: (Scene rootScene) =>
This line initiates the game loop. TheRun
method is responsible for starting the game, and it takes a delegate as a parameter. This delegate is a function that is called once when the game starts. TherootScene
parameter represents the main scene of your game.game.SetupBase3DScene();
This line sets up a basic 3D scene. It's a helper method provided to quickly set up a scene with a default camera, lighting.game.AddSkybox();
This line adds a skybox to the scene. A skybox is a cube that surrounds the entire scene and is textured with an image to create the illusion of a sky.var entity = game.Create3DPrimitive(PrimitiveModelType.Capsule);
Here, a new entity is created in the form of a 3D capsule primitive. TheCreate3DPrimitive
method is another helper method provided to create basic 3D shapes.entity.Transform.Position = new Vector3(0, 8, 0);
This line sets the position of the created entity in the 3D space. ThePosition
property of theTransform
component determines the location of the entity.entity.Scene = rootScene;
Finally, the entity is added to therootScene
. TheScene
property of an entity determines which scene it belongs to.