3

Currently I am creating a 2d platformer in unity using C# and I was wondering how I would approach creating a weighing scale like the one below. enter image description here

To explain, I want it so that when a heavy rigidbody2d is placed on one of the scales, the other scale moves up and vice versa. I would also like it so that the scales balance if the weights are equal. How would I go about approaching this? Is there a component which may make this easy to accomplish?

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Gazz
  • 1,017
  • 3
  • 17
  • 31

1 Answers1

5

The Math

First of all, there is no component I know of to do this for you. We'll just have to do some physics ourselves. Skip to the bottom if you don't care about the math and just want to see what you should do. Let's pretend like there are two boxes, one on the left puller and one on the right. I'm assuming the pulley contraption with the string on it is frictionless and massless. In that case, we can draw the diagram out like so:

enter image description here

W1 is the weight of the left box, W2 is the weight of the right box. T is the tension in the rope caused by the two boxes. Let's look at just the left box for now:

enter image description here

The sum of forces in the x-direction is then Fx_1 = T - W_1, since we chose the right to be our positive direction. Let's apply Newton's second law, force = mass * acceleration: M_1 * a_1 = T - M_1 * g. M_1 is the mass of the box, a_1 is the net acceleration of the box, and g is the acceleration due to gravity. Let's rewrite this as the following: T = M_1 * a_1 + M_1 * g.

Now let's look at the box on the right:

enter image description here

Let's sum the forces in the x-direction: Fx_2 = W_2 - T. Let's rewrite this as well: T = W_2 - Fx_2. Let's apply Newton's second law: T = M_2 * g - M_2 * a_2. The tensile forces are the same for both the box on the right and on the left so we can combine both equations by setting T = T:

M_1 * a_1 + M_1 * g = M_2 * g - M_2 * a_2
Rewrite: M_1 * a_1 + M_2 * a_2 = M_2 * g - M_1 * g
Rewrite: M_1 * a_1 + M_2 * a_2 = g(M_2 - M_1)

When you put two things on a balance scale or pulley in this case, The scale tips at the same acceleration for both the left and right boxes, right? The left side of the scale will fall at the same rate that the right side will rise. So, a_1 and a_2 are actually the same value. So we can rewrite like so:

a(M_1 + M_2) = g(M_2 - M_1)
Rewrite: a = g(M_2 - M_1) / (M_1 + M_2)

Conclusion

So, the system has a net acceleration to the right of g(M_2 - M_1) / (M_1 + M_2). Thus, if you know the mass of both boxes, and you know the acceleration due to gravity (you probably set these values to begin with), then this formula tells you how quickly the right box falls and how quickly the left box rises.

If the equation gives you a negative value, that means the box on the left is heavier than the box on the right. The formula then tells you how fast the box on the left falls and how fast the box on the right rises.

If the masses are equal then the system will have a net acceleration of 0 and neither box will move.

How to do this in Unity

You know the mass of your boxes and you know the acceleration of both boxes with the formula above (a = g(M_2 - M_1) / (M_1 + M_2)). So, attach a script to either thing you want to move and write the following:

this.GetComponent<Rigidbody>().AddForce(Vector3.down * MASS_OF_THIS_OBJECT * CALCULATED_ACCELERATION);

That should work. Let me know if it doesn't and I'll fix my answer.

bpgeck
  • 1,592
  • 1
  • 14
  • 32
  • I'm really late to the party here. Your math is an excellent example of how to do this and it's really helpful. Do you know a way to calculate this without using the weight of a single object on each scale? For example, I would like to have a working scale where any number of objects could be on the scale, or any force could be pushing/pulling on them. – Sean256 Apr 27 '20 at 02:51