Dealing with physics of spinng wheels

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JonnyTech

Newbie question about physics, thought it would be simple…: Trying to make a digital prize wheel for a charity event. The main “spinning wheel” has multiple RigidBody2D “peg” nodes to interact with a “pin” RigidBody2D node. But when they collide both the “wheel” and the “pin” bounce off in opposite directions. How do I make the “wheel” and “pin” nodes become fixed to the background and make the “pin” sping back and forth when it collides with the “pegs”? Any pointers greatly appreciated. This video demonstrates the idea:

spinner

:bust_in_silhouette: Reply From: njamster

General rule of thumb: nothing involving physics is simple - ever! :wink: It would probably be a lot easier to fake all of this by generating a random number in the backend and then playing one of two animations (one where the pin ends up on a “WIN”, another one where it lands on a “LOSE”) depending on its value.

That being said, if you still want to do it your way, take a look at PinJoint2D.
Your wheel would be a RigidBody2D (with gravity set to 0) and each peg a StaticBody2D, added as a child and offset in position, so it lies on the edge of the spinning disk. Now add a PinJoint2D for each of your pegs, where “Node A” always is the RigidBody2D of your wheel and “Node B” is the StaticBody2D of your peg (set these as properties in the inspector).

If you now apply any angular_velocity to the RigidBody2D, the pegs surrounding it should start rotating around the center (but will slow down over time because of the simulated physics). So all that’s left to do now is adding the pin by placing another RigidBody2D and StaticBody2D (but this time: not as children of the wheel!) and connect them via a final PinJoint2D. The static part will keep the pin “nailed to the wall”, while the rigid part will behave flexible and be pushed aside by the pegs moving by and colliding with it.

Thank you very much @njamster for taking the time to leave a detailed explanation, it is very helpful. Why do each of the “peg” objects require a PinJoint2D? Are they not simply fixed to the “wheel” as children? It seems that the only joint required is for the spring of the “needle” or am I missing some logic?

JonnyTech | 2020-02-07 00:20

You’re absolutely right, was thinking to complicated! Great catch, I edited my original answer for any future readers. :slight_smile:

njamster | 2020-02-07 16:17

Brilliant! To wrap this up and make it a complete answer: how to make the “pin” spring back to its original position pointing towards the board? Currently the “pegs” knock it away and it remains in that position. Is there a setting that makes it return to a ‘home’ position as if it were held with a spring? Or does it require code?

JonnyTech | 2020-02-08 02:40

My solution was to add a DampedSpringJoint2D at the bottom of the “pin” to pull it back to the resting position as well as the PinJoint2D at its centre of rotation.

JonnyTech | 2020-02-09 03:31