Skip to content

Instantly share code, notes, and snippets.

@arxae
Last active December 23, 2015 17:29
Show Gist options
  • Save arxae/6668686 to your computer and use it in GitHub Desktop.
Save arxae/6668686 to your computer and use it in GitHub Desktop.
Basic SFML.NET / Box2DX example
using System;
using System.Diagnostics;
using Box2DX.Collision;
using Box2DX.Common;
using Box2DX.Dynamics;
using SFML.Graphics;
using SFML.Window;
using Color = SFML.Graphics.Color;
namespace TestingSFML
{
static class Program
{
static void Main()
{
// Create the main window
RenderWindow app = new RenderWindow(new VideoMode(800, 600), "SFML Works!");
// Set the function to be called when window is closed
app.Closed += OnClose;
// Set the max framerate for the window
app.SetFramerateLimit(60);
// Set the background color of the window
Color windowColor = new Color(0, 192, 255);
// ----------- Start Physics World Setup
// Set gravity value
Vec2 gravity = new Vec2(0, 10.0f);
AABB worldAabb = new AABB();
worldAabb.LowerBound.Set(-200.0f, -100.0f);
worldAabb.UpperBound.Set(200.0f, 200.0f);
// Setup the world for Box2D
World world = new World(worldAabb, gravity, false);
CreateGround(world, 400, 500);
// Lambda Expression
// This is what happens whenever a mouse button is clicked
app.MouseButtonPressed += (sender, args) =>
{
int mouseX = Mouse.GetPosition(app).X;
int mouseY = Mouse.GetPosition(app).Y;
CreateBox(world, mouseX, mouseY);
};
// Make a stopwatch object
// We use this to calculate Elapsed Time
// We dont use this at all in this function
// I was just testing this out
Stopwatch calculatedElapsedTime = new Stopwatch();
// Start the game loop
while (app.IsOpen())
{
// Start the timer
calculatedElapsedTime.Start();
// Process events
app.DispatchEvents();
// We have to update the Physics World
// So we step through the process like this
world.Step(1 / 60.0f, 8, 1);
// Clear screen
app.Clear(windowColor);
// ------------- Start Drawing
// Iterate through the bodies
// draw what was passed in User Data
Body body = world.GetBodyList();
while (body.GetNext() != null)
{
// We get the sprite that was passed in from user data
Sprite sprite = (Sprite) body.GetUserData();
// We need to get the position from the physics world
// And then we reset the position of the sprite
sprite.Position = new Vector2f(30.0f * body.GetPosition().X, 30.0f * body.GetPosition().Y);
// We then need to do the same to the rotation
sprite.Rotation = body.GetAngle()*180/Settings.Pi;
// We then draw the sprite
app.Draw(sprite);
// And then we get the next body
body = body.GetNext();
}
// ------------ End Drawing
// We would stop the timer here
// And then reset it
calculatedElapsedTime.Stop();
// Get elapsed time
TimeSpan ts = calculatedElapsedTime.Elapsed;
// Make sure we reset the timer
calculatedElapsedTime.Reset();
// Output the time so we can see if its what we want
//Console.WriteLine(ts);
// Update the window
app.Display();
} //End game loop
} //End Main()
static void OnClose(object sender, EventArgs e)
{
// Close the window when OnClose event is received
RenderWindow window = (RenderWindow)sender;
window.Close();
}
// Lets create the ground
public static void CreateGround(World world, float x, float y)
{
// We need to define a body with the position
BodyDef bodyDef = new BodyDef();
// Box2D doesn't use pixel measurement
// So we need to divide by 30
// 1m = 30px
bodyDef.Position.Set(x / 30.0f, y / 30.0f);
// Create the physics body
Body body = world.CreateBody(bodyDef);
// Define a new shape def
PolygonDef shapeDef = new PolygonDef();
shapeDef.SetAsBox((800.0f / 2) / 30.0f, (16.0f / 2) / 30.0f);
shapeDef.Density = 0.0f;
// Create a texture from filename
Texture groundTexture = new Texture("ground.png");
// Create a sprite based on texture
// In order to draw it properly we need
// to set the origin
Sprite sprite = new Sprite(groundTexture) { Origin = new Vector2f(400, 8) };
// We then set the user data in the body
// so we can access it later on
body.SetUserData(sprite);
// Finalize the shape and body
body.CreateShape(shapeDef);
body.SetMassFromShapes();
}
// Create a box
public static void CreateBox(World world, int mouseX, int mouseY)
{
// We need to define a body with the position
BodyDef bodyDef = new BodyDef();
// Box2D doesn't use pixel measurement
// So we need to divide by 30
// 1m = 30px
bodyDef.Position.Set(mouseX / 30.0f, mouseY / 30.0f);
// Create the physics body
Body body = world.CreateBody(bodyDef);
// Define a new shape def
PolygonDef shapeDef = new PolygonDef();
shapeDef.SetAsBox((32.0f / 2) / 30.0f, (32.0f / 2) / 30.0f);
// Set the shape properties
shapeDef.Density = 1.0f;
shapeDef.Friction = 0.7f;
// Create a texture from filename
Texture boxTexture = new Texture("box.png");
// Create a sprite based on texture
// In order to draw it properly we need
// to set the origin
Sprite sprite = new Sprite(boxTexture) {Origin = new Vector2f(16, 16)};
// We then set the user data in the body
// so we can access it later on
body.SetUserData(sprite);
// Finalize the shape and body
body.CreateShape(shapeDef);
body.SetMassFromShapes();
}
} //End Program
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment