Table of Contents

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.

Stride UI Example

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 the Game class. The Game class is the central part of the Stride engine, managing the overall game loop, the scenes, and the updates to the entities. The using keyword ensures that the Dispose() method is called on the game object when it goes out of scope, ensuring that any resources it uses are properly cleaned up
  • game.Run(start: (Scene rootScene) => This line initiates the game loop. The Run 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. The rootScene 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. The Create3DPrimitive 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. The Position property of the Transform component determines the location of the entity.
  • entity.Scene = rootScene; Finally, the entity is added to the rootScene. The Scene property of an entity determines which scene it belongs to.