Example #1
0
		private static void IsCollidableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			PhysicsController physicsController = FindParentPhysicsController(d);
			if (physicsController != null)
			{
				foreach (Body body in physicsController.Engine.Bodies)
				{
					if (body.Tag != null && body.Tag.Equals(d))
						body.IsCollidable = (bool)e.NewValue;
				}
			}
		}
Example #2
0
		void CreateNewBody(FrameworkElement element)
		{
			double angle = 0;
			if (element.RenderTransform is RotateTransform)
				angle = ((RotateTransform)element.RenderTransform).Angle;
			PhysicsState state = new PhysicsState(new ALVector2D(angle, Canvas.GetLeft(element) + (element.ActualWidth / 2),
			                                                     Canvas.GetTop(element) + (element.ActualHeight / 2)));

			Shape shape = new PolygonShape(PolygonShape.CreateRectangle(element.ActualHeight, element.ActualWidth), 2);
			MassInfo mass = MassInfo.FromPolygon(shape.Vertexes, 1);
			Body body = new CustomBody(state, shape, mass, new Coefficients(0, 1), new Lifespan());
			body.LinearDamping = 0.95;
			body.AngularDamping = 0.95;
			body.IsCollidable = GetIsCollidable(element);
			body.Tag = element;

			PhysicsController controller = FindParentPhysicsController(element);
			if (controller != null)
				controller.engine.AddBody(body);
			itemToBody.Add(element, body);
		}
Example #3
0
		private static void EnablePhysicsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			FrameworkElement element = d as FrameworkElement;
			if (element == null)
				throw new ArgumentException("Only FrameworkElement, and it descendants, are supported");

			PhysicsController controller = FindParentPhysicsController(element);
			if (controller != null)
			{
				if ((bool)e.NewValue)
					element.SizeChanged += controller.Element_SizeChanged;
				else
				{
					element.SizeChanged -= controller.Element_SizeChanged;
					Body body;
					if(controller.itemToBody.TryGetValue(element, out body))
					{
						body.Lifetime.IsExpired = true;
						controller.itemToBody.Remove(element);
					}
				}
			}
		}