|
-
Pumpkin Carving 2008
Train Simulation; Need physics instead of TweenMax
Hey all. I've been busy over the past year or so with college and work, but I've finally transferred to another school and now I have a couple weeks to play with flash again. I've always envisioned finishing the train engine that I started here in Eager Beaver's thread at post #15. I originally had worked with Mr. Malee to come up with a working, though not 100%, piece of code to take an x/y velocity for the train and manipulate it through straight rails as well as 90 degree turns.
Fast forward a year. I've since formatted and thus lost my code. So I started a completely new project and took a completely different approach to the engine. I had previously read up a bit on TweenMax, which seemed to fit the bill quite nicely, so I continued on with the data side of the project and boiled the whole engine down into 12 actual tweens:
Code:
var NS:Object = {sx:8, sy:0, ex:8, ey:16, cx:8, cy:16};
var SN:Object = {sx:8, sy:16, ex:8, ey:0, cx:8, cy:0};
var NW:Object = {sx:8, sy:0, ex:0, ey:8, cx:16, cy:16};
var WN:Object = {sx:0, sy:8, ex:8, ey:0, cx:16, cy:16};
var NE:Object = {sx:8, sy:0, ex:16, ey:8, cx:0, cy:16};
var EN:Object = {sx:16, sy:8, ex:8, ey:0, cx:0, cy:16};
var SE:Object = {sx:8, sy:16, ex:16, ey:8, cx:16, cy:16};
var ES:Object = {sx:16, sy:8, ex:8, ey:16, cx:16, cy:16};
var WS:Object = {sx:0, sy:8, ex:8, ey:16, cx:0, cy:16};
var SW:Object = {sx:8, sy:16, ex:0, ey:8, cx:0, cy:16};
var EW:Object = {sx:16, sy:8, ex:0, ey:8, cx:0, cy:8};
var WE:Object = {sx:0, sy:8, ex:16, ey:8, cx:16, cy:8};
These would be to/from any side to any other side, assuming each side only has a single exit (which in my engine is true). From here, I started writing a way to dynamically look up each tile's tween object:
Code:
var paths:Array = [];
paths[0] = {switched:false};
paths[1] = {switched:false};
paths[2] = {switched:false};
paths[3] = {switched:false};
paths[4] = {switched:false};
paths[5] = {switched:false};
paths[6] = {switched:false};
paths[7] = {switched:false};
paths[8] = {switched:false};
paths[9] = {switched:false};
paths[10]= {switched:false};
paths[11]= {switched:false};
paths[12]= {switched:false};
paths[13]= {switched:false};
paths[14]= {switched:false};
This array contains the start of would be an array to look up the tween objects, one for each of the 15 different types of tiles I have. Continuing further with this approach, I wanted to find a way to have the vehicle calculate it's next move on the fly. Since it would only ever be in one of four cardinal directions when it does this, I took the rotation of the vehicle, divided it by 90, and added 2. Voila, I always get a number 0-3. From there, and in the above code, you can see I needed a way for "switchable" sections of the track to be kept track of. Combine the "switched" boolean with a private member of "canSwitch", and you can probably decipher what the follow code means:
Code:
paths[0].e0 = {canSwitch:false, p00:WN};
paths[0].e1 = {canSwitch:false, p00:NW};
//...
paths[12].e1 = {canSwitch:true, p10:NS, p11:NE};
paths[12].e2 = {canSwitch:false, p00:EN};
paths[12].e3 = {canSwitch:false, p00:SN};
//...
paths[14].e0 = {canSwitch:false, p00:WE};
paths[14].e1 = {canSwitch:false, p00:NS};
paths[14].e2 = {canSwitch:false, p00:EW};
paths[14].e3 = {canSwitch:false, p00:SN};
On a side note, depending on the entry direction, the "switched" variable would be changed to true to simulate real life tracks. When the car comes from one direction it actually pushes the track to the side. Another car would have to come from a different entry direction to push it back.
In case you can't tell what it means, let me explain by showing you another small example like the above:
Code:
paths[ww].px = {canSwitch:false, pyz:WE};
Where:- ww - tile number
- x - path value (direction car is traveling)
- y - canSwitch variable's value (0 false, 1 true)
- z - switched variable's value (0 false, 1 true)
I might use a syntax like the following in the end to get the tween info:
Code:
var tileNum = map[car.x][car.y];
var tileObj = paths[tileNum];
var pathObj = tileObj["e"+car.d];
var tweenObj = pathObj["p"+tileObj.switched+""+pathObj.canSwitch];
From here, I thought it was as simple as implementing TweenMax to do the dirty work, and just feed it my bezier tween anchor and control points from each tile. I created a test file, imported TweenMax, and started playing around. (My first thought was how %&@*#&% polished that bad boy is. If anyone hasn't tried it, USE IT.) Unfortunately for me, what I wanted to do was tween the car from the start of one tile to the start of the next, then calculate the next tween in the same frame and start the next tween (in calculating, it would decide from the above data what path to take "out" of a tile if it can switch). But damnit, no matter what I tried, there is an extremely small pause right at the end of the tween before the second one starts. I did a lot of research and have sadly come to the conclusion that I'm not going to be able to "fake" my way out of this and I have to do actual math (trig + physics) to simulate how the car might make those 90 degree turns, staying equidistant from the control point (the "corner" of the tile corresponding to the inside "corner" of the turn). To make things worse, TweenMax automatically oriented the MC's rotation accordingly based on the tween path, which I thought would be a treat. But now I also have to find a way to do that.
If you're still reading at this point, major Kudos to you. The problem boils down to using this engine (the data specifically, I'm proud of it ) and expanding it to use a given x/y velocity for the car and use trig to move the car. Anyone care to point me in the right direction?
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|