A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Car Trailer Physics

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    9

    Car Trailer Physics

    I am attempting to create a game in flash in which a user is required to drive a car with a trailer attached around various obstacles using the arrow keys.

    I am able to get the car working fine but I am having problems scripting the physics for the trailer.

    If anyone could help with this matter it would be much appriciated.

    There is an example of what I am trying to create at:

    http://www.ukcampsite.co.uk/games/reverse.asp

  2. #2
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    You can do it using IK (seems what they've done). All IK is (I was surprised how simple it was) is setting a distance between 2 nodes, and then when you move one, you move the other so that the distance requirement is met. The rear of the car would be one node, and then the rear of the trailer would be the other, and then each frame you do:
    Code:
    var dx:Number = car_rear.x - trailer_rear.x;
    var dy:Number = car_rear.y - trailer_rear.y;
    var rads:Number = Math.atan2( dy, dx);
    trailer_rear.x = car_rear.x + Math.cos( rads) * distance;
    trailer_rear.y = car_rear.y + Math.sin( rads) * distance;
    trailer_rear.rotation = 180/Math.PI * rads;
    The only thing you need to define before hand is the distance variable, which will be the distance you want to keep between the car and trailer.

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  3. #3
    Senior Member Sietjp's Avatar
    Join Date
    Jan 2005
    Location
    Paris, France
    Posts
    711
    Please link the original site
    Caravan Parking Game

  4. #4
    Junior Member
    Join Date
    Apr 2009
    Posts
    9

    Pazil

    Thank you for your reply. I have attempted to implement the code you have sent me although have had no success.

    I was wondering if the script is AS3 only? Or does it have to be published in a certain version e.g. 10?

  5. #5
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  6. #6
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    Technically that's as3, but the math and code itself is pretty vague. You just need to add an underscore before the x, y, and rotation variables and it should run in as2 as well.

    I might be able to better help you if you post an image with you're work environment, just so I get an idea of what the mc's look like...
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  7. #7
    Senior Member
    Join Date
    Apr 2009
    Posts
    117
    that caravan parking game is solid lol i done it and got -39.53 as score lol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center