protected override void Init(World world) { Body body1 = new StaticBody("Ground1", new Box(500.0f, 20.0f)); body1.SetPosition(250.0f, 400); world.Add(body1); Body body2 = new Body("Teeter", new Box(250.0f, 10.0f), 5); body2.Friction = 0.5f; body2.SetPosition(250.0f, 370); world.Add(body2); Body body3 = new Body("light1", new Box(10.0f, 10.0f), 10); body3.SetPosition(135, 360); world.Add(body3); Body body4 = new Body("light2", new Box(10.0f, 10.0f), 10); body4.SetPosition(150, 360); world.Add(body4); Body body5 = new Body("weight", new Box(25.0f, 25.0f), 30); body5.SetPosition(350, 50); world.Add(body5); BasicJoint j = new BasicJoint(body1, body2, new Vector2f(250, 370)); world.Add(j); }
/// <summary> Create a new joint /// /// </summary> /// <param name="body1">The first body jointed /// </param> /// <param name="body2">The second body jointed /// </param> /// <param name="anchor">The anchor point for the underlying basic joint /// </param> /// <param name="Distance">The Distance the bodies must be apart before force is applied /// </param> public ConstrainingJoint(Body body1, Body body2, Vector2f anchor, float distance) { this.distance = distance; this.body1 = body1; this.body2 = body2; realJoint = new BasicJoint(body1, body2, anchor); }
protected override void Init(World world) { Body body1 = new StaticBody("Ground1", new Box(400.0f, 20.0f)); body1.SetPosition(250.0f, 400); world.Add(body1); Body body1b = new StaticBody("Ground1", new Box(20.0f, 400.0f)); body1b.SetPosition(20.0f, 200); world.Add(body1b); Body body3 = new Body("Mover2", new Circle(25), 50.0f); body3.SetPosition(225.0f, 365); world.Add(body3); Body body2 = new Body("Mover1", new Circle(25), 50.0f); body2.SetPosition(275.0f, 365); world.Add(body2); Body body3a = new Body("Mover2", new Circle(25), 50.0f); body3a.SetPosition(175.0f, 365); world.Add(body3a); Body body2a = new Body("Mover1", new Circle(25), 50.0f); body2a.SetPosition(325.0f, 365); world.Add(body2a); Body faller = new Body("Faller", new Circle(25), 200.0f); faller.SetPosition(250.0f, -20f); world.Add(faller); }
protected override void Init(World world) { float w = 50f; Body body1 = new StaticBody("Ground1", new Box(w,w)); body1.SetPosition(250.0f, 400); world.Add(body1); Body body3 = new StaticBody("Ground2", new Box(w,w)); body3.SetPosition(360.0f, 380); world.Add(body3); Body body5 = new StaticBody("Ground3", new Box(w,w)); body5.SetPosition(200.0f, 300); world.Add(body5); Body body6 = new StaticBody("Ground3", new Box(w,w)); body6.SetPosition(400.0f, 300); world.Add(body6); Body body2 = new Body("Mover1", new Box(w, w), 100.0f); body2.SetPosition(250.0f, 4.0f); world.Add(body2); Body body4 = new Body("Mover2", new Box(w,w), 100.0f); body4.SetPosition(230.0f, -60.0f); world.Add(body4); Body body8 = new Body("Mover3", new Box(w, w), 100.0f); body8.SetPosition(280.0f, -120.0f); world.Add(body8); }
protected override void Init(World world) { Body body1 = new StaticBody("Ground1", new Box(600.0f, 20.0f)); body1.SetPosition(250.0f, 400); world.Add(body1); Body body3 = new StaticBody("Ground2", new Box(200.0f, 20.0f)); body3.SetPosition(360.0f, 340); body3.Rotation = 0.4f; world.Add(body3); Body body9 = new StaticBody("Ground3", new Box(200.0f, 20.0f)); body9.SetPosition(140.0f, 340); body9.Rotation = - 0.4f; world.Add(body9); Body bodya = new StaticBody("Wall1", new Box(20.0f, 400.0f)); bodya.SetPosition(20.0f, 190); world.Add(bodya); Body bodyb = new StaticBody("Wall2", new Box(20.0f, 400.0f)); bodyb.SetPosition(480.0f, 190); world.Add(bodyb); Body body2 = new Body("Mover1", new Box(50.0f, 50.0f), 100.0f); body2.SetPosition(250.0f, 4.0f); body2.Rotation = 0.2f; world.Add(body2); Body body4 = new Body("Mover2", new Box(50.0f, 50.0f), 100.0f); body4.SetPosition(230.0f, - 60.0f); world.Add(body4); Body body8 = new Body("Mover3", new Box(50.0f, 50.0f), 100.0f); body8.SetPosition(280.0f, - 120.0f); world.Add(body8); }
protected override void Init(World world) { this.world = world; Body body1 = new StaticBody("Ground1", new Box(400.0f, 20.0f)); body1.SetPosition(250.0f, 400); body1.Friction = 1; world.Add(body1); for (int y = 0; y < 5; y++) { int xbase = 250 - (y * 21); for (int x = 0; x < y + 1; x++) { DynamicShape shape = new Box(40, 40); if ((x == 1) && (y == 2)) { shape = new Circle(19); } if ((x == 1) && (y == 4)) { shape = new Circle(19); } if ((x == 3) && (y == 4)) { shape = new Circle(19); } Body body2 = new Body("Mover1", shape, 100.0f); body2.SetPosition(xbase + (x * 42), y * 45); world.Add(body2); } } }
/// <summary> Create a new event describing a contact /// /// </summary> /// <param name="time">The time of the collision /// </param> /// <param name="body1">The first body in the collision /// </param> /// <param name="body2">The second body in the collision /// </param> /// <param name="point">The point of collision (not always perfect - accepts penetration) /// </param> /// <param name="normal">The normal of collision /// </param> /// <param name="depth">The penetration of of the contact /// </param> public CollisionEvent(float time, Body body1, Body body2, ROVector2f point, ROVector2f normal, float depth) { this.time = time; this.body1 = body1; this.body2 = body2; this.point = point; this.normal = normal; this.depth = depth; }
protected override void HandleKey(KeyHandler handler) { base.HandleKey(handler); if(handler.IsKeyPressed(Key.Space)) { Random r = new Random(); double rand = r.NextDouble(); Body body2 = new Body("Mover1", new Box(40.0f, 40.0f), 300.0f); body2.SetPosition(- 50, (float) (((rand * 50) + 150))); world.Add(body2); body2.AdjustAngularVelocity(1); body2.AdjustVelocity(new Vector2f(200, (float) (rand * 200))); } }
protected override void Init(World world) { Body body1 = new StaticBody("Ground1", new Box(400.0f, 20.0f)); body1.SetPosition(250.0f, 400); body1.Friction = 1; world.Add(body1); for (int y = 0; y < 3; y++) { int xbase = 250 - (y * 21); for (int x = 0; x < y + 1; x++) { Body body2 = new Body("Mover1", new Box(40.0f, 40.0f), 100.0f); body2.SetPosition(xbase + (x * 42), y * 45); world.Add(body2); } } }
/// <summary> Create a new arbiter - this should only be done by the /// engine /// /// </summary> /// <param name="b1">The first body in contact /// </param> /// <param name="b2">The second body in contact /// </param> internal Arbiter(Body b1, Body b2) { for (int i = 0; i < MAX_POINTS; i++) { contacts[i] = new Contact(); } if (!(b2 is StaticBody) && b1.GetHashCode() < b2.GetHashCode()) { body1 = b1; body2 = b2; } else { body1 = b2; body2 = b1; } }
public void DrawBody(Body body) { if (body.Shape is Box) { DrawBoxBody(body, (Box)body.Shape); } if (body.Shape is Circle) { DrawCircleBody(body, (Circle)body.Shape); } if (body.Shape is Silver.Weight.Raw.Shapes.Line) { DrawLineBody(body, (Silver.Weight.Raw.Shapes.Line)body.Shape); } if (body.Shape is Silver.Weight.Raw.Shapes.Polygon) { DrawPolygonBody(body, (Silver.Weight.Raw.Shapes.Polygon)body.Shape); } }
protected override void Init(World world) { Body body1 = new StaticBody("Ground1", new Box(400.0f, 20.0f)); body1.SetPosition(250.0f, 400); world.Add(body1); Body body3 = new StaticBody("Ground2", new Box(200.0f, 20.0f)); body3.SetPosition(250.0f, 100); world.Add(body3); Body swing = new Body("Swing", new Circle(10), 50); swing.SetPosition(160.0f, 300); world.Add(swing); Body swing2 = new Body("Swing", new Circle(10), 50); swing2.SetPosition(340.0f, 300); world.Add(swing2); Body swing3 = new Body("Swing", new Box(250.0f, 10.0f), 50); swing3.SetPosition(250.0f, 285); swing3.Friction = 4.0f; world.Add(swing3); Body box = new Body("Resting", new Box(30, 30), 1); box.SetPosition(250.0f, 200); box.Rotation = 0.15f; world.Add(box); BasicJoint j1 = new BasicJoint(body3, swing, new Vector2f(160, 110)); world.Add(j1); BasicJoint j2 = new BasicJoint(body3, swing2, new Vector2f(340, 110)); world.Add(j2); BasicJoint j3 = new BasicJoint(swing, swing3, new Vector2f(160, 300)); world.Add(j3); BasicJoint j4 = new BasicJoint(swing2, swing3, new Vector2f(340, 300)); world.Add(j4); swing.AdjustVelocity(new Vector2f(-100, 0)); }
/// <summary> Remove a body from the excluded list of this body. i.e. the /// body specified will be allowed to Collide with this body again /// /// </summary> /// <param name="other">The body to Remove from the exclusion list /// </param> public virtual void RemoveExcludedBody(Body other) { if (other.Equals(this)) { return ; } if (excluded.Contains(other)) { excluded.Remove(other); other.RemoveExcludedBody(this); } }
protected override void DrawCircleBody(Body body, Circle circle) { float x = body.GetPosition().X; float y = body.GetPosition().Y; float r = circle.Radius; float rot = body.Rotation; float xo = (float)(System.Math.Cos(rot) * r); float yo = (float)(System.Math.Sin(rot) * r); var circleSize = new Vector2f(circle.Radius * 2, circle.Radius * 2); UIElement e = body.UserData as UIElement; if (e == null) { e = new VisualCircle(circleSize, imagePath); canvas.Children.Add(e); body.UserData = e; } UpdateElementPosition(e, x, y, circleSize); ApplyRotationToElement(e, circle.Radius * 2, circle.Radius * 2, RadToDeg(body.Rotation)); }
/// <summary> Remove a body from the list /// /// </summary> /// <param name="body">The body to Remove from the list /// </param> public virtual void Remove(Body body) { //SupportClass.ICollectionSupport.Remove(elements, body); elements.Remove(body); }
/// <summary> Notify listeners of a collision /// /// </summary> /// <param name="body1">The first body in the collision /// </param> /// <param name="body2">The second body in the collision /// </param> /// <param name="point">The point of collision (not always perfect - accepts penetration) /// </param> /// <param name="normal">The normal of collision /// </param> /// <param name="depth">The penetration of of the contact /// </param> private void NotifyCollision(Body body1, Body body2, ROVector2f point, ROVector2f normal, float depth) { if (listeners.Count == 0) { return ; } CollisionEvent newEvent = new CollisionEvent(totalTime, body1, body2, point, normal, depth); for (int i = 0; i < listeners.Count; i++) { ((CollisionListener) listeners[i]).CollisionOccured(newEvent); } }
/// <summary> Create a joint holding two bodies together /// /// </summary> /// <param name="b1">The first body attached to the joint /// </param> /// <param name="b2">The second body attached to the joint /// </param> /// <param name="anchor">The anchor point which movement/rotation will occur /// arround. /// </param> public BasicJoint(Body b1, Body b2, Vector2f anchor) { id = NEXT_ID++; accumulatedImpulse.Reconfigure(0.0f, 0.0f); relaxation = 1.0f; Reconfigure(b1, b2, anchor); }
protected override void DrawBoxBody(Body body, Box box) { FrameworkElement e = body.UserData as FrameworkElement; if (e == null) { e = new VisualBox(box.Size, imagePath); canvas.Children.Add(e); body.UserData = e; } ROVector2f pos = body.GetPosition(); if(Single.IsNaN(pos.X) || Single.IsNaN(pos.Y)) return; UpdateElementPosition(e, Convert.ToInt32(pos.X), Convert.ToInt32(pos.Y), box.Size); ApplyRotationToElement(e,box.Size.X,box.Size.Y,RadToDeg(body.Rotation)); }
/// <summary> Create a joint holding two bodies together /// /// </summary> /// <param name="b1">The first body attached to the joint /// </param> /// <param name="b2">The second body attached to the joint /// </param> public FixedJoint(Body b1, Body b2) { id = NEXT_ID++; Reconfigure(b1, b2); }
/// <summary> Reconfigure this joint /// /// </summary> /// <param name="b1">The first body attached to this joint /// </param> /// <param name="b2">The second body attached to this joint /// </param> public virtual void Reconfigure(Body b1, Body b2) { body1 = b1; body2 = b2; joint1 = new BasicJoint(b1, b2, new Vector2f(b1.GetPosition())); joint2 = new BasicJoint(b2, b1, new Vector2f(b2.GetPosition())); }
/// <summary> Add a body to the list /// /// </summary> /// <param name="body">The body to Add /// </param> public virtual void Add(Body body) { elements.Add(body); }
private void DrawLineBody(Body body, Silver.Weight.Raw.Shapes.Line line) { System.Windows.Shapes.Line line1 = MakeLine(new Vector2f(line.X1, line.X2), new Vector2f(line.X2,line.Y2), 1, Colors.White); canvas.Children.Add(line1); }
/// <summary> Get the energy contained within 2 bodies /// /// </summary> /// <param name="body1">The first body /// </param> /// <param name="body2">The second body /// </param> /// <returns> The energy contained /// </returns> protected internal virtual float GetEnergy(Body body1, Body body2) { Vector2f combinedVel = MathUtil.Scale(body1.Velocity, body1.Mass); combinedVel.Add(MathUtil.Scale(body2.Velocity, body2.Mass)); float combinedInertia = body1.Inertia * body1.AngularVelocity; combinedInertia += body2.Inertia * body2.AngularVelocity; float vel1Energy = body1.Mass * body1.Velocity.Dot(body1.Velocity); float vel2Energy = body2.Mass * body2.Velocity.Dot(body2.Velocity); float ang1Energy = body1.Inertia * (body1.AngularVelocity * body1.AngularVelocity); float ang2Energy = body2.Inertia * (body2.AngularVelocity * body2.AngularVelocity); float energy = vel1Energy + vel2Energy + ang1Energy + ang2Energy; return energy; }
/// <summary> Check if this arbiter affects the specified body /// /// </summary> /// <param name="body">The body to check for /// </param> /// <returns> True if this arbiter effects the body /// </returns> public virtual bool Concerns(Body body) { return (body1 == body) || (body2 == body); }
/// <summary> Check if this list Contains the specified body /// /// </summary> /// <param name="body">The body to look for /// </param> /// <returns> True if this list Contains the specified body /// </returns> public virtual bool Contains(Body body) { return elements.Contains(body); }
/// <summary> Add a body that this body is not allowed to Collide with, i.e. /// the body specified will Collide with this body /// /// </summary> /// <param name="other">The body to exclude from collisions with this body /// </param> public virtual void AddExcludedBody(Body other) { if (other.Equals(this)) { return ; } if (!excluded.Contains(other)) { excluded.Add(other); other.AddExcludedBody(this); } }
/// <summary> Notification that this body Collided with another /// /// </summary> /// <param name="other">The other body that this body Collided with /// </param> public virtual void Collided(Body other) { if (!restingBodyDetection) { return ; } if (!touching.Contains(other)) { touching.Add(other); } if (Resting) { if ((!other.Resting)) { if (other.Velocity.LengthSquared() > hitTolerance) { hitByAnother = true; SetMass(originalMass); } } } hitCount++; }
/// <summary> Add a body to the simulation /// /// </summary> /// <param name="body">The body to be added /// </param> public virtual void Add(Body body) { bodies.Add(body); }
/// <summary> Remove a body from the simulation /// /// </summary> /// <param name="body">The body to be removed /// </param> public virtual void Remove(Body body) { bodies.Remove(body); }
/// <summary> Reconfigure this joint /// /// </summary> /// <param name="b1">The first body attached to this joint /// </param> /// <param name="b2">The second body attached to this joint /// </param> /// <param name="anchor">The static anchor point between the joints /// </param> public virtual void Reconfigure(Body b1, Body b2, Vector2f anchor) { body1 = b1; body2 = b2; Matrix2f rot1 = new Matrix2f(body1.Rotation); Matrix2f rot2 = new Matrix2f(body2.Rotation); Matrix2f rot1T = rot1.Transpose(); Matrix2f rot2T = rot2.Transpose(); Vector2f a1 = new Vector2f(anchor); a1.Sub(body1.GetPosition()); localAnchor1 = MathUtil.Mul(rot1T, a1); Vector2f a2 = new Vector2f(anchor); a2.Sub(body2.GetPosition()); localAnchor2 = MathUtil.Mul(rot2T, a2); accumulatedImpulse.Reconfigure(0.0f, 0.0f); relaxation = 1.0f; }