public void SetPhysicsCanvas(PhysicsCanvas cnvGame) { _physicsCanvas = cnvGame; }
/// <summary> /// Gets a list of all joint-related sprites, given a sprite. /// </summary> public static void GetAllSpritesRelatedByJoints(PhysicsCanvas cnvGame, PhysicsSprite spr, List<PhysicsSprite> sprJoined) { // add in the given sprite if (sprJoined.IndexOf(spr) < 0) sprJoined.Add(spr); foreach (Joint joint in cnvGame.Simulator.JointList) { if (joint.BodyA == spr.BodyObject || joint.BodyB == spr.BodyObject) { var spritesJoined = from s in cnvGame.PhysicsObjects.Values where s.BodyObject == joint.BodyA || s.BodyObject == joint.BodyB && (joint.BodyA.Enabled == true && joint.BodyB.Enabled == true) select s; foreach (PhysicsSprite sprFound in spritesJoined) { if (sprJoined.IndexOf(sprFound) < 0) { GetAllSpritesRelatedByJoints(cnvGame, sprFound, sprJoined); } } } } }
void PhysicsSprite_Loaded(object sender, RoutedEventArgs e) { // set up the transform for position and rotation if (this.RenderTransform != null && this.RenderTransform is SysWinMedia.TransformGroup) { foreach (SysWinMedia.Transform trn in (this.RenderTransform as SysWinMedia.TransformGroup).Children) { if (trn is SysWinMedia.CompositeTransform) Transform = trn as SysWinMedia.CompositeTransform; } } else if (this.RenderTransform is SysWinMedia.CompositeTransform) { Transform = this.RenderTransform as SysWinMedia.CompositeTransform; } else { this.RenderTransform = new SysWinMedia.CompositeTransform(); Transform = this.RenderTransform as SysWinMedia.CompositeTransform; } if (_physicsCanvas == null) { _physicsCanvas = PhysicsUtilities.FindPhysicsCanvas(this); if (_physicsCanvas == null) { System.Diagnostics.Debug.WriteLine("PhysicsCanvas was not found - sprite will not be loaded"); return; } } if (InitializePhysics) { List<FarseerPhysics.Collision.Shapes.Shape> shapes; // DEMO: (1) Determine the Shape (Outline) of the element on the screen try { shapes = PhysicsCanvas.BoundaryHelper.CreateShapesFromElement(_physicsCanvas.Simulator, this, _shapeType); } catch (Exception ex) { shapes = PhysicsCanvas.BoundaryHelper.CreateShapesFromElement(_physicsCanvas.Simulator, this, ShapeTypes.Rectangle); } // DEMO: (3) Create the Body based on shape (outline) BodyObject = BodyFactory.CreateBody(_physicsCanvas.Simulator); BodyObject.BodyType = BodyType.Dynamic; foreach (FarseerPhysics.Collision.Shapes.Shape shape in shapes) { BodyObject.CreateFixture(shape); } foreach (Fixture f in BodyObject.FixtureList) { if (_collisionEventDisable) { System.Diagnostics.Debug.WriteLine(this.GetValue(Canvas.NameProperty) + " WILL NOT fire collision events."); CollisionEventDisable = _collisionEventDisable; } } BodyObject.Awake = true; // did this item have a rotation if so then we need to add it back into new sprite? double angle = 0; if (this.RenderTransform is SysWinMedia.CompositeTransform) { angle = (this.RenderTransform as SysWinMedia.CompositeTransform).Rotation; (this.RenderTransform as SysWinMedia.CompositeTransform).Rotation = 0; this.RenderTransformOrigin = new sw.Point(0, 0); } if (angle != 0) { float radAngle = (float)(Math.PI * angle / 180.0); BodyObject.Rotation = radAngle; _rotation = radAngle; } // refresh static now that body is created IsStatic = _isStatic; FrictionCoefficient = _frictionCoefficient; CollisionGroup = _collisionGroup; CausesCollisions = _causesCollisions; DebugMode = _debugMode; RestitutionCoefficient = _restitutionCoefficient; CollisionEventDisable = _collisionEventDisable; InternalName = this.Name; DebugMode = _physicsCanvas.DebugMode; IsBullet = _isBullet; IsEnabled = _isEnabled; IsBodyEnabled = _isBodyEnabled; Rotation = PhysicsUtilities.RadiansToDecimalDegrees(_rotation); AngularDamping = _angularDamping; LinearDamping = _linearDamping; if (_mass == -1) _mass = BodyObject.Mass; else Mass = _mass; IsSensor = _isSensor; // get the overall dimensions double width, height; width = this.ActualWidth; height = this.ActualHeight; // adjust the offset position in the world xna.Vector2 offsetPosition = PhysicsUtilities.GetOffsetPositionInScreen(this); // adjust position to take into consideration origin at middle offsetPosition.X = (offsetPosition.X + ((float)getWidth / 2)); offsetPosition.Y = (offsetPosition.Y + ((float)getHeight / 2)); if (_position != Vector2.Zero) Position = _position; else BodyObject.Position = PhysicsCanvas.BoundaryHelper.ScreenToWorld(offsetPosition); Update(); } // add this sprite into the simulation if (_physicsCanvas != null) { _physicsCanvas.AddPhysicsSprite(this, InitializePhysics); } this.Loaded -= PhysicsSprite_Loaded; IsLoaded = true; //System.Diagnostics.Debug.WriteLine("added sprite {0} in {1} ms. with mass={2}", this.Name, (DateTime.Now - _dtCtor).TotalMilliseconds, _mass); }
public static List<Joint> FindJoint(PhysicsCanvas cnvGame, string body1, string body2) { List<Joint> joint = new List<Joint>(); foreach (Joint x in cnvGame.Simulator.JointList) { if ((x.BodyA == cnvGame.PhysicsObjects[body1].BodyObject && x.BodyB == cnvGame.PhysicsObjects[body2].BodyObject) || (x.BodyB == cnvGame.PhysicsObjects[body1].BodyObject && x.BodyA == cnvGame.PhysicsObjects[body2].BodyObject)) { joint.Add(x); } } return joint; }