Search Unity

Pinball flippers. (swatting objects using physics)

Discussion in 'Scripting' started by TinyTracker, Dec 31, 2013.

  1. TinyTracker

    TinyTracker

    Joined:
    Nov 10, 2012
    Posts:
    54
    I've looked up how to do pinball flippers. From my research...

    Some have suggested using hinge joints. However I can't get the flipper to be stationary or not move so this isn't feasible for a pinball flipper as pinball flippers aren't supposed to have any give from contact with the ball.

    Some have suggested using configurable joints. However the documentation on how to properly use this feature or what this feature is even intended for seems quite vague. As most of the information I have dug up on configurable joints show how to rig something for ball and chain type rigs which isn't anything close to what I want.


    I can't help but think maybe I should just try to fake it by applying a force to the ball when in the vicinity of a flipper that is going through a swatting motion.

    Is there a simple way to get a pinball flipper?
     
  2. Peter Ples

    Peter Ples

    Joined:
    Mar 12, 2013
    Posts:
    237
    I'm very new to the physics. I haven't done this, but just an idea you might want to try.

    Make an animation for your flipper object. Make sure it also has a collider and all that physics stuff. Set it to kinematic I think... And then add a script that says when input whatever - then play animation, the physics should work together?
     
  3. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    hinge joints and motors are what I used in my pinball game.

    With this setup there is a limit to the "targetVelocity" in relation to the mass of the rigedbody. If you go over this limit the joint becomes erratic jumping the whole time but if you are below this breaking limit it works perfectly.

    I cant say what the relation between the mass and this force is .. I just tested it by trial and error and watched the transform inspectors values to make sure the rotational values came to a stand still after each use of the motor.

    http://docs.unity3d.com/Documentation/ScriptReference/HingeJoint-motor.html

    You can download my game for android ( link in my sig ) or view it in your browser here :

    http://www.shaderbytes.co.za/tortuga_tales/

    CRTL keys for flippers
    SHIFT keys for nudge
    ENTER key for ball launch
     
    Last edited: Dec 31, 2013
  4. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    I should also mention you need to use capsule colliders for the flipper collision object if you want to use continuous dynamic algorithm on the ball and flippers. Continuous dynamic only supports primitive colliders.
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I would've said that using a pre-built physics solution for a game where the physics IS the gameplay (like pinball) is a bad idea,
    You can't really expect to plug in an all-purpose middleware physics sim like physX or havok (which are great for effects), and expect it to give a good realistic feel that some of the 'popular' pinball titles have (which I expect have purpose-built physics for the gameplay)

    But shaderbytes game feels pretty good, so I might have to have another go at getting it right (What scale are you working at? ie is the ball actually 0.027 units (1 1/16 inches standard ball size) or are you working with everything bigger)
     
  6. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    @hpjohn

    Im very sure zen pinball and other top pinball production studios have custom physics solutions as you mention.

    Everything in my game is super size big eg.. I think the ball was 0.48 unity units, so that is like just under half a meter! (diameter) I was of the mind set that working with extremely small objects would be subject to more floating point errors ? not sure about that ?

    My Gravity was about 150 or 170 and table slope about 5 degrees
     
    Last edited: Jan 1, 2014
  7. TinyTracker

    TinyTracker

    Joined:
    Nov 10, 2012
    Posts:
    54
    Cool I'll try it out!

    The reason why I'm interested in this. I tried out zen pinball and then I tried out The Pinball Arcade on steam. (it's free to try out) And I couldn't help but feel the ball movement was a bit sluggish. Especially when I compared the real tables like Attack from Mars and Funhouse to the simulated version in The pinball arcade. The ball felt sluggish and the ball felt glued to the table and didn't have the ability to be kicked up off the table when hitting a pop bumper.

    Simulated Funhouse pinball table http://www.youtube.com/watch?v=ADBFGN0VaHM
    The real Funhouse pinball table http://www.youtube.com/watch?v=BhPsXaQkqlM
     
  8. Danicano

    Danicano

    Joined:
    Mar 10, 2015
    Posts:
    18
    _______________
    Wow, nice game!!!
    I'm also trying to maka a pinball but i'm stucked at the flippers!!!! I used hingejoint and motor but when the ball is held by the flipper, the flipper stills adds force to ball and it makes it bump.
    Video
     
  9. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    Hi @Danicano

    wow that game of mine is old now hehe , do you have a gaming pc? you can try my latest table , its a recreation of a real life table and also uses proper rom emulation :

    https://drive.google.com/open?id=0B5GQag5iS_vzMHpFZFJlUlE4a2c

    you didnt mention what version of unity you are using? The thing is its tricky to say what values to use because the scale of the objects and their mass all play a part. Have you set physics layers up for your collisions , I do this in my tables so objects like flippers and table surface are all on one physics layer which I then set to not collide with themselves... they only collide with the balls which are on a different physics layer.
     
  10. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    @Danicano I just looked at your video again .. your flippers joint is moving when you use the flipper , look carefully, when your flipper rotates it is not rotating perfectly around a point .. it shifts position. I'm sure this would be playing a part as to why your flipper is not remaining stable..
     
  11. Danicano

    Danicano

    Joined:
    Mar 10, 2015
    Posts:
    18
    Hi. Thanks for answering!!!! Sorry for my late answer !!!!!I'm now using 5.3.4f1!!!!
    Wow. You are right! My flippers don`t rotate perfectly!!! I'll work on that and I'll report!!!!
    Thank you very much.
    I tried your Elvis pinball and I think you've made an awesome job!
    Whish I have your skills!
     
    Last edited: May 2, 2016
  12. Danicano

    Danicano

    Joined:
    Mar 10, 2015
    Posts:
    18
    I'm struggling with the "perfect" rotation around a point and I can't get it. Wherever I put the joint, when a important force or speed is applied, the joint goes a little "mad"....:confused:
     
  13. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    How are your setting up the joint ? In the inspector or are you building it at runtime?
    How are you moving the joint , via the joint motor force property?
    Have you increrased your rigidbody maxAngularVelocity enough?
    Did you make sure the flipper is not colliding with the playfield , did you set up physics layers and exclude collisions on the same layer as I mentioned before?

    Some other properties I set on my joints are :

    Code (csharp):
    1.  
    2. joint.enableCollision = false;
    3.         joint.enablePreprocessing = false;      
    4.         joint.autoConfigureConnectedAnchor = true;
    5.  
    I also set my axis manually but that is to control rotation direction and depends on the specific orientation , so yours might not be the same as mine, just letting you know

    Code (csharp):
    1.  
    2. joint.axis = Vector3.forward*-1f;
    3.  
    show me your code??
     
  14. Danicano

    Danicano

    Joined:
    Mar 10, 2015
    Posts:
    18
    Wow. Lots of information here.
    I'm setting up the joint via inspector
    The joint moves via motor
    I tried to increase maxAngularVelocity to 10? (the manual says the default is 7 )
    I followed your advise and made a layer for non collision objects.

    Code (CSharp):
    1. HingeJoint hinge;
    2. JointMotor motor;
    3. public KeyCode myKey;
    4.  
    5. void Start(){
    6. GetComponent<Rigidbody>().maxAngularVelocity =10f;
    7. }
    8.  
    9. void Update () {
    10.  
    11.  
    12. if (Input.GetKeyDown(myKey) || Input.GetKeyUp(myKey)){
    13.  
    14. HingeJoint hinge = GetComponent<HingeJoint>();
    15. JointMotor motor = hinge.motor;
    16. motor.targetVelocity = -motor.targetVelocity;
    17. hinge.motor = motor;
    18. }
    19. }
    I simply turn the velocity of the motor a negative value to move the flipper...

    But after struggilng with settings, I realized that the bounciness threshold was 1 and I set it to 2, and violà!, with this setting and adjusting the flippers bounciness (via material) I've managed the ball to stay still.

    But all your advises are a huge help to me. Thanks for that!!!

    Now I will going on and try it make it work like I wish, playing with all settings...

    Thanks again!!!
     
    Last edited: May 5, 2016
  15. Danicano

    Danicano

    Joined:
    Mar 10, 2015
    Posts:
    18
    The new problem I'm facing now is that the ball pass through the flippers and fall sometimes...
     
  16. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    @Danicano look at my previous posts , I made mention that you need to use a capsule collider on the flippers. This is because only primitive shapes support "continuous dynamic" collisions , which is what you need. Also the ball is a primitive collider so make sure you have set continuous dynamic on this as well.
     
  17. Danicano

    Danicano

    Joined:
    Mar 10, 2015
    Posts:
    18
    mmm, my flippers have box colliders. I suppose box collider is a primitive shape as well.

    But I'll try on capsules too...
     
    Last edited: May 5, 2016
  18. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    yes a box collider is a primitive .. did you set the collision mode to continuous dynamic on the flippers and the ball?

    Also you should use a capsule because a pinball flipper has to be rounded on the end
     
  19. Danicano

    Danicano

    Joined:
    Mar 10, 2015
    Posts:
    18
    Wow, I'm not experimenting the problem when I put capsules instead of boxes. By the moment it's working!
    Thanks for your help!!!
     
  20. Danicano

    Danicano

    Joined:
    Mar 10, 2015
    Posts:
    18


    Thanks to some hours of experimenting with settings and specially thanks to shaderbytes!!!

    Dedicated to you!!!

    P.S. now is the turn to learn to model with Blender!
     
    shaderbytes likes this.
  21. mr_turkey11

    mr_turkey11

    Joined:
    Nov 12, 2020
    Posts:
    2
    Don't click link it leads to Porn
     
  22. NewerMan420

    NewerMan420

    Joined:
    Sep 16, 2022
    Posts:
    4
    I'm using version 2021.3.11f1, and I can't find a way to get the flippers to flip. :(
    It's twitching violently.
     
    Last edited: Nov 3, 2022
  23. eoniantech

    eoniantech

    Joined:
    Jul 1, 2014
    Posts:
    7
    you need to use a hinge joint like below
    upload_2022-11-15_12-43-13.png
     
  24. eoniantech

    eoniantech

    Joined:
    Jul 1, 2014
    Posts:
    7
    content of flipperPhysic.cs script
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class FlipperPhysic : MonoBehaviour
    4. {
    5.     public HingeJoint hinge;
    6.     public float targetPosition;
    7.  
    8.     public void Down()
    9.     {
    10.         var spring = hinge.spring;
    11.         spring.targetPosition = 0;
    12.         hinge.spring = spring;
    13.     }
    14.  
    15.     public void Up()
    16.     {
    17.         var spring = hinge.spring;
    18.         spring.targetPosition = targetPosition;
    19.         hinge.spring = spring;
    20.     }
    21. }
    22.