Essential Guide To Flash Games Code Supplement #2: Chapter 10 Driving and Turning Supplement

Essential Guide To Flash Games Code Supplement #2: Chapter 10 Driving and Turning Supplement

In chapter 10 we build an engine to handle an unlimited sized scrolling world with 32×32 sized tiles. On top of this scrolling engine we build a driving game. I had never completed a 360 degree driving game before this but as I wanted to do something “new” for the book I took on the challenge. Given the short time frame needed to build the engine, the full game and then the 60+ page chapter on the subject (in two weeks) I had to leave a few things out of the finished product. I will be exploring adding them in this an future installments. Let’s fix something first

Now, the point of the chapter was really to create the scrolling engine and provide tile-base collision detection routines in this type of fluid open environment. I didn’t focus too much on the realism of the car movement as I am a B minus Physics student at best. But, I do want it to be at least moderately accurate, so there is at least one fix to the current version that I would like to present.

While creating this game I wanted to provide somewhat realistic driving controls to ensure that it at least simulated how a car might drive in the real world. One thing I did was I add in code that changed the amount of rotation of the car when turning based on the velocity of the car. In the book I make the car turn in a linear relationship with its speed in a positive proportional manner. In a less masturbatory wording, the car turns at a sharper angle the faster it is going. I finished up the game, wrote (and re-wrote) the chapter (3 times). It went to the publisher and was printed in the book. Two weeks ago I started a re-skin / updated version to support the book virally. I let Steve play this version and he said, “hmm, your car drives like a tank”. At first I thought he was full of crap, but yes, he was right. Cars can turn at a sharper angle when moving slower than they can when moving faster. I had my turning speed code implemented backward!

So, to fix this, I have changed the code.

Here is the code as it currently exists in the file’s update() function

<font color="blue" size="2"><br /><font size="3">if (player.velocity == 0) {<br />    player.turnSpeed = 0;<br />}else {<br />    player.turnSpeed = player.minTurnSpeed + (Math.abs(player.velocity/10));<br />    if (player.turnSpeed &gt; player.maxTurnSpeed) {<br />        player.turnSpeed = player.maxTurnSpeed;<br />    }<br />}</font><br /></font><br />

Here is the modified code that will make a more realistic driving and turning experience.

<font color="blue" size="2"><br /><font size="3">if (player.velocity == 0) {<br />    player.turnSpeed = 0;<br />}else {<br />    player.turnSpeed = player.maxTurnSpeed - (Math.abs(player.velocity/10));<br />    if (player.turnSpeed &gt; player.maxTurnSpeed) {<br />        player.turnSpeed = player.maxTurnSpeed;<br />    }else if (player.turnSpeed&lt; player.minTurnSpeed){<br />        player.turnSpeed = player.minTurnSpeed;<br />    }<br />}</font><br /></font><br />

That’s it. Hopefully this will; make the car drive and turn in a more realistic manner.

If you prefer a more realistic Driving and Cryin’ experience, I suggest Scarred By Smarter.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
This site is protected by Comment SPAM Wiper.