Physics Racing Game – Laps (Part 2)

Hey there! In this post, we’re gonna continue working on our physics-based racing game. Make sure you’ve read Part 1 before you start this!

In this part, we’ll be implementing laps.


Laps


Create

We’ll implement this in oCar. Let’s begin my initialize two variables in its Create event:

//Track
trackPos = 0;
trackLap = 0;

trackPos is the position of the car on the path (from 0 (start) to 1 (end)) and trackLap is the number of laps the car is completed.

Step

Now we’ll implement the lap detection in its Step event. The way it’ll work is this: trackPos will store the position of the car on the path, starting at 0. Each time the car touches the line of the next point on the path, trackPos increases. If it reaches 1, it gets set to 0, and trackLap gets increased by 1.

So we’ll start by adding this in Step:

//Vars
var levelPath = oMain.levelPath;
var pathWidth = oMain.pathWidth;
var pathN = path_get_number(levelPath);

//Get next two points on the track
var nextP = trackPos+(1/pathN);
var nextNextP = nextP+(1/pathN);

var nextX = path_get_x(levelPath, nextP mod 1);
var nextY = path_get_y(levelPath, nextP mod 1);
var nextNextX = path_get_x(levelPath, nextNextP mod 1);
var nextNextY = path_get_y(levelPath, nextNextP mod 1);

The //Vars section will set up some variables. First it’ll get the levelPath and pathWidth variables from the object oMain, and create local variables for them, so that those values could be used in this event. pathN will store the total number of points in the level path.

The next section will get the coordinates of the next two points on the track, coming after the car’s track position (trackPos). nextP will get the next point, and nextNextP will get the point subsequent to the next point. And so, nextX & nextY will get the coordinates of nextP; nextNextX & nextNextY the coordinates of nextNextP.

I am applying “mod 1” to the point values so that if any value reaches 1, it goes back to 0.

Next we’ll add this:

//Get the direction from the next point towards its succeeding point
var pathDir = point_direction(nextX, nextY, nextNextX, nextNextY);

//Calculate the line coordinates
nextX1 = nextX + lengthdir_x(pathWidth, pathDir-90);
nextY1 = nextY + lengthdir_y(pathWidth, pathDir-90);
nextX2 = nextX + lengthdir_x(pathWidth, pathDir+90);
nextY2 = nextY + lengthdir_y(pathWidth, pathDir+90);

pathDir will get the direction from the nextP coordinates towards the nextNextP coordinates.

Using that direction, the line coordinates of nextP will be calculated, where nextX1, nextY1 will be the coordinates of one point on the line, and nextX2, nextY2 will be the coordinates of the other point on the line. This is the line we’ll use to check if the player has reached the next point on the path.

So, now we’ll do just that:

//Check if the car is at that line
if (collision_line(nextX1, nextY1, nextX2, nextY2, id, 0, 0)){
    //Increase position
    trackPos += (1/pathN);
   
    //Lap complete
    if (trackPos >= 1){
        trackPos = 0;
        trackLap++;
    }
}
We’ll use those line coordinates in collision_line to check for a collision with id, which is the car itself.
If a collision is found, trackPos will be increased by 1/pathN, so that it proceeds to the next point on the 0-1 line.
If trackPos reaches 1, it’ll be set to 0, and trackLap will be increased by 1.
Now I’ll add the Draw GUI event. Inside it, I’ll add this:
draw_text(8, 16, "Pos: " + string(oCar.trackPos) + "\nLap: " + string(oCar.trackLap));
So it’ll draw the trackPos and trackLap variables to the screen. If you are using GM:S 1.4, replace the \n” part with “#”, because that’s what 1.4 uses for a new line.
Now if you run the game, you can run the car on the track and see its position increase. When you go all the way around, the lap count increases:
0.gif
If you remove the var keyword before initializing the nextX1, nextY1, nextX2, and nextY2 variables so that they last through events, and use the draw_line() function in the Draw event to draw it, you can see how it works:
draw_set_color(c_red);
draw_line(nextX1, nextY1, nextX2, nextY2);
draw_set_color(-1);
1.gif

Conclusion


You can download the project here (GMS2)

The next part will cover adding AI bots to the game. Make sure you follow this blog (just click Follow at the bottom of this page) to receive an email when I post it!

follow.gif

If you need free GML help or want to provide GML help to those who need it, join our Discord server! We have a fun little community there (and a fun bot); it’ll be good to have you too!

See ya later, and till then, happy dev’ing!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s