public void AddJoint(PhysicsJointMain joint) { string bodyOne = joint.BodyOne; string bodyTwo = joint.BodyTwo; int collisionGroup = joint.CollisionGroup; bool isAngleSpringEnabled = joint.AngleSpringEnabled; float springConstant = joint.AngleSpringConstant; float dampeningConstant = joint.AngleSpringDampningConstant; float angleLowerLimit = (float)joint.AngleLowerLimit; float angleUpperLimit = (float)joint.AngleUpperLimit; Point center = joint.GetCenter(); Vector2 ptCollisionCenter = new Vector2((float)center.X, (float)center.Y); if (!PhysicsObjects.ContainsKey(bodyOne)) { throw new Exception("Cannot add joint for an invalid BodyOne value of '" + bodyOne + "'. If using Behaviors, did you forgot to add a PhysicsObjectBehavior?"); } if (!PhysicsObjects.ContainsKey(bodyTwo)) { throw new Exception("Cannot add joint for an invalid BodyTwo value of '" + bodyTwo + "'. If using Behaviors, did you forgot to add a PhysicsObjectBehavior?"); } Body body1 = PhysicsObjects[bodyOne].BodyObject; Body body2 = PhysicsObjects[bodyTwo].BodyObject; Geom geom1 = PhysicsObjects[bodyOne].GeometryObject; Geom geom2 = PhysicsObjects[bodyTwo].GeometryObject; RevoluteJoint revoluteJoint = JointFactory.Instance.CreateRevoluteJoint(Simulator, body1, body2, ptCollisionCenter); if (isAngleSpringEnabled) { AngleSpring angleSpring = new AngleSpring(body1, body2, springConstant, dampeningConstant); Simulator.Add(angleSpring); } if (angleUpperLimit != -1 && angleLowerLimit != -1) { float upperAngle = (float)DegreesToRadians(angleUpperLimit); float lowerAngle = (float)DegreesToRadians(angleLowerLimit); AngleLimitJoint angleLimitJoint = new AngleLimitJoint(body1, body2, lowerAngle, upperAngle); Simulator.Add(angleLimitJoint); } if (collisionGroup > 0) { geom1.CollisionGroup = collisionGroup; geom2.CollisionGroup = collisionGroup; } // get rid of the UI representation of the joint joint.VisualElement.Visibility = Visibility.Collapsed; }
/// <summary> /// Adds a single Static holder object from a canvas into the simulation. /// </summary> /// <param name="cnvContainer">The static holder to add</param> public void AddStaticHolder(string body) { if (!PhysicsObjects.ContainsKey(body)) { throw new Exception("A PhysicsStaticHolder exists with an invalid Body value of '" + body + "'."); } Body body1 = PhysicsObjects[body].BodyObject; body1.IsStatic = true; }
/// <summary> /// Adds a single Static holder object from a canvas into the simulation. /// </summary> /// <param name="cnvContainer">The static holder to add</param> public void AddStaticHolder(PhysicsStaticHolder staticHolder) { if (!PhysicsObjects.ContainsKey(staticHolder.Body)) { throw new Exception("A PhysicsStaticHolder exists with an invalid Body value of '" + staticHolder.Body + "'."); } Body body1 = PhysicsObjects[staticHolder.Body].BodyObject; body1.IsStatic = true; staticHolder.Visibility = Visibility.Collapsed; }
/// <summary> /// Completely remove a sprite /// </summary> /// <param name="sprite"></param> public void DeletePhysicsObject(string sprite) { bool found = PhysicsObjects.Where(e => e.Key == sprite).Count() > 0; if (found) { PhysicsObjects[sprite].IsActive = false; PhysicsObjects[sprite].Update(); Simulator.Remove(PhysicsObjects[sprite].BodyObject); Simulator.Remove(PhysicsObjects[sprite].GeometryObject); _parentCanvas.Children.Remove(_parentCanvas.FindName(sprite) as UIElement); PhysicsObjects.Remove(sprite); } }
private void OnWorldObjectsChanged(object sender, NotifyCollectionChangedEventArgs e) { WorldObjects = new ObservableRangeCollection <IWorldObject>( WorldObjects.OrderBy(x => x.ZIndex) .ThenBy(x => (x is IParallaxable p) ? p.ParallaxMultiplier : 1.0f) .ThenBy(x => (x is ITextured t) ? t.Material.Textures[(int)Material.TextureType.Diffuse].Texture.GetHashCode() : 0)); WorldObjects.CollectionChanged += OnWorldObjectsChanged; // update the PhysicsObjects list with all physics objects. PhysicsObjects.Clear(); foreach (IWorldObject wObj in WorldObjects) { if (wObj is IPhysicsObject physObj) { PhysicsObjects.Add(physObj); } } }
/// <summary> /// Adds a single XAML element into the Physics simulation. /// </summary> /// <param name="cnvContainer">The container to add items from.</param> public PhysicsSprite AddPhysicsBody(UIElement element, UIElement boundaryElement) { string thisName = element.GetValue(Canvas.NameProperty).ToString(); element.SetValue(Canvas.TagProperty, thisName); // special case: if we are adding a UserControl _AND_ the UserControl contains one or more // Physics Controllers, then we handle that UserControl as a "nested" canvas! if (element is UserControl) { Canvas cnvNestedPhysics = FindPhysicsCanvas(element as UserControl); if (cnvNestedPhysics != null) { EnsureUniqueNames(cnvNestedPhysics); AddPhysicsBodyForCanvas(cnvNestedPhysics); return(null); } } // look to see if we have processed this object before, and if so, // use its point collection instead of recalculating List <Point> points = FindMatchingUIElement(element); string nameKey = (string)element.GetValue(Canvas.NameProperty); PhysicsSprite polygon = new PhysicsSprite(Simulator, _parentCanvas, element, points, DebugMode, (float)FrictionDefault, boundaryElement); polygon.Name = nameKey; if (polygon.OutlinePoints != null) { polygon.Collision += new PhysicsSprite.CollisionHandler(polygon_Collision); _parentCanvas.Children.Add(polygon); PhysicsObjects.Add(polygon.Name, polygon); polygon.Update(); } //if (MousePickEnabled) //{ polygon.MouseLeftButtonDown += new MouseButtonEventHandler(polygon_MouseLeftButtonDown); polygon.MouseLeftButtonUp += new MouseButtonEventHandler(polygon_MouseLeftButtonUp); polygon.MouseMove += new MouseEventHandler(polygon_MouseMove); //} return(polygon); }
/// <summary> /// Adds a single joint objectt from a Canvas into the simulation. /// </summary> /// <param name="cnvContainer">The Canvas to add joints from</param> public void AddJoint(PhysicsJoint joint) { if (!PhysicsObjects.ContainsKey(joint.BodyOne)) { throw new Exception("A PhysicsJoint exists with an invalid BodyOne value of '" + joint.BodyOne + "'."); } if (!PhysicsObjects.ContainsKey(joint.BodyTwo)) { throw new Exception("A PhysicsJoint exists with an invalid BodyTwo value of '" + joint.BodyTwo + "'."); } Body body1 = PhysicsObjects[joint.BodyOne].BodyObject; Body body2 = PhysicsObjects[joint.BodyTwo].BodyObject; Geom geom1 = PhysicsObjects[joint.BodyOne].GeometryObject; Geom geom2 = PhysicsObjects[joint.BodyTwo].GeometryObject; Vector2 ptCollisionCenter = new Vector2((float)(joint.GetCenter().X), (float)(joint.GetCenter().Y)); RevoluteJoint revoluteJoint = JointFactory.Instance.CreateRevoluteJoint(Simulator, body1, body2, ptCollisionCenter); joint.RevoluteJointObject = revoluteJoint; if (joint.AngleSpringEnabled) { float springConstant = joint.AngleSpringConstant; float dampeningConstant = joint.AngleSpringDampningConstant; AngleSpring angleSpring = new AngleSpring(body1, body2, springConstant, dampeningConstant); Simulator.Add(angleSpring); } if (joint.CollisionGroup > 0) { geom1.CollisionGroup = joint.CollisionGroup; geom2.CollisionGroup = joint.CollisionGroup; } joint.Visibility = Visibility.Collapsed; }
public static void removePhysicsObject(PhysicsObject Object) { PhysicsObjects.Remove(Object); }
public static void addPhysicsObject(PhysicsObject Object) { Window.Controls.Add(Object.getBox()); PhysicsObjects.Add(Object); }
public PhysicsSprite AddPhysicsBody(PhysicsObjectMain physObject) { FrameworkElement element = physObject.VisualElement; string thisName = element.GetValue(Canvas.NameProperty).ToString(); element.SetValue(Canvas.TagProperty, thisName); // special case: if we are adding a UserControl _AND_ the UserControl contains one or more // Physics Controllers, then we handle that UserControl as a "nested" canvas! if (BoundaryHelper.IsInsideNestedUsercontrol(element)) //element is UserControl) { UserControl ucParent = BoundaryHelper.GetParentUserControl(element); Canvas cnvNestedPhysics = FindPhysicsCanvas(ucParent); if (cnvNestedPhysics != null) { EnsureUniqueNames(cnvNestedPhysics); if (element is UserControl) { AddPhysicsBodyForCanvasWithBehaviors(cnvNestedPhysics); return(null); } } } // look to see if we have processed this object before, and if so, // use its point collection instead of recalculating List <Point> points = FindMatchingUIElement(element); string nameKey = (string)element.GetValue(Canvas.NameProperty); FrameworkElement boundaryElement = null; if (physObject.BoundaryElement != null && physObject.BoundaryElement != string.Empty) { boundaryElement = element.FindName(physObject.BoundaryElement) as FrameworkElement; } if (nameKey == string.Empty) { nameKey = GetUniqueSpriteName(); element.Name = nameKey; } PhysicsSprite sprite = new PhysicsSprite(Simulator, _parentCanvas, element, points, DebugMode, (float)FrictionDefault, boundaryElement); sprite.Name = nameKey; if (sprite.OutlinePoints != null) { sprite.Collision += new PhysicsSprite.CollisionHandler(polygon_Collision); _parentCanvas.Children.Add(sprite); PhysicsObjects.Add(sprite.Name, sprite); sprite.Update(); } sprite.MouseLeftButtonDown += new MouseButtonEventHandler(polygon_MouseLeftButtonDown); sprite.MouseLeftButtonUp += new MouseButtonEventHandler(polygon_MouseLeftButtonUp); sprite.MouseMove += new MouseEventHandler(polygon_MouseMove); sprite.GeometryObject.CollisionGroup = physObject.CollisionGroup; sprite.BodyObject.IsStatic = physObject.IsStatic; if (physObject.RestitutionCoefficient != 0) { sprite.GeometryObject.RestitutionCoefficient = (float)physObject.RestitutionCoefficient; } if (physObject.FrictionCoefficient != 0) { sprite.GeometryObject.FrictionCoefficient = (float)physObject.FrictionCoefficient; } if (physObject.MomentOfIntertia != 0) { sprite.BodyObject.MomentOfInertia = (float)physObject.MomentOfIntertia; } if (physObject.Mass != 0) { sprite.BodyObject.Mass = (float)physObject.Mass; } return(sprite); }