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); }
public override void Init(PrototypingFramework.engine.GameEngine game) { base.Init(game); world = new PhysicsEngine(); world.BroadPhase = new Physics2DDotNet.Detectors.SelectiveSweepDetector(); world.Solver = new Physics2DDotNet.Solvers.SequentialImpulsesSolver(); PhysicsTimer timer = new PhysicsTimer(world.Update, .01f); timer.IsRunning = true; rs = new RectangleShape(new SFML.Window.Vector2f(10, 10)); rs.FillColor = Color.Black; rs.Position = new SFML.Window.Vector2f(10, 5); Coefficients coffecients = new Coefficients(/*restitution*/1, /*friction*/.5f); IShape shape2 = new PolygonShape(VertexHelper.CreateRectangle(10, 20), 3); body2 = new Body(new PhysicsState(new ALVector2D(0,10,5)), shape2, 5, coffecients, new Lifespan()); world.AddBody(body2); PhysicsLogic logGravity; logGravity = (PhysicsLogic)new GravityField(new Vector2D(0f, 200f), new Lifespan()); //pretty basic, create a downward force world.AddLogic(logGravity); Body bdyFloor; PhysicsState flrState; PolygonShape flrShape; Coefficients flrCoff; Lifespan flrLife; flrState = new PhysicsState(new ALVector2D((float)0.0, 0, (float)_game.Window.Size.Y-64)); //create the state, centering the x-axis on screen and bottom of the y-axis flrShape = new PolygonShape(VertexHelper.CreateRectangle(_game.Window.Size.X, 64), 2); //create form.widthX64 rectangle (sq) with grid spacing at 2 flrCoff = new Coefficients(0.5f, 0.4f, 0.4f); //might require tuning to your liking... flrLife = new Lifespan(); //forever and ever bdyFloor = new Body(flrState, flrShape, float.PositiveInfinity, flrCoff, flrLife); //never ending mass means it isn't going to move on impact bdyFloor.IgnoresGravity = true; //make sure the floor stays world.AddBody(bdyFloor); floor = new RectangleShape(new SFML.Window.Vector2f( _game.Window.Size.X,64)); floor.Position = new SFML.Window.Vector2f(0, _game.Window.Size.Y - 64); floor.FillColor = Color.Red; }
public static PolygonShape CreateColoredPolygon(Vector2D[] vertexes, Scalar gridSpacing) { PolygonShape shape = new PolygonShape(vertexes, gridSpacing); Vector2D[] reduced = VertexHelper.Reduce(vertexes); shape.Tag = DrawableFactory.CreatePolygon(reduced, CreateColor3Array(reduced.Length)); return shape; }
public static PolygonShape CreatePolygon(Vector2D[] vertexes, Scalar gridSpacing, ScalarColor4[] colors) { PolygonShape shape = new PolygonShape(vertexes, gridSpacing); shape.Tag = DrawableFactory.CreatePolygon(vertexes, colors); return shape; }
public static IShape CreateSprite(SurfacePolygons surfacePolygons, Surface bumpmap, bool xInverted, bool yInverted, int reduce, Scalar subdivide, Scalar gridSpacing, Light light) { Vector2D[][] polygons = surfacePolygons.Polygons; for (int index = 1; index < reduce; index++) { polygons = VertexHelper.ReduceRange(polygons, index); } polygons = VertexHelper.SubdivideRange(polygons, subdivide); Vector2D centroid = surfacePolygons.Offset; IShape shape; if (polygons.Length == 1) { shape = new PolygonShape(polygons[0], gridSpacing); } else { shape = new MultiPolygonShape(polygons, gridSpacing); } shape.Tag = DrawableFactory.CreateSprite(surfacePolygons.Surface, bumpmap,xInverted,yInverted, centroid,light); return shape; }
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); }
Body AddRectangle(Scalar height, Scalar width, Scalar mass, ALVector2D position) { Vector2D[] vertexes = VertexHelper.CreateRectangle(width, height); vertexes = VertexHelper.Subdivide(vertexes, (height + width) / 9); IShape boxShape = new PolygonShape(vertexes, Math.Min(height, width) / 2); Body e = new Body( new PhysicsState(position), boxShape, mass, coefficients.Duplicate(), new Lifespan()); AddGlObject(e); engine.AddBody(e); return e; }
void CreateClipper() { int width = 800; int height = 600; PolygonShape shape = new PolygonShape(VertexHelper.CreateRectangle(width, height), 100); clipper = new Body(new PhysicsState(), shape,1, new Coefficients(0, 0), new Lifespan()); clipper.State.Position.Linear.X = width / 2f; clipper.State.Position.Linear.Y = height / 2f; clipper.ApplyPosition(); clipper.IsBroadPhaseOnly = true; clipper.IsEventable = false; clipper.IgnoresGravity = true; clipper.IgnoresPhysicsLogics = true; clipper.Collided += new EventHandler<CollisionEventArgs>(clipper_Collided); clipper.Updated += new EventHandler<UpdatedEventArgs>(clipper_Updated); }
void DemoU() { BeginDemoChange(); Reset(false); AddGravityField(); Sprite backSprite = GetSprite("physicsplayGround.png"); Vector2D[][] polygons = backSprite.Polygons; //MultiPartPolygon shape = new MultiPartPolygon(vertexes, 4); //Body b = AddShape(shape, 40, new ALVector2D(0, backSprite.Offset)); foreach (Vector2D[] vertexes in polygons) { PolygonShape shape = new PolygonShape(vertexes, 10); Body b = AddShape(shape, Scalar.PositiveInfinity, new ALVector2D(0, backSprite.Offset)); b.IgnoresGravity = true; } for (int x = 440; x < 480; x += 10) { for (int y = -2000; y < 0; y += 12) { Body g = AddCircle(5, 7, 3, new ALVector2D(0, x + rand.Next(-400,400), y)); g.State.Velocity.Angular = 1; g.Updated += new EventHandler<UpdatedEventArgs>(DemoU_Body_Updated); // g.State.Velocity.Linear = new Vector2D(0, 500); } } for (int x = 490; x < 510; x += 10) { for (int y = -550; y < -500; y += 12) { Body g = AddRectangle(10, 20, 10, new ALVector2D(0, x + rand.Next(-400, 400), y)); g.State.Velocity.Angular = 1; g.Updated += new EventHandler<UpdatedEventArgs>(DemoU_Body_Updated); // g.State.Velocity.Linear = new Vector2D(0, 500); } } EndDemoChange(); }
void DemoE() { BeginDemoChange(); Reset(false); // Clear(); // AddClipper(); Vector2D gravityCenter = new Vector2D(500, 500); Scalar gravityPower = 200; engine.AddLogic(new GravityPointField(gravityCenter, gravityPower, new Lifespan())); AddRagDoll(gravityCenter + new Vector2D(0, -20)); Scalar length = 41; Scalar size = 8 ; bool reverse = false; for (Scalar distance = 250; distance < 650; length += 10, size *= 2, distance += 60 + length) { Scalar da = MathHelper.TwoPi / size;// ((MathHelper.TWO_PI * distance) / size); Scalar l2 = length / 2; // da /= 2; Vector2D[] vertexes = new Vector2D[] { Vector2D.FromLengthAndAngle(distance-l2,da/2), Vector2D.FromLengthAndAngle(distance-l2,-da/2), Vector2D.FromLengthAndAngle(distance+l2,-da/2), Vector2D.FromLengthAndAngle(distance+l2,da/2), }; //da *= 2; Vector2D[] vertexes2 = VertexHelper.CenterVertexes(vertexes); vertexes = VertexHelper.Subdivide(vertexes2, 5); PolygonShape shape = new PolygonShape(vertexes, 1.5f); for (Scalar angle = 0; angle < MathHelper.TwoPi; angle += da) { Vector2D position = Vector2D.FromLengthAndAngle(distance, angle) + gravityCenter; Body body = AddShape(shape, (size * length) / 10, new ALVector2D(angle, position)); body.State.Velocity.Linear = GetOrbitVelocity(gravityCenter, Vector2D.FromLengthAndAngle(distance - length, angle) + gravityCenter, gravityPower); body.State.Velocity.Linear *= .5f; body.State.Velocity.Angular = -(body.State.Velocity.Linear.Magnitude) / (distance);// *(1 / MathHelper.TWO_PI); if (reverse) { body.State.Velocity.Linear = -body.State.Velocity.Linear; body.State.Velocity.Angular = -body.State.Velocity.Angular; } } reverse = !reverse; } EndDemoChange(); }
List<Body> AddRagDoll(Vector2D location) { List<Body> result = new List<Body>(); Scalar mass = 10; Body head = AddCircle(12, 9, mass, new ALVector2D(0, location + new Vector2D(0, 0))); Scalar Ld2 = 50 / 2; Scalar Wd2 = 25 / 2; Vector2D[] vertexes = new Vector2D[] { new Vector2D(Wd2, 0), new Vector2D(Wd2, Ld2), new Vector2D(5, Ld2+7), new Vector2D(-5, Ld2+7), new Vector2D(-Wd2, Ld2), new Vector2D(-Wd2, 0), new Vector2D(-(Wd2+4), -Ld2/2+6), new Vector2D(-Wd2+2, -Ld2), new Vector2D(0, -Ld2), new Vector2D(Wd2-2, -Ld2), new Vector2D(Wd2+4, -Ld2/2+6), }; IShape shape = new PolygonShape(vertexes, 5); Body torso = AddShape(shape, mass * 4, new ALVector2D(0, location + new Vector2D(0, 40))); Body ltarm = AddRectangle(10, 30, mass, new ALVector2D(0, location + new Vector2D(-30, 20))); Body lbarm = AddRectangle(10, 30, mass, new ALVector2D(0, location + new Vector2D(-65, 20))); Body rtarm = AddRectangle(10, 30, mass, new ALVector2D(0, location + new Vector2D(30, 20))); Body rbarm = AddRectangle(10, 30, mass, new ALVector2D(0, location + new Vector2D(65, 20))); Body ltleg = AddRectangle(40, 15, mass * 2, new ALVector2D(.06f, location + new Vector2D(-10, 95))); Body lbleg = AddRectangle(40, 15, mass * 2, new ALVector2D(0, location + new Vector2D(-11, 140))); Body rtleg = AddRectangle(40, 15, mass * 1.5f, new ALVector2D(-.06f, location + new Vector2D(10, 95))); Body rbleg = AddRectangle(40, 15, mass * 1.5f, new ALVector2D(0, location + new Vector2D(11, 140))); result.Add(head); result.Add(torso); result.Add(ltarm); result.Add(lbarm); result.Add(rtarm); result.Add(rbarm); result.Add(ltleg); result.Add(lbleg); result.Add(rtleg); result.Add(rbleg); HingeJoint neck = new HingeJoint(head, torso, location + new Vector2D(0, 15), new Lifespan()); HingeJoint lshoulder = new HingeJoint(ltarm, torso, location + new Vector2D(-18, 20), new Lifespan()); HingeJoint lelbow = new HingeJoint(ltarm, lbarm, location + new Vector2D(-47, 20), new Lifespan()); HingeJoint rshoulder = new HingeJoint(rtarm, torso, location + new Vector2D(18, 20), new Lifespan()); HingeJoint relbow = new HingeJoint(rtarm, rbarm, location + new Vector2D(47, 20), new Lifespan()); HingeJoint lhip = new HingeJoint(ltleg, torso, location + new Vector2D(-8, 72), new Lifespan()); HingeJoint lknee = new HingeJoint(ltleg, lbleg, location + new Vector2D(-11, 115), new Lifespan()); HingeJoint rhip = new HingeJoint(rtleg, torso, location + new Vector2D(8, 72), new Lifespan()); HingeJoint rknee = new HingeJoint(rtleg, rbleg, location + new Vector2D(11, 115), new Lifespan()); List<Joint> joints = new List<Joint>(); joints.Add(neck); joints.Add(lelbow); joints.Add(rshoulder); joints.Add(relbow); joints.Add(lshoulder); joints.Add(lhip); joints.Add(lknee); joints.Add(rhip); joints.Add(rknee); foreach (HingeJoint joint in joints) { joint.DistanceTolerance = 10; } engine.AddJointRange<Joint>(joints); return result; }
private void RenderPolygon( PolygonShape pps, PointF point, double angle) { /* HACK: This is a hack. I couldn't figure out the // calculations at this point, so I just dove into the // physics system to figure out the points. */ // Try to figure out the real shape based on the // physics object for (int i = 0; i < pps.Vertexes.Length; i++) { // Figure out what two vertexes we care about Vector2D v1, v2; int i1 = i; int i2 = i + 1; if (i2 >= pps.Vertexes.Length) i2 = 0; v1 = pps.Vertexes[i1]; v2 = pps.Vertexes[i2]; PointF p1 = ToPoint(v1.X + point.X, v1.Y + point.Y); PointF p2 = ToPoint(v2.X + point.X, v2.Y + point.Y); Color color = Color.Gray; if (State.Score.Countdown == 0) color = Color.Red; Paint.Line(p1.X, p1.Y, p2.X, p2.Y, color); } }
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; }
/// <summary> /// Recursively process the polygon to attempt to find a /// single polygon element that could be added to the physics /// layer without any holes or additional physics. /// </summary> private void CreateJunctionPhysics( int depth, IPoly poly, RectangleF bounds) { // Ignore empty polygons if (poly.InnerPolygonCount == 0) return; // See if we are a solid polygon double areaDifference = bounds.Width * bounds.Height - poly.Area; if (poly.InnerPolygonCount == 1) { if (poly.IsHole()) // Don't add holes return; if (poly.PointCount == 4 && areaDifference <= 0.1f) // We appear to be at least mostly solid, drop it return; } // If we have more than one polygon, split it if (poly.InnerPolygonCount > 1 || bounds.Width > Constants.MaximumJunctionPhysicsBlock || bounds.Height > Constants.MaximumJunctionPhysicsBlock) { // We split the polygon into quads and process each // one to add it recursively. CreateJunctionPhysics(depth + 1, poly, bounds, 0, 0); CreateJunctionPhysics(depth + 1, poly, bounds, 0, 1); CreateJunctionPhysics(depth + 1, poly, bounds, 1, 1); CreateJunctionPhysics(depth + 1, poly, bounds, 1, 0); return; } // We should never get a hole if (poly.IsHole()) { // We shouldn't get this Log.Error("Got a top-level polygon hole"); return; } // Create a polygon shape as vectors LinkedList<Vector2D> vectors = new LinkedList<Vector2D>(); for (int i = 0; i < poly.PointCount; i++) { // Get the coordinates float x = (float) poly.GetX(i); float y = (float) poly.GetY(i); // Create the vector vectors.Add(new Vector2D(x, y)); } // Convert it into a physics2d polygon shape. Making the // PolygonShape second parameter too small makes the game // basically unusable in terms of stage generation but // more accurate for impacts with the side. Vector2D [] array = vectors.ToArray(); IShape ps = new PolygonShape(array, 5f); physicsShapes.Add(ps); }