Search Unity

2D Car Physics.

Discussion in '2D' started by eprauz, Nov 26, 2013.

  1. eprauz

    eprauz

    Joined:
    Feb 9, 2013
    Posts:
    9
    Hi! I'm trying to create a prototype 2D physics based game. (like Hill Climb Racing).

    I'm newbie so I have a problem. I was created two wheels:

    $CarWheel.png $CarWheel.png

    and a 'transmission':

    $Transmiss.png

    And added to sprites colliders rigibodies. And for first wheel i added two spring joints (first SJ connected wheels, second connected wheel and transmission), for second i added one spring joint.

    So I have something like this:

    $ss1.png


    On a flat surface all good, but this is what happening on an inclined surface:

    $ss4.png

    $ss7.png

    What I'm doing wrong?
     
  2. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Do not connect the wheels to each other.
     
  3. eprauz

    eprauz

    Joined:
    Feb 9, 2013
    Posts:
    9
    Then wheels disperse in different directions.

    "Transmission" should turn but it doesn't.
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    I tried doing what you did and got really strange results as well. Its kinda a bad solution, but you could try using both spring and slider joints to connect the tires to the body. like this:

    $qoov.png

    Where you have two spring joints on the body, which connect to the wheels, then one slider joint on the body to one wheel, and another on the other wheel, connecting to body (only because you cant have two slider joint components on one gameobject)

    this creates a semi-realistic suspension.... sorta haha

    EDIT oh another thing you must match up the numbers for the anchor points or things will go really crazy trying to fight between the joints... so if anchor of rear wheel on spring joint is x, y, then anchor of rear wheels slider must also be x, y... make sure they match or it will create strange results this way as well.

    So that means if you have two spring/slider joints on your body, which have anchor of -5 and -10 connecting to the rear wheel... then also on the rear wheel you have just a slider connecting to the body, the anchor must be POSITIVE 5 and POSITIVE 10, which matches up the locations of the joints... get it? it would be opposite because its below the body, making all the joints trying to hold all the parts in the same position, sliders allow only one axis of movement, and springs allow the... springyness
     
    Last edited: Nov 26, 2013
  5. eprauz

    eprauz

    Joined:
    Feb 9, 2013
    Posts:
    9
    I solved the problem.

    It's just trigonometry. Physics does not help here.
    At first we must deduct X-coordinate first wheel from X-coordinates second wheel.
    And so the same we must deduct Y-coordinate first wheel from Y-coordinates second wheel.
    And then calculate atan2 degrees. And always rotated the transmission by that amount.

    Maybe it will help someone.
     
  6. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Hmm I don't know exactly what you mean, but I am glad you solved your problem - best of luck!
     
  7. eprauz

    eprauz

    Joined:
    Feb 9, 2013
    Posts:
    9
    Thank u.