Client LuaCsForBarotrauma
JointBendDeformation.cs
1 using System;
2 using System.Xml.Linq;
3 using Microsoft.Xna.Framework;
4 
6 {
8  {
9  public JointBendDeformationParams(XElement element) : base(element)
10  {
11  }
12  }
13 
18  {
19  //how much to bend at the right side of the sprite
20  private float bendRight;
21  public float BendRight
22  {
23  get { return bendRight; }
24  set { bendRight = MathHelper.Clamp(value, -MaxRotationInRadians, MaxRotationInRadians); }
25  }
26  //the pivot point to rotate the right side around
27  public Vector2 BendRightRefPos = new Vector2(1.0f, 0.5f);
28 
29  private float bendLeft;
30  public float BendLeft
31  {
32  get { return bendLeft; }
33  set { bendLeft = MathHelper.Clamp(value, -MaxRotationInRadians, MaxRotationInRadians); }
34  }
35  public Vector2 BendLeftRefPos = new Vector2(0.0f, 0.5f);
36 
37  private float bendUp;
38  public float BendUp
39  {
40  get { return bendUp; }
41  set { bendUp = MathHelper.Clamp(value, -MaxRotationInRadians, MaxRotationInRadians); }
42  }
43  public Vector2 BendUpRefPos = new Vector2(0.5f, 0.0f);
44 
45  private float bendDown;
46  public float BendDown
47  {
48  get { return bendDown; }
49  set { bendDown = MathHelper.Clamp(value, -MaxRotationInRadians, MaxRotationInRadians); }
50  }
51  public Vector2 BendDownRefPos = new Vector2(0.5f, 1.0f);
52 
53  public Vector2 Scale = Vector2.Zero;
54 
55  private float MaxRotationInRadians => MathHelper.ToRadians(Params.MaxRotation);
56 
57  public JointBendDeformation(XElement element) : base(element, new JointBendDeformationParams(element)) { }
58 
59  protected override void GetDeformation(out Vector2[,] deformation, out float multiplier, bool inverse)
60  {
61  deformation = Deformation;
62  multiplier = 1.0f;// this.multiplier;
63  }
64 
65  public override void Update(float deltaTime)
66  {
67  Vector2 normalizedPos = Vector2.Zero;
68  for (int x = 0; x < Resolution.X; x++)
69  {
70  normalizedPos.X = x / (float)(Resolution.X - 1);
71  for (int y = 0; y < Resolution.Y; y++)
72  {
73  normalizedPos.Y = y / (float)(Resolution.Y - 1);
74  Deformation[x, y] = Vector2.Zero;
75 
76  if (Math.Abs(BendLeft) > 0.001f)
77  {
78  float strength = 1.0f - normalizedPos.X;//(1.0f - Math.Max(normalizedPos.X - BendLeftRefPos.X, 0.0f) / (1.0f - BendLeftRefPos.X));
79  strength = (strength - 0.5f) * 2.0f;
80  if (strength > 0.0f)
81  {
82  Vector2 rotatedP = RotatePointAroundTarget(normalizedPos, BendLeftRefPos, BendLeft * strength * Params.Strength);
83  Vector2 offset = rotatedP - normalizedPos;
84  offset.X *= Scale.Y / Scale.X;
85  Deformation[x, y] += offset;
86  }
87  }
88  if (Math.Abs(BendRight) > 0.001f)
89  {
90  float strength = normalizedPos.X;//(1.0f - Math.Max(BendRightRefPos.X - normalizedPos.X, 0.0f) / (BendRightRefPos.X));
91  strength = (strength - 0.5f) * 2.0f;
92  if (strength > 0.0f)
93  {
94  Vector2 rotatedP = RotatePointAroundTarget(normalizedPos, BendRightRefPos, BendRight * strength * Params.Strength);
95  Vector2 offset = rotatedP - normalizedPos;
96  offset.X *= Scale.Y / Scale.X;
97  Deformation[x, y] += offset;
98  }
99  }
100 
101  if (Math.Abs(BendUp) > 0.001f)
102  {
103  float strength = 1.0f - normalizedPos.Y;//(1.0f - Math.Max(normalizedPos.Y - BendUpRefPos.Y, 0.0f) / (1.0f - BendUpRefPos.Y));
104  strength = (strength - 0.5f) * 2.0f;
105  if (strength > 0.0f)
106  {
107  Vector2 rotatedP = RotatePointAroundTarget(normalizedPos, BendUpRefPos, BendUp * strength * Params.Strength);
108  Vector2 offset = rotatedP - normalizedPos;
109  offset.Y *= Scale.X / Scale.Y;
110  Deformation[x, y] += offset;
111  }
112  }
113  if (Math.Abs(BendDown) > 0.001f)
114  {
115  float strength = normalizedPos.Y;//(1.0f - Math.Max(BendDownRefPos.Y - normalizedPos.Y, 0.0f) / (BendDownRefPos.Y));
116  strength = (strength - 0.5f) * 2.0f;
117  if (strength > 0.0f)
118  {
119  Vector2 rotatedP = RotatePointAroundTarget(normalizedPos, BendDownRefPos, BendDown * strength * Params.Strength);
120  Vector2 offset = rotatedP - normalizedPos;
121  offset.Y *= Scale.X / Scale.Y;
122  Deformation[x, y] += offset;
123  }
124  }
125  }
126  }
127  }
128 
129  public static Vector2 RotatePointAroundTarget(Vector2 point, Vector2 target, float angle)
130  {
131  return RotatePointAroundTarget(point, target, (float)Math.Sin(angle), (float)Math.Cos(angle));
132  }
133 
134  public static Vector2 RotatePointAroundTarget(Vector2 point, Vector2 target, float sin, float cos)
135  {
136  Vector2 dir = point - target;
137  var x = (cos * dir.X) - (sin * dir.Y) + target.X;
138  var y = (sin * dir.X) + (cos * dir.Y) + target.Y;
139  return new Vector2(x, y);
140  }
141  }
142 }
Does a rotational deformations around pivot points at the edges of the sprite.
static Vector2 RotatePointAroundTarget(Vector2 point, Vector2 target, float angle)
override void GetDeformation(out Vector2[,] deformation, out float multiplier, bool inverse)
static Vector2 RotatePointAroundTarget(Vector2 point, Vector2 target, float sin, float cos)