Guest User

Untitled

a guest
Jun 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Media;
  5. using FarseerPhysics.Dynamics;
  6.  
  7. namespace FarseerTransforms
  8. {
  9. public class BodyViewModel : INotifyPropertyChanged
  10. {
  11. private Fixture _fixture;
  12. private CompositeTransform _transform = new CompositeTransform();
  13. private System.Windows.Media.GeneralTransform _worldToCanvas;
  14. private double _width;
  15. private double _height;
  16.  
  17. public BodyViewModel(Fixture fixture, double width, double height, System.Windows.Media.GeneralTransform worldToCanvas)
  18. {
  19. _fixture = fixture;
  20. _width = width;
  21. _height = height;
  22. _worldToCanvas = worldToCanvas;
  23.  
  24. // rotate around center
  25. _transform.CenterX = width / 2;
  26. _transform.CenterY = height / 2;
  27. // adjust for center
  28. var position = _worldToCanvas.Transform(new Point(_fixture.Body.Position.X, _fixture.Body.Position.Y));
  29. _transform.TranslateX = position.X - width / 2;
  30. _transform.TranslateY = position.Y - width / 2;
  31. _transform.Rotation = _fixture.Body.Rotation * 360 / (2 * Math.PI);
  32. }
  33.  
  34. public event PropertyChangedEventHandler PropertyChanged;
  35.  
  36. public double Width
  37. {
  38. get { return _width; }
  39. }
  40.  
  41. public double Height
  42. {
  43. get { return _height; }
  44. }
  45.  
  46. public CompositeTransform RenderTransform
  47. {
  48. get { return _transform; }
  49. }
  50.  
  51. internal void Notify()
  52. {
  53. var position = _worldToCanvas.Transform(new Point(_fixture.Body.Position.X, _fixture.Body.Position.Y));
  54. _transform.TranslateX = position.X - Width / 2;
  55. _transform.TranslateY = position.Y - Height / 2;
  56. _transform.Rotation = _fixture.Body.Rotation * 360 / (2 * Math.PI);
  57. if (PropertyChanged != null)
  58. {
  59. PropertyChanged(this, new PropertyChangedEventArgs("RenderTransform"));
  60. }
  61. }
  62. }
  63. }
Add Comment
Please, Sign In to add comment