private void CreateWalls() { var wallCoff = new Coefficients(0.8f, 0.95f); var wallLife = new Lifespan(); var flrState = new PhysicsState(new ALVector2D((float)0.0, ((float)ActualWidth) * ((float)0.5), (float)ActualHeight + 100.0)); var flrShape = new PolygonShape(VertexHelper.CreateRectangle(ActualWidth, 200), 2); var bdyFloor = new Body(flrState, flrShape, float.PositiveInfinity, wallCoff, wallLife); var ceiState = new PhysicsState(new ALVector2D((float)0.0, ((float)ActualWidth) * ((float)0.5), -100.0)); var ceiShape = new PolygonShape(VertexHelper.CreateRectangle(ActualWidth, 200), 2); var bdyCeiling = new Body(ceiState, ceiShape, float.PositiveInfinity, wallCoff, wallLife); var lwlState = new PhysicsState(new ALVector2D((float)0.0, -100.0, ((float)ActualHeight) * ((float)0.5))); var lwlShape = new PolygonShape(VertexHelper.CreateRectangle(200, ActualHeight), 2); var bdyLeftWall = new Body(lwlState, lwlShape, float.PositiveInfinity, wallCoff, wallLife); var rwlState = new PhysicsState(new ALVector2D((float)0.0, (float)ActualWidth + 100.0, ((float)ActualHeight) * ((float)0.5))); var rwlShape = new PolygonShape(VertexHelper.CreateRectangle(200, ActualHeight), 2); var bdyRightWall = new Body(rwlState, rwlShape, float.PositiveInfinity, wallCoff, wallLife); engine.AddBody(bdyFloor); engine.AddBody(bdyCeiling); engine.AddBody(bdyLeftWall); engine.AddBody(bdyRightWall); }
void CreateBody(Window window) { lock (lockPhysicsLoop) { double angle = window.Angle; Rectangle position = window.Position; if (position.Width > 0 && position.Height > 0) { PhysicsState state = new PhysicsState(new ALVector2D(angle, position.Left + position.Width / 2, position.Top + position.Height / 2)); IShape shape = new PolygonShape(VertexHelper.CreateRectangle(position.Height, position.Width), 2); MassInfo mass = MassInfo.FromPolygon(shape.Vertexes, 1); Body body = new Body(state, shape, mass, new Coefficients(0, 1), new Lifespan()); body.LinearDamping = 0.95; body.AngularDamping = 0.95; body.IsCollidable = false; body.Tag = window; engine.AddBody(body); windowToBody.Add(window, body); try { dwm.SetWindowMatrix(window.HWnd.ToInt32(), 0f, 0f, 1f, 1f, 0f); } catch (Exception e) { HandleDwmCrash(e); } } } }
/// <summary> /// Called when the <see cref="P:System.Windows.Controls.ContentControl.Content"/> property changes. /// </summary> /// <param name="oldContent">The old value of the <see cref="P:System.Windows.Controls.ContentControl.Content"/> property.</param> /// <param name="newContent">The new value of the <see cref="P:System.Windows.Controls.ContentControl.Content"/> property.</param> protected override void OnContentChanged(object oldContent, object newContent) { timer.IsRunning = false; if (Body != null) { Body.Lifetime.IsExpired = true; } base.OnContentChanged(oldContent, newContent); PhysicsState state = new PhysicsState(new ALVector2D(0, 0, 0)); IShape shape = new PolygonShape(VertexHelper.CreateRectangle(5, 5), 1); MassInfo mass = MassInfo.FromPolygon(shape.Vertexes, 1); Body = new Body(state, shape, mass, new Coefficients(0, 1), new Lifespan()); Body.LinearDamping = LinearDumping; Body.Mass.MomentOfInertia = double.PositiveInfinity; Body.Tag = newContent; engine.AddBody(Body); if (!DesignerProperties.GetIsInDesignMode(this)) { timer.IsRunning = true; } }
public static Body AddRectangle(DemoOpenInfo info, Scalar height, Scalar width, Scalar mass, ALVector2D position) { Vector2D[] vertexes = VertexHelper.CreateRectangle(width, height); vertexes = VertexHelper.Subdivide(vertexes, Math.Min(height, width) / 5); IShape boxShape = ShapeFactory.CreateColoredPolygon(vertexes, Math.Min(height, width) / 5); Body body = new Body(new PhysicsState(position), boxShape, mass, Coefficients.Duplicate(), new Lifespan()); info.Scene.AddGraphic(CreateGraphic(body)); return(body); }
/// <summary> /// Creates new Rectange Body /// </summary> /// <param name="height">Height of the Body</param> /// <param name="width">Width of the Body</param> /// <param name="mass">Mass of the Body</param> /// <param name="position">Initial Direction and Linear Position of the Body</param> /// <returns>Return the new value of the BasePolygonBody</returns> /// <remarks>The Guid of new Body will be stored in Body.Tags["Guid"]. The raw Colored Drawable of new Body will be stored in Body.Tags["Drawable"].</remarks> public static Body CreateRectangle(Scalar height, Scalar width, Scalar mass, ALVector2D position) { var vertices = VertexHelper.CreateRectangle(width, height); vertices = VertexHelper.Subdivide(vertices, Math.Min(height, width) / 5); var boxShape = ShapeFactory.GetOrCreateColoredPolygonShape(vertices, Math.Min(height, width) / 5); var newBody = new Body(new PhysicsState(position), boxShape, mass, Coefficients.Duplicate(), new Lifespan()); return(newBody); }
public static Body AddFloor(DemoOpenInfo info, ALVector2D position) { Scalar height = 60; Scalar width = 2000; Vector2D[] vertexes = VertexHelper.CreateRectangle(width, height); IShape boxShape = ShapeFactory.CreateColoredPolygon(vertexes, Math.Min(height, width) / 5); Body body = new Body(new PhysicsState(position), boxShape, Scalar.PositiveInfinity, Coefficients.Duplicate(), new Lifespan()); body.IgnoresGravity = true; info.Scene.AddGraphic(CreateGraphic(body)); return(body); }
protected override void Open() { dispose += DemoHelper.BasicDemoSetup(DemoInfo); dispose += DemoHelper.CreateTank(DemoInfo, new Vector2D(50, 0)); Scene.Engine.AddLogic(new GravityField(new Vector2D(0, 1000), new Lifespan())); DemoHelper.AddFloor(DemoInfo, new ALVector2D(0, new Vector2D(700, 750))); IShape shape = ShapeFactory.CreateSprite(Cache <SurfacePolygons> .GetItem("block.png"), 3, 7, 4); DemoHelper.AddGrid( DemoInfo, shape, 20, new BoundingRectangle(440, 450, 500, 730), 1, 1); Body ball = DemoHelper.AddCircle(DemoInfo, 80, 20, 4000, new ALVector2D(0, 1028, 272)); Vector2D[][] polygons1 = new Vector2D[2][]; polygons1[0] = VertexHelper.Subdivide(VertexHelper.CreateRectangle(50, 50), 16); polygons1[1] = VertexHelper.CreateCircle(30, 20); Matrix2x3 matrix = Matrix2x3.FromTransformation(1, new Vector2D(30, 50)); polygons1[0] = VertexHelper.ApplyMatrix(ref matrix, polygons1[0]); IShape shape1 = ShapeFactory.CreateColoredMultiPolygon(polygons1, 3); DemoHelper.AddShape(DemoInfo, shape1, 50, new ALVector2D(0, 300, 300)); Vector2D[][] polygons = new Vector2D[3][]; polygons[0] = VertexHelper.Subdivide(VertexHelper.CreateRectangle(30, 50), 16); polygons[1] = VertexHelper.Subdivide(VertexHelper.CreateRectangle(50, 70), 16); polygons[2] = VertexHelper.CreateCircle(30, 20); matrix = Matrix2x3.FromTransformation(6, new Vector2D(36, 50)); polygons[2] = VertexHelper.ApplyMatrix(ref matrix, polygons[2]); matrix = Matrix2x3.FromTransformation(-6, new Vector2D(-36, 50)); polygons[1] = VertexHelper.ApplyMatrix(ref matrix, polygons[1]); IShape shape2 = ShapeFactory.CreateColoredMultiPolygon(polygons, 3); DemoHelper.AddShape(DemoInfo, shape2, 50, new ALVector2D(0, 400, 300)); }
void CreateBody(FrameworkElement frameworkElement) { double angle = 0; //if an element already has rotatetransform, get it's angle. RotateTransform rotateTransform = frameworkElement.RenderTransform as RotateTransform; if (rotateTransform != null) { angle = MathHelper.ToRadians(rotateTransform.Angle); } PhysicsState state = new PhysicsState(new ALVector2D(angle, GetLeft(frameworkElement) + (frameworkElement.ActualWidth / 2), GetTop(frameworkElement) + (frameworkElement.ActualHeight / 2))); IShape shape = new PolygonShape(VertexHelper.CreateRectangle(frameworkElement.ActualWidth, frameworkElement.ActualHeight), 2); MassInfo mass = MassInfo.FromPolygon(shape.Vertexes, 1); Body body = new Body(state, shape, mass, new Coefficients(0.4, 0.95), new Lifespan()); body.LinearDamping = LinearDamping; body.AngularDamping = AngularDamping; if (EnableWalls) { body.IsCollidable = true; body.CollisionIgnorer = ignorer; } else { body.IsCollidable = false; } body.Tag = frameworkElement; engine.AddBody(body); elementToBody.Add(frameworkElement, body); elementToScale.Add(frameworkElement, new ScaleState()); TransformGroup transform = new TransformGroup(); transform.Children.Add(new ScaleTransform()); transform.Children.Add(new RotateTransform()); frameworkElement.RenderTransform = transform; SetZTop(frameworkElement); SubscribeEventsToChild(frameworkElement); }
public static ConnectionSlotBody CreateConnectionSlotBody(IConnectionSlot slot, Guid modelId) { var size = slot.Size; var vertexList = VertexHelper.CreateRectangle(slot.Size, slot.Size).ToList(); vertexList.Insert(0, new Vector2D(size, 0)); var vertices = VertexHelper.Subdivide(vertexList.ToArray(), Math.Min(size, size) / 5); var boxShape = ShapeFactory.GetOrCreateColoredPolygonShape(vertices, Math.Min(size, size) / 5); var rectBody = new Body(new PhysicsState(ALVector2D.Zero), boxShape, 0.0001, Coefficients.Duplicate(), new Lifespan()); rectBody.Coefficients = new Physics2DDotNet.Coefficients(0.1, 0.7); var newSlot = new ConnectionSlotBody(rectBody.State, rectBody.Shape, rectBody.Mass, rectBody.Coefficients, rectBody.Lifetime, modelId) { Model = slot, IsCollidable = false }; return(newSlot); }
static void Main(string[] args) { GroupCollection collection1 = new GroupCollection(); GroupCollection collection2 = new GroupCollection(); /* collection1.AddRange(new int[] { 1 }); * collection1.AddRange(new int[] { 3 }); * collection1.AddRange(new int[] { 2 });*/ collection1.Add(1); collection1.Add(3); collection1.Add(2); collection2.Add(1); Console.WriteLine(GroupCollection.Intersect(collection1, collection2)); Console.ReadLine(); return; PhysicsEngine engine = new PhysicsEngine(); engine.BroadPhase = new Physics2DDotNet.Detectors.SelectiveSweepDetector(); engine.Solver = new Physics2DDotNet.Solvers.SequentialImpulsesSolver(); PhysicsTimer timer = new PhysicsTimer(engine.Update, .01f); timer.IsRunning = true; Coefficients coffecients = new Coefficients(/*restitution*/ 1, /*friction*/ .5f); IShape shape1 = new CircleShape(8, 7); IShape shape2 = new PolygonShape(VertexHelper.CreateRectangle(20, 10), 3); Scalar mass = 5; Body body1 = new Body(new PhysicsState(), shape1, mass, coffecients, new Lifespan()); Body body2 = new Body(new PhysicsState(), shape2, mass, coffecients, new Lifespan()); engine.AddBody(body1); engine.AddBody(body2); Joint joint = new HingeJoint(body1, body2, Vector2D.Zero, new Lifespan()); engine.AddJoint(joint); joint.Lifetime.IsExpired = true; engine.AddJoint(new HingeJoint(body1, body2, Vector2D.Zero, new Lifespan())); engine.Update(0, 0); body1.Lifetime.IsExpired = true; timer.IsRunning = false; engine.AddProxy(body1, body2, Matrix2x2.Identity); // b1.RemoveFromProxy(); BinaryFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); formatter.Serialize(stream, engine); stream.Seek(0, SeekOrigin.Begin); PhysicsEngine engine2 = (PhysicsEngine)formatter.Deserialize(stream); Console.WriteLine(); /* * * Vector2D[] vertexes1 = new Vector2D[] * { * new Vector2D(-1,1), * new Vector2D(-3,1), * new Vector2D(-3,-1), * new Vector2D(-1,-1), * }; * Vector2D[] vertexes2 = new Vector2D[] * { * new Vector2D(1,-1), * new Vector2D(3,-1), * new Vector2D(3,1), * new Vector2D(1,1), * }; * Vector2D[][] polygons = new Vector2D[2][]; * polygons[0] = vertexes1; * polygons[1] = vertexes2; * Console.WriteLine(MultiPartPolygon.GetCentroid(polygons)); * Console.WriteLine(MultiPartPolygon.GetArea(polygons)); */ Console.WriteLine("Finished"); Console.ReadLine(); }