0 votes

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

in Engine by (17 points)

1 Answer

+1 vote
Best answer

General rule of thumb: nothing involving physics is simple - ever! ;) 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.

by (10,195 points)
edited by

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?

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

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?

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories