How to Create a Roller Coaster Game in HTML5

In this tutorial we are going to implement an HTM5 roller coaster game, based on an older roller coaster tutorial I’ve written in as3, a long time long time ago, when flash was alive as dinosaurs were sometimes. We are going use the createjs library and to implement the game from scratch in typescript which is transpilled in javascript. I decided to publish this tutorial because I want to use as a base for an educational game I’m going to create for roller coaster games, in which to study the movement of a roller coaster cart along a pathway.

Here is an interactive version of the html5 roller coaster game we are going to have at the end of this tutorial:

The tutorial is divided 3 parts:

  • Drawing Segments That Defines The Roller Coaster Rail – In the first part we are going to create the segment on which the track of the roller coaster is based.
  • Generating Rail Points Using Bezier Curves – In this part we are using Bezier curves to draw the rail and to determine the trajectory points, points which are useful both for drawing the trail and to move the cart along it.
  • Implementing Roller Coaster Cart Physics – In the last part we are going to write the code which animate the roller coaster along the curve.

 

1. Drawing Rail Segments

In the first part the code does not have much to do with a roller coaster game. Not even with a game. We are implementing to code to draw segments of a predefined length when we press the mouse button and keep it pressed while we move the mouse. We begin by creating a class and adding the three methods to handle the mouse down, mouse move and mouse up events.

All the events are performed on the stage object( which in createjs is the main object where all the other graphical objects are added ). We are also adding another object where we are doing all the drawing operations:

In addition to what we have until now, we need to add a few member variables:

  • SEGMENT_LENGTH – represents the length of a segment. Once the distance between the previous point and the current mouse position exceeds 60 pixels we add a new point to our list
  • segmentPoints – an array which holds the points we are adding
  • currentPoint – this is the last point which was added in the segmentPoints array. We could retrieve it using segmentPoints[ segmentPoints.length – 1 ], but it we declare it as a member to make the code more readable and less verbose.
  • drawing – a boolean which is true if we are in the process of drawing segments, otherwise is false. Initially is false, when we press the mouse button it gets true, when the mouse button is up it turns to false. In short the drawing is true as long as the mouse button is down; the code in mouseMove will execute only when the drawing is true.

Now let’s start implementing the mouse down event:

The code reset everything: it empties the segmentPoints array, sets the drawing true and clear the drawing area; it takes the coordinates of the current point and it adds it to the segment points as the first point. Then it draws a horizontal line to be a reference line to easily see how high is the initial point.

In mouseMove method, we read the current mouse position and if the distance to the previous one is higher than SEGMENT_LENGTH we add the current point to the segmentPoints array and draw the new segment in the drawingShape:

In the last step, we add and draw the last segment, when the mouse is up:

2. Generate Roller Coaster Rail Using Bezier Curves

In the previous step we wrote code which allows us to draw the segments. Of course a roller coaster game would not look too natural if the roller coaster goes on trails with such sharp angles. We need to come with an algorithm to generate the rail based on the segments we just draw in the previous step. The Bezier method to generate curves is the one which fits very well our requirements.

When are are implementing the bezier curve, what we obtain is not really a curve. It’s a set of tiny segments which our eye perceive as a curve, but in reality it’s just an approximation of a curve. When we are moving the rail on the curve, we are really moving it along those tiny segments. The segments are small enough, so we perceive the movement as continuous along a curve.

First of all we implement an new class to hold the position of the existing point and the distance and angle to the next point. Those are required especially for moving part, to make sure we are not calculating them ever and ever again each time the cart is at a specific location:

Now we are going to create a class, BezierAssist to help us to generate the bezier points. Bezier curves can be generated to create intermediary points for 2,3 or 4 points. Bezier assist is based on the following method which checks if we want to generate the bezier segments for 2,3 or 4 points:

We also implement the linearBezierPoint, quadraticBezierPoint and cubicBezierPoint( actually we can skip the last one, because the quadratic curve is enough ). The linearBezierPoint does not create a curve, we use it to split a segment in smaller segments. Each call to the function returns us a specific point, for the progress specified by t. T is between 0 and 1, for 0 we are getting the same coordinated as the first point, 1 we are getting the same coordinates as the second point, for 0.5 we get exactly the middle.  If we need to get 10 points we call the method 10 times with 0.1 intervals.

The following method is similar to the previous one, to create the curve segments for 3 points by running it for each small segment. Here is the implementation of the method:

 

The quadratic method is used to generate the intermediate points. Based on the parameter t, and the point [P0, P1, P2] we get the point B.

Of course we need to run quadraticBezierPoint repeatedly, depending on how many segments we want to generate. We are going to divide 1 by the number of segments we want to obtain and to run it step by step for to calculate intermediate each point. The bezier method in BezierAssist does exactly what this animation does:

It takes every 3 points we draw and it creates intermediate bezier points invoking the previous defined methods:

Now, at this stage we need to add the bezier functionality in the main class. First we add the following adday to hold the bezier points:

Next we add  the a method to generate bezier points for every segment we draw and to draw the line:

The we call the bezier when the mouse is up:

Implementing the roller coaster physics

Until now we have the trail, now we need to make the rollercoaster to move on the trail. The fact that we already have the bezier points will help us a lot. The Bezier points are not used only for drawing but also to move the roller coaster; if you remember, each bezier point(TrajectoryPoint) holds not only the coordinates, but also the distance to the next one and the angle.

Now we are adding the following member variables:

Those are the following ones:

  • moving – is a variable which is true when the chart is moving. It’s the opposite of drawing, with the only difference that moving is false if the track we draw is not valid.
  • FRICTION and GRAVITY are to parameters which don’t need much explanation. We can change them to see how they influence the move.
  • cart – is the variable which holds the graphics object. We don’t use fancy graphics for our roller coaster as of now, we use a simple rectangle. The roller coaster cart also has 2 member variables:
    • line – holds the index of the current bezier point
    • speed – the current speed of the cart

In the constructor we add a new event which gets triggered every tick:

When the mouse is up we add the Cart to the scene and if the bezier has enough points we set moving variable to true:

In onTick if moving variable is true, we get the current trajectory point, adjust the speed parameter, update the card positon by running the run method and update the cart rotation:

The last method is the one which calculate the new position of the roller coaster:

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *