Table of Contents

2D Primitives

Create a minimal 2D scene using toolkit helpers and place multiple different primitive shapes. Demonstrates entity creation, basic positioning, and attaching the entities to the scene.

The Program.cs file shows how to:

  • Creating multiple 2D primitives (Circle, Capsule, Rectangle, Square, RegularPolygon, Triangle, Polygon)
  • Positioning entities with Transform.Position
  • Adding entities to a Scene (rootScene)
  • Using helpers: SceneSetup2D, AddGrabber

2D Primitives

View on GitHub.

using Stride.CommunityToolkit.Bepu;
using Stride.CommunityToolkit.Engine;
using Stride.CommunityToolkit.Rendering.ProceduralModels;
using Stride.Core.Mathematics;
using Stride.Engine;

// The five colours of the first examples, one shape after another
Color[] colours = [new(30, 70, 170), new(240, 150, 60), new(235, 100, 80), new(110, 200, 110), new(190, 130, 230)];

Vector2[] parallelogram = [new(-0.5f, -0.25f), new(0.5f, -0.25f), new(0.75f, 0.25f), new(-0.25f, 0.25f)];

Primitive2D[] shapes =
[
    Primitive2D.Circle(),
    Primitive2D.Capsule(),
    Primitive2D.Rectangle(),
    Primitive2D.Square(),
    Primitive2D.RegularPolygon(),
    Primitive2D.Triangle(),
    Primitive2D.Polygon(parallelogram),
];

using var game = new Game();

game.Run(new SceneSetup2D().UseBepu(), start: Start);

void Start(Scene rootScene)
{
    // Left mouse grabs any body, drags it and throws it on release
    game.AddGrabber();

    foreach (var (index, shape) in shapes.Index())
    {
        var entity = game.CreatePrimitive(shape, new()
        {
            Material = game.CreateFlatMaterial(colours[index % colours.Length]),
        });

        entity.Transform.Position = new Vector3(0, 10 + index * 1.5f, 0);
        entity.Scene = rootScene;
    }
}