#### /PhysicsEngines/Tests2D/Tutorials.cs

#
C# | 400 lines | 242 code | 45 blank | 113 comment | 8 complexity | 17e8700a78133f33ce4700f711ddc3c7 MD5 | raw file
``````
using System.Collections.Generic;

using Delta.Engine;

using Delta.InputSystem;

using Delta.Rendering.Basics.Drawing;

using Delta.Rendering.Basics.Fonts;

using Delta.Utilities;

using Delta.Utilities.Datatypes;

using NUnit.Framework;

namespace Delta.PhysicsEngines.Tests2D

{

/// <summary>

/// Tutorials for Physics 2D

/// </summary>

public class Tutorials

{

#region Simple2DSimulation (Static)

/// <summary>

/// Tutorial: 2D Physics Tutorial 1: Simple 2D Simulation

/// Difficulty: Easy

/// Description: Simple physics simulation with a ground, box and circle.

/// Image: http://DeltaEngine.net/Icons/Physics2DTutorial1.png

/// </summary>

[Test, Category("Visual")]

public static void Simple2DSimulation()

{

Physics.Gravity = new Vector(0f, 0.0981f, 0f);

// Enable debug drawing

Physics.DebugEnabled = true;

float mass = 1.0f;

ball.Position = new Vector(0.45f, 0.1f, 0f);

ball.Friction = 1.0f;

ball.DebugColor = Color.Red;

Rectangle boxRect = new Rectangle(0f, 0f, 0.05f, 0.05f);

PhysicsBody box = Physics.CreateRectangle(boxRect.Width,

boxRect.Height, 1.0f);

box.Position = new Vector(0.5f, 0.4f, 0.0f);

box.Friction = 1.0f;

box.DebugColor = Color.Green;

// set our ground

Rectangle groundRect = new Rectangle(0.1f, 0.65f, 0.8f, 0.1f);

PhysicsBody ground = Physics.CreateRectangle(groundRect.Width,

groundRect.Height, 1f);

ground.Position = new Vector(groundRect.Center, 0.0f);

ground.IsStatic = true;

ground.Friction = 1.0f;

ground.DebugColor = Color.Yellow;

// And start the application.

Application.Start();

}

#endregion

#region JointsSimulation (Static)

/// <summary>

/// Tutorial: 2D Physics Tutorial 2: Joints Simulation

/// Difficulty: Easy

/// Description: Simple 2d physics simulation with joints.

/// Image: http://DeltaEngine.net/Icons/Physics2DTutorial2.png

/// </summary>

[Test, Category("Visual")]

public static void JointsSimulation()

{

Physics.Gravity = new Vector(0f, 0.0981f, 0f);

// Enable debug drawing

Physics.DebugEnabled = true;

PhysicsBody box = Physics.CreateRectangle(0.05f, 0.05f, 1f);

box.Position = new Vector(0.3f, 0.35f, 0f);

box.Friction = 1f;

box.DebugColor = Color.Green;

PhysicsBody box2 = Physics.CreateRectangle(0.05f, 0.05f, 1f);

box2.Position = new Vector(0.4f, 0.3f, 0f);

box2.Friction = 1f;

box2.DebugColor = Color.Yellow;

// Create angle joint

PhysicsJoint joint = Physics.CreatePointPointDistance(

box, box2,

new Vector(6.0f, 0.0f, 0.0f),

new Vector(0.0f, -1.0f, 0.0f));

// set our ground

Rectangle groundRect = new Rectangle(0.2f, 0.65f, 0.7f, 0.1f);

PhysicsBody ground = Physics.CreateRectangle(groundRect.Width,

groundRect.Height, 1f);

ground.Position = new Vector(groundRect.Center, 0f);

ground.IsStatic = true;

ground.Friction = 1f;

ground.DebugColor = Color.Yellow;

Application.Start(delegate

{

Font.Default.Draw("You can control a box with W/A/S/D",

ScreenSpace.DrawArea);

// Up movement

if (Input.Keyboard.IsPressed(InputButton.W))

{

box.ApplyLinearImpulse(new Vector(0, -2, 0));

}

// Down movement

if (Input.Keyboard.IsPressed(InputButton.S))

{

box.ApplyLinearImpulse(new Vector(0, 2, 0));

}

// Left movement

if (Input.Keyboard.IsPressed(InputButton.A))

{

box.ApplyLinearImpulse(new Vector(-2, 0, 0));

}

// Right movement

if (Input.Keyboard.IsPressed(InputButton.D))

{

box.ApplyLinearImpulse(new Vector(2, 0, 0));

}

});

}

#endregion

#region BouncyBallSimulation (Static)

/// <summary>

/// Tutorial: 2D Physics Tutorial 3: Bouncy Ball Simulation

/// Difficulty: Easy

/// Description: Simple 2d physics simulation of a bouncy ball.

/// Image: http://DeltaEngine.net/Icons/Physics2DTutorial3.png

/// </summary>

[Test, Category("Visual")]

public static void BouncyBallSimulation()

{

Physics.Gravity = new Vector(0f, 0.0981f, 0f);

// Enable debug drawing

Physics.DebugEnabled = true;

// Border rectangle

Rectangle leftRect = new Rectangle(0.0f, 0.0f, 0.1f, 1.0f);

Rectangle topRect = new Rectangle(0.1f, 0.1f, 0.8f, 0.1f);

Rectangle rightRect = new Rectangle(0.9f, 0.0f, 0.1f, 1.0f);

Rectangle bottomRect = new Rectangle(0.05f, 0.8f, 0.85f, 0.2f);

PhysicsBody leftBorder = Physics.CreateRectangle(leftRect.Width,

leftRect.Height, 5f);

leftBorder.Friction = 1f;

leftBorder.Restitution = 1f;

leftBorder.Position2D = leftRect.Center;

leftBorder.IsStatic = true;

leftBorder.DebugColor = Color.White;

PhysicsBody topBorder = Physics.CreateRectangle(topRect.Width,

topRect.Height, 5f);

topBorder.Friction = 1f;

topBorder.Restitution = 1f;

topBorder.Position2D = topRect.Center;

topBorder.IsStatic = true;

topBorder.DebugColor = Color.White;

PhysicsBody rightBorder = Physics.CreateRectangle(rightRect.Width,

rightRect.Height, 5f);

rightBorder.Friction = 1f;

rightBorder.Restitution = 1f;

rightBorder.Position2D = rightRect.Center;

rightBorder.IsStatic = true;

rightBorder.DebugColor = Color.White;

PhysicsBody bottomBorder = Physics.CreateRectangle(bottomRect.Width,

bottomRect.Height, 5f);

bottomBorder.Friction = 1f;

bottomBorder.Restitution = 1f;

bottomBorder.Position2D = bottomRect.Center;

bottomBorder.IsStatic = true;

bottomBorder.Rotation = 10f;

// Create a ball;

PhysicsBody ball = Physics.CreateCircle(0.035f, 1f);

ball.Friction = 0.3f;

ball.Restitution = 1f;

ball.Position2D = new Point(0.2f, 0.5f);

ball.DebugColor = Color.CornflowerBlue;

Application.Start();

}

#endregion

#region RayCasting2D (Static)

/// <summary>

/// Tutorial: 2D Physics Tutorial 4: Ray Casting in 2D

/// Difficulty: Easy

/// Description: Simple 2d ray cast with a box.

/// Image: http://DeltaEngine.net/Icons/Physics2DTutorial4.png

/// </summary>

[Test, Category("Visual")]

public static void RayCasting2D()

{

Physics.Gravity = new Vector(0f, 0.0981f, 0f);

// Enable debug drawing

Physics.DebugEnabled = true;

// set our ground

Rectangle groundRect = new Rectangle(0.6f, 0.4f, 0.2f, 0.2f);

PhysicsBody ground = Physics.CreateRectangle(groundRect.Width,

groundRect.Height, 1f);

ground.Position = new Vector(groundRect.Center, 0.0f);

ground.IsStatic = true;

ground.Friction = 1.0f;

ground.DebugColor = Color.Yellow;

Point rayStart = new Point(0f, 0.5f);

Point rayEnd = ScreenSpace.DrawArea.TopRight;

Application.Start(delegate

{

// Draw the ray.

Line.Draw(rayStart, rayEnd, Color.Green);

// And perform the ray cast.

PhysicsBody foundObject;

Point normal;

float fraction;

if (Physics.FindRayCast2D(rayStart, rayEnd, out foundObject,

out normal, out fraction))

{

ground.Rotation += 10f * Time.Delta;

}

// Update the ray movement.

rayEnd.Y += 0.1f * Time.Delta;

if (rayEnd.Y >= ScreenSpace.DrawArea.Bottom)

{

rayEnd.Y = ScreenSpace.DrawArea.Top;

}

});

}

#endregion

#region CollisionEvents (Static)

/// <summary>

/// Tutorial: 2D Physics Tutorial 5: Collision Events

/// Difficulty: Easy

/// Description: Simple tutorial showing how to use physic collision events.

/// Image: http://DeltaEngine.net/Icons/Physics2DTutorial5.png

/// </summary>

[Test, Category("Visual")]

public static void CollisionEvents()

{

Physics.Gravity = new Vector(0.0f, 0.0981f, 0.0f);

// Enable debug drawing

Physics.DebugEnabled = true;

PhysicsBody ball = Physics.CreateCircle(0.05f, 1f);

ball.Position = new Vector(0.2f, 0.3f, 0f);

ball.Friction = 0.3f;

ball.Restitution = 1f;

ball.DebugColor = Color.Red;

ball.Name = "Ball";

PhysicsBody box = Physics.CreateRectangle(0.05f, 0.05f, 1f);

box.Position = new Vector(0.5f, 0.4f, 0f);

box.Friction = 1f;

box.DebugColor = Color.Green;

box.Name = "Box";

Rectangle groundRect = new Rectangle(0.1f, 0.65f, 0.8f, 0.1f);

// set our ground

PhysicsBody ground = Physics.CreateRectangle(groundRect.Width,

groundRect.Height, 1f);

ground.Position = new Vector(groundRect.Center, 0f);

ground.IsStatic = true;

ground.Friction = 1f;

ground.DebugColor = Color.Yellow;

ground.Name = "Plane";

ground.CollisionBegin += delegate(PhysicsBody other)

{

Log.Info("Body '" + other.Name + "' collides with the plane.", false);

};

ground.CollisionEnd += delegate(PhysicsBody other)

{

Log.Info("Body '" + other.Name + "' ends collision with the plane.",

false);

};

Application.Start();

}

#endregion

#region PyramidSimulation (Static)

/// <summary>

/// Tutorial: 2D Physics Tutorial 6: Pyramid Simulation

/// Difficulty: Normal

/// Description: Simple 2d physics simulation with pyramid

/// Image: http://DeltaEngine.net/Icons/Physics2DTutorial6.png

/// </summary>

[Test, Category("Visual")]

public static void PyramidSimulation()

{

Physics.Gravity = new Vector(0f, 0.0981f, 0f);

// Enable debug drawing

Physics.DebugEnabled = true;

Rectangle groundRect = new Rectangle(0.2f, 0.65f, 0.7f, 0.1f);

const int PyramidBaseBodyCount = 10;

List<PhysicsBody> bodies = new List<PhysicsBody>();

Point rowStart = new Point(0.75f, 0.7f);

rowStart.Y -= 0.05f + PyramidBaseBodyCount * 0.11f;

Point deltaRow = new Point(-0.0625f, 0.11f);

const float spacing = 0.075f;

for (int i = 0; i < PyramidBaseBodyCount; ++i)

{

Point pos = rowStart;

for (int j = 0; j < i + 1; ++j)

{

PhysicsBody rect = Physics.CreateRectangle(0.05f, 0.05f, 1.0f);

rect.Position2D = pos;

//rect.Friction = 1.0f;

rect.DebugColor = Color.Red;

pos.X += spacing;

}

rowStart += deltaRow;

}

// set our ground

PhysicsBody ground = Physics.CreateRectangle(groundRect.Width,

groundRect.Height, 1f);

ground.Position = new Vector(groundRect.Center, 0f);

ground.IsStatic = true;

ground.Friction = 1f;

ground.DebugColor = Color.Yellow;

Application.Start();

}

#endregion

/*TODO: integrate: tst physics projectile firing (farseer high fps bug)

class Game : DynamicModule

{

PhysicsBody projectile = null;

private PhysicsBody ground = null;

public Game()

: base("Simple Game", typeof(Application))

{

Settings.Extra.LimitFramerateNumber = 60;

Settings.Modules.PhysicsModule = "Farseer";

Physics.Gravity = new Vector(0f, 1f, 0f);

Physics.DebugEnabled = true;

Rectangle groundRect = new Rectangle(0f, 0.74f, 1f, 0.01f);

ground = Physics.CreateRectangle(groundRect.Width, groundRect.Height, 1f);

ground.Position = new Vector(groundRect.Center, 0.0f);

ground.IsStatic = true;

ground.Friction = 1.0f;

ground.DebugColor = Color.Yellow;

{

// Kill the old projectile

if (projectile != null)

{

projectile.Remove();

}

projectile = Physics.CreateRectangle(0.06f, 0.01f, 1f);

projectile.Position2D = new Point(0.205f, 0.66f);

projectile.Mass = 1.0f;

projectile.Friction = 1f;

projectile.DebugColor = Color.Red;

projectile.ApplyLinearImpulse(new Vector(new Point(665f, -746f), 0f));

Log.Info("Peng " + Time.Milliseconds);

Log.Info("LinearVelocity=" + projectile.LinearVelocity, false);

Log.Info("AngularVelocity=" + projectile.AngularVelocity, false);

Log.Info("Restitution=" + projectile.Restitution, false);

});

}

public override void Run()

{

if (projectile != null)

{

Log.Info("GetDebugInfo=" + projectile.GetDebugInfo(), false);

}

}

}

*/

}

}

``````