-
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
-
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.
-
Please link the original site :)
Caravan Parking Game
-
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?
-
-
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...
-
that caravan parking game is solid lol i done it and got -39.53 as score lol