[TOOTING TRAIN WHISTLE]
Hello.
Welcome to a coding challenge.
I am just thrilled to be here.
I have had a wonderful invitation
from my friend Grady over at Practical Engineering
to work on a simulation of horizontal directional
drilling.
Now, if you had asked me, a year ago, hey,
are you ever going to do a coding
challenge about horizontal directional drilling,
I would have said, like, what's that?
And probably not.
That doesn't sound like a thing I would do.
But after some research, after being
taught about this from Grady, I am just fascinated
and I think there is potential to create a fun, playable, 2D,
casual game where you are the horizontal directional driller.
So what is horizontal directional drilling?
Well, you don't need to listen to me explain it.
I've linked to Practical Engineering's video
on the topic.
It's very well researched, gives you lots of context and back
story as well as many amazing demonstrations of doing it
on big and small scales.
Grady was also kind enough to send me
this diagram, which sort of shows a scenario of how
it might play out with a whole lot of suggestions for how this
could be turned into a game, from practice mode, hard mode,
and other ideas.
I'm going to try to take all of that
and synthesize it down into the simplest-case scenario,
diagram it here on the whiteboard,
and then code it up.
All right, my artistic skills aside,
here is the scenario we're dealing
with In an overly simplified manner, especially given
the fact that this is completely two-dimensional,
we need to run some pipe from point A
to point B. If there were no river,
we could just excavate, drop the pipe in, and backfill it in,
but this river is here and also maybe there's
some other restrictions that we can't dig up all of the land.
So could we sort thread the needle, so to speak?
How could we just thread a pipe in, and curve it around,
and steer it.
That's what horizontal directional drilling does.
Now, it involves all sorts of real-world considerations,
drilling fluid, and materials.
I'm not going to concern myself with any of that.
I'm just going to look at the mechanic of how
the controller steers the drill and create a simulation mini
game out of that.
Now, again, I have very little real-world experience
with this, but a regular drill, the drill bit
might just look like this, obviously
with some ridges and edges and markings and stuff
to make it cut into things.
For horizontal directional drilling,
also sometimes called directional boring,
the drill bit itself will usually have
something askew at the end.
We can think of it as like, oh, it comes off
and it kind of points down.
This is what gives it a bias.
So as it's going into the ground and drilling,
this bias will cause it to curve down so
that the drill, as it's inserted,
it could start curving down.
In a 3D world, this bias might be something
that we fully rotate, but here, in our 2D scenario,
I'm just going to think of it as either pointing
down or pointing up.
So pointing down for negative 1.
And we could flip it to pointing up.
If I flip it pointing up, it's going
to start to curve the other way.
The drill is always turning.
There's no way for it to go straight.
So the challenge is, if I have a drill that's always
turning either up or down, left or right, on or off,
plus 1, minus 1, how can I effectively steer it
through a complex path?
There could be all sorts of other stuff in the way here.
There could be other lines we don't
want to interrupt, strange rock formations.
I don't know what's underneath the earth
and what engineers really deal with,
but we've got to be able to get some kind of control
over steering this.
I think we've got what we need, though,
for me to be able to get started coding.
Here's the base code I'm going to start with.
Now, if, for some reason, this is the first time
you're watching the Coding Train, hello, welcome.
[TOOTING TRAIN WHISTLE]
This is p5.js, a JavaScript framework for creative coding.
I use it in almost all of my videos.
If you've never coded before, I do
happen to have a complete intro beginner series.
There will be things in this video
that you don't understand if you haven't coded before,
but I'll try my best to walk you through it,
and then you can always refer back to some of my intro videos
to fill in the gaps.
But I'm starting with the same scene here--
a canvas with some ground, and a river, and my drill
is going to start somewhere around here on the left.
I'm going to use p5 vectors as the main data structure
to keep track of where is the drill
and what direction is it moving.
So first thing first, a position variable for the drill.
And I've put a little dot in the canvas for where
the drill is going to begin.
I have the drill position.
What else do I need?
Well, I need a variable to keep track of the drill direction,
like what direction is it currently drilling in,
as well as the bias.
Is the bias of the drill bit, pointing up or pointing down?
This is, again, a physical thing that's either flipped
up or flipped down, plus 1 or negative 1.
dir for direction, bias for bias.
And I'm going to initialize the bias as negative 1
and the direction as a vector pointing horizontally straight.
I mean, I guess we could start-- maybe the drill should
start pointing down.
I don't know.
What would be typical?
You can't go in the earth this way.
So yeah, let's have it pointing down.
So the simplest thing I could do right now
would be to write a function, call it drill,
and then, in that function, I could add
the direction to the position.
And if I were to then call drill, here in draw,
which happens over and over again,
you can see the drill is moving down.
So obviously I need to add this idea of the bias
and being able to steer it around.
But before I even do that, I'd like to draw the full path.
I think that will really help us in terms of visualizing
the process here.
I'm going to use an array.
And every time I move the position,
right before I move it, let's make a copy of where it was.
That's, essentially, the previous position.
It's very important that I make a copy,
because these are objects.
If I just add the object directly without making a copy,
I'm adding a reference to it.
And then I just have many, many, many, many references
to the same point.
So when that point moves, they all move.
Then, in draw, I can use begin shape and end shape
to connect all the previous points with a line.
Loop through everything in the path.
And you can now see this black line
trailing behind the red dot.
Now, instead of drawing that red dot,
I'd like to visualize the drill bit
so we can see what's going on and which direction the bias
is pointing.
So let me first just draw it as a line pointing down.
And now I want to rotate it askew, up or down, or up--
I don't know which way is what.
But to rotate this line, I need to make use of transformation,
another topic that you may or may not be familiar with,
but luckily for you, I have a whole set of videos
about transformations in p5.
Transformations come up any time you need to rotate something.
So I want to rotate by, let's say, pi divided by 6--
it's like 30 degrees--
times the bias, so positive or negative direction.
Of course, you can see, now, that's way in the wrong place.
This is where I need to translate
to the point of origin to have the rotation work correctly.
And there you go.
So let's start it, actually, at entering in around 30 degrees.
I can create a vector from an angle--
interesting.
Hmm.
Let's try flipping the bias.
And oh, let's add a button for that.
So what this bit of code does is it creates a button,
attaches a mousePressed event to it,
and whenever the mouse is pressed,
that bias is multiplied by negative 1, making it positive,
or negative, or vice versa.
But look at that.
The drill bit is not pointing in the direction
that the drill is actually going.
It's always just flipping left or right from pointing down.
So we need to also incorporate the heading
of the drill bit itself into where the bias is pointing.
I've made an error here where I originally
drew the drill bit pointing down,
but all of the rotation and angles
are relative to the horizontal axis.
So for all this to work nicely, I
should be drawing the drill bit pointing to the right.
I also made another error, which is that the heading is
the heading.
The bias is affecting whether I'm rotating up or down based
on that heading.
So I want to multiply the bias only by that offset,
which is pi divided by 6.
There we go.
Now we can see the drill bit toggling between pointing up,
down, up, down, up, down, up.
Now, no matter what angle we start with-- for example,
90 degrees--
it's still going to work.
I can even start from 0 degrees.
And you can see I'm still able to toggle.
And visually, everything's looking right.
I'm actually doing this live on Twitch.
And the chat is asking a great question.
Should we be using push and pop here?
And I don't need to because I'm not
drawing anything else after drawing the drill bit,
but it would be really good to protect myself by using it.
This translate and rotate will affect
anything that comes afterwards.
That drill function, don't let that mislead you.
That's not drawing anything.
But one, I might say, logically, it
might make more sense to put drill at the beginning
and then put a push and pop around here.
All right, I kind of went about this in a strange order
because I really was working on just
visualizing all these elements.
The key here is actually taking the bias
and having it affect the direction
to which the drill turns.
Remember, it's always turning.
It's like a shark.
It's always got to turn.
It can't go straight.
That's not what a shark does.
The shark always keeps moving.
You get the idea though.
This begs the question, how much does the drill actually turn?
Now, I'm sure, again, there are real-world values
for all of these things.
I'm going to make up a value.
I'm going to call it angle 0.01, a small, incremental change.
And I'm going to say direction rotate
by that angle times the bias.
Oop, my drill, it's taking off to space.
I have defied all of the laws of physics.
It's amazing.
Let's have the bias pointing down to start.
And now here we go.
I'm going to try to toggle that bias.
Curve back.
I got to get to the other side.
[STIRRING MUSIC]
Oh no.
Oh no, don't hit the water.
Now, you can see, if I just keep toggling it, over and over
again, that's, essentially, how I'm getting
to go in a straight line.
And can I make it to the other side?
Come on.
Keep going.
[TRUMPET FANFARE]
We did it-- horizontal directional drilling.
[BELL DINGS]
Coding challenge complete.
So what's next here?
Certainly, I shouldn't be allowing the drill to sort
of orbit in this strange way.
I need to add collision detection
to see if it hits water.
Maybe the scene should be procedurally generated
different every time.
I could have an easy mode and a hard mode.
If you're actually doing this in real life,
you don't see everything underground
and have a complete view of the entire world of what
you're doing.
So I would encourage you to take this as a code base
and see how you can expand it, share it with me on the Coding
Train website.
There's a form you can submit links
to your versions of horizontal directional drilling.
But I also have made--
hopefully, not right now, but by the time
you're watching this-- a fully playable version of this game.
That code will be on GitHub.
I've made a list of all sorts of new features and ideas
that could be added to this.
By the time you're watching this, a lot of those
will be implemented.
Maybe there'll be some new ones.
So come, check out, play the demo,
enjoy it, contribute to this, make your own version,
share it with me.
And see you next time on the Coding Train.
[TOOTING TRAIN WHISTLE]
[MUSIC PLAYING]