Skip to content

Instantly share code, notes, and snippets.

@havarnov
Created December 4, 2025 20:38
Show Gist options
  • Select an option

  • Save havarnov/299e229b98c521a0444d5dad41651858 to your computer and use it in GitHub Desktop.

Select an option

Save havarnov/299e229b98c521a0444d5dad41651858 to your computer and use it in GitHub Desktop.
Program.cs
/*
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace>monogame_playground</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aether.Physics2D" Version="2.2.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.4.1" />
</ItemGroup>
</Project>
*/
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using nkast.Aether.Physics2D.Collision.Shapes;
using nkast.Aether.Physics2D.Dynamics;
using Vector2 = nkast.Aether.Physics2D.Common.Vector2;
using var game = new PlaygroundGame();
game.Run();
public class PlaygroundGame : Game
{
private World world = null!;
private SpriteBatch spriteBatch = null!;
private Body hero = null!;
public PlaygroundGame()
{
_ = new GraphicsDeviceManager(this);
}
protected override void Initialize()
{
world = new World
{
Gravity = new Vector2(0, 0),
};
hero = world.CreateBody(new Vector2(0, 0), bodyType: BodyType.Dynamic);
hero.CreateRectangle(30, 10, 1, Vector2.Zero);
var movable = world.CreateBody(new Vector2(150, 150), bodyType: BodyType.Dynamic);
movable.CreateRectangle(5, 5, 10f, new Vector2(0, 0));
movable.LinearDamping = 3;
var wall = world.CreateBody(new Vector2(100, 100), bodyType: BodyType.Static);
wall.CreateRectangle(40, 40, 1f, new Vector2(0, 0));
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
base.LoadContent();
}
protected override void Update(GameTime gameTime)
{
var keyboardState = Keyboard.GetState();
var horizontalInput =
(keyboardState.IsKeyDown(Keys.Left) ? -1 : 0)
+ (keyboardState.IsKeyDown(Keys.Right) ? 1 : 0);
var verticalInput =
(keyboardState.IsKeyDown(Keys.Up) ? -1 : 0)
+ (keyboardState.IsKeyDown(Keys.Down) ? 1 : 0);
var moveVector = new Vector2(horizontalInput, verticalInput);
if (moveVector == Vector2.Zero)
{
hero.LinearDamping = 4;
}
else if (moveVector.X == 0)
{
var c = hero.LinearVelocity;
c.Y = moveVector.Y * 60;
c.Normalize();
hero.LinearVelocity = c * 60;
hero.LinearDamping = 4;
}
else if (moveVector.Y == 0)
{
var c = hero.LinearVelocity;
c.X = moveVector.X * 60;
c.Normalize();
hero.LinearVelocity = c * 60;
hero.LinearDamping = 4;
}
else
{
hero.LinearVelocity = moveVector * 60;
}
world.Step((float)gameTime.ElapsedGameTime.TotalSeconds);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin(SpriteSortMode.FrontToBack, null, SamplerState.PointClamp);
BasicEffect basicEffect = new BasicEffect(GraphicsDevice);
var _t = new Texture2D(GraphicsDevice, 1, 1);
_t.SetData([Color.Blue]);
basicEffect.Texture = _t;
basicEffect.TextureEnabled = true;
foreach (var body in world.BodyList)
{
if (body.FixtureList[0].Shape is PolygonShape s)
{
var _texture = new Texture2D(GraphicsDevice, 1, 1);
_texture.SetData([Color.Blue]);
var aabb = s.Vertices.GetAABB();
spriteBatch.Draw(
_texture,
new Rectangle(
new Point((int)(body.Position.X - aabb.Extents.X), (int)(body.Position.Y - aabb.Extents.Y)),
new Point((int)s.Vertices.GetAABB().Extents.X * 2, (int)s.Vertices.GetAABB().Extents.Y * 2)),
Color.White);
}
}
spriteBatch.End();
base.Draw(gameTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment