Coding Challenge 172: Horizontal Directional Drilling - YouTube

Channel: The Coding Train

[0]
hello welcome to a coding challenge i am
[2]
just thrilled to be here i have had a
[4]
wonderful invitation from my friend
[6]
grady over at practical engineering to
[9]
work on a simulation of horizontal
[12]
directional drilling now if you had
[13]
asked me a year ago hey are you ever
[16]
going to do a coding challenge about
[17]
horizontal directional drilling would
[19]
say like what's that and probably not
[21]
that doesn't sound like a thing i would
[22]
do but after some research after being
[25]
taught about this from grady i am just
[28]
fascinated and i think there is
[30]
potential to create a fun playable 2d
[34]
casual game where you are
[36]
the horizontal directional driller so
[38]
what is horizontal directional drilling
[40]
well you don't need to listen to me
[41]
explain to it i've linked to practical
[43]
engineering's video on the topic it's
[45]
very well researched gives you lots of
[47]
context and backstory as well as many
[50]
amazing demonstrations of doing it on
[52]
big and small scales
[54]
grady was also kind enough to send me
[55]
this diagram which sort of shows a
[57]
scenario of how it might play out with a
[59]
whole lot of suggestions for how this
[61]
could be turned into a game from
[62]
practice mode hard mode and other ideas
[65]
i'm going to try to take all of that
[68]
and synthesize it down into the simplest
[70]
case scenario diagram it here on the
[72]
whiteboard and then code it up
[78]
all right my artistic skills aside here
[80]
is the scenario we're dealing with in an
[82]
overly simplified manner especially
[84]
given the fact that this is completely
[86]
two-dimensional we need to run some pipe
[88]
from point a
[90]
to point b
[92]
if there were no river we could just
[93]
excavate drop the pipe in and back fill
[96]
it in but this river is here and also
[99]
maybe there's some other restrictions
[100]
that we can't dig up all the land so
[102]
could we sort of thread the needle so to
[105]
speak how could we just thread a pipe in
[106]
and curve it around and steer it that's
[108]
what horizontal directional drilling
[110]
does now it involves all sorts of real
[112]
world considerations drilling fluid and
[115]
materials i'm not going to concern
[116]
myself with any of that i'm just going
[118]
to look at the mechanic of how the
[120]
controller steers the drill and create a
[123]
simulation mini game out of that now
[126]
again i have very little real world
[127]
experience with this but a regular drill
[130]
you know the drill bit might just look
[131]
like this
[133]
obviously with some ridges and edges and
[135]
markings and stuff to make it you know
[137]
cut into things for horizontal
[139]
directional drilling also sometimes
[140]
called directional boring the drill bit
[142]
itself will usually have something askew
[144]
at the end we can think of it as like oh
[146]
it comes off and it kind of points down
[149]
this is what gives it a bias
[151]
so as it's going into the ground and
[154]
drilling this bias will cause it to
[157]
curve down
[159]
so that the drill as it's inserted
[162]
it could start curving down in a 3d
[164]
world this bias might be something that
[166]
we fully rotate but here in our 2d
[168]
scenario i'm just going to think of it
[169]
as either pointing down or pointing up
[171]
so pointing down for negative one and we
[173]
could flip it to pointing up
[176]
if i flip it pointing up it's going to
[178]
start to curve the other way
[180]
the drill is always turning there's no
[183]
way for it to go straight so the
[185]
challenge is if i have a drill that's
[187]
always turning either up or down left or
[189]
right on or off plus one minus one how
[192]
can i effectively steer it through a
[194]
complex path there could be all sorts of
[196]
other stuff in the way here there could
[197]
be other lines we don't want to
[198]
interrupt strange rock formations i
[200]
don't know what's underneath the earth
[201]
and what engineers really deal with but
[203]
we've got to be able to get some kind of
[205]
control over steering this i think we've
[207]
got what we need though for me to be
[208]
able to get started coding here's the
[211]
base code i'm going to start with now if
[212]
for some reason this is the first time
[214]
you're watching the coding train hello
[215]
welcome
[218]
this is p5.js a javascript framework for
[221]
creative coding i use it in almost all
[223]
of my videos if you've never coded
[225]
before i do happen to have a complete
[227]
intro beginner series there will be
[229]
things in this video that you don't
[230]
understand if you haven't coded before
[232]
but i'll try my best to walk you through
[234]
it and then you can always refer back to
[235]
some of my intro videos to fill in the
[237]
gaps when i'm starting with the same
[239]
scene here a canvas with some ground and
[242]
a river and my drill is going to start
[245]
somewhere around here on the left i'm
[249]
going to use p5 vectors as the main data
[252]
structure to keep track of where is the
[254]
drill and what direction is it moving
[257]
so first thing first a position variable
[260]
for the drill
[265]
and i've put a little dot in the canvas
[266]
for where the drill is going to begin i
[269]
have the drill position what else do i
[271]
need well i need a variable to keep
[273]
track of the drill direction like what
[276]
direction is it currently drilling in as
[279]
well as the bias is the bias of the
[281]
drill bit pointing up or pointing down
[284]
this is again a physical thing that's
[286]
either flipped up or flipped down plus
[288]
one or negative one
[292]
dir for direction
[294]
bias for bias and i'm going to
[295]
initialize the bias as negative one
[298]
and the direction as a vector
[300]
pointing horizontally
[302]
straight i mean i guess we could start
[304]
maybe the drill should start pointing
[305]
down i don't know what would be typical
[308]
you have to go you can't go in the earth
[310]
this way so yeah let's have it pointing
[311]
down
[313]
so the simplest thing i could do right
[315]
now would be to write a function call it
[316]
drill
[318]
and then in that function i could add
[320]
the direction to the position
[323]
and if i were to then call drill here in
[326]
draw which happens over and over again
[328]
you can see the drill is moving down so
[330]
obviously i need to add this idea of the
[332]
bias and being able to steer it around
[334]
but before i even do that i'd like to
[337]
draw the full path i think that will
[339]
really help us in terms of visualizing
[341]
the process here
[344]
i'm going to use an array and every time
[346]
i move the position right before i move
[349]
it let's make a copy of where it was
[351]
that's essentially the previous position
[354]
it's very important that i make a copy
[356]
because these are objects if i just add
[359]
the object directly without making a
[360]
copy i'm adding a reference to it and
[362]
then i just have many many many many
[364]
references to the same point so when
[366]
that point moves they all move then in
[368]
draw i can use begin shape and end shape
[370]
to connect all the previous points with
[372]
a line
[376]
loop through everything in the path
[380]
and you can now see this black line
[382]
trailing behind the red dot now instead
[384]
of drawing that red dot i'd like to
[386]
visualize the drill bit so we can see
[388]
what's going on in which direction the
[390]
bias is pointing so let me first just
[392]
draw it as a line pointing down
[396]
and now i want to rotate it a skew up or
[398]
down
[400]
up i don't know which way is what but to
[402]
rotate this line i need to make use of
[404]
transformation another topic that you
[406]
may or may not be familiar with but
[408]
luckily for you i have a whole set of
[409]
videos about transformations in p5
[411]
transformations come up any time you
[413]
need to rotate something so i want to
[414]
rotate by let's say pi divided by six
[418]
it's like 30 degrees times the bias so
[421]
positive or negative direction of course
[422]
you can see now that's way in the wrong
[424]
place this is where i need to translate
[427]
to the point of origin to have the
[428]
rotation work correctly
[434]
and there you go so let's start it
[435]
actually at entering in around 30
[437]
degrees
[439]
i could create a vector from an angle
[442]
interesting hmm
[444]
let's try flipping the bias oh let's add
[447]
a button for that
[451]
so what this bit of code does is it
[453]
creates a button attaches a mouse
[455]
pressed event to it and whenever the
[457]
mouse is pressed that bias is multiplied
[459]
by negative one making it positive or
[461]
negative or vice versa but look at that
[464]
the drill bit is not pointing in the
[466]
direction that the drill is actually
[467]
going it's always just flipping left or
[469]
right from pointing down so we need to
[471]
also incorporate the heading of the
[473]
drill bit itself into where the bias is
[475]
pointing
[477]
i've made an error here where i
[479]
originally drew the drill bit pointing
[481]
down but all of the rotation and angles
[483]
are relative to the horizontal axis so
[485]
for all this to work nicely i should be
[487]
drawing the drill bit pointing to the
[489]
right
[490]
i also made another error which is that
[492]
the heading is the heading
[494]
the bias is affecting whether i'm
[496]
rotating to up or down based on that
[498]
heading so i want to multiply the bias
[500]
only by that offset which is pi divided
[503]
by 6. there we go now we can see the
[506]
drill bit
[508]
toggling between pointing up down up
[510]
down up down up now no matter what angle
[514]
we start with for example 90 degrees
[517]
it's still going to work
[519]
i can even start from zero degrees
[522]
and you can see i'm still able to toggle
[524]
and visually everything's looking right
[526]
i'm actually doing this live on twitch
[527]
and the chat is asking a great question
[529]
should we be using push and pop here and
[532]
i don't need to because i'm not drawing
[534]
anything else after drawing the drill
[536]
bit but it would be really good to
[537]
protect myself by using it this
[540]
translate and rotate will affect
[542]
anything that comes afterwards that
[543]
drill function don't let that mislead
[545]
you that's not drawing anything but one
[547]
i might say logically it might make more
[549]
sense to put drill at the beginning
[551]
and then put a push and pop around here
[556]
all right i kind of went about this in a
[557]
strange order because i really was
[559]
working on just visualizing all these
[560]
elements the key here is actually taking
[562]
the bias and having it affect the
[564]
direction to which the drill turns
[566]
remember it's always turning it's like a
[568]
shark it's always got to turn it can't
[570]
go straight that's not what a shark does
[571]
the shark always keeps moving you get
[572]
the idea though this begs the question
[574]
how much does the drill actually turn
[576]
now i'm sure again there are real world
[577]
values for all of these things i'm going
[579]
to make up a value
[581]
i'm going to call it angle .01 a small
[584]
incremental change and i'm going to say
[586]
direction rotate by that angle times the
[590]
bias oop
[592]
my drill it's taking off to space i have
[595]
defied all the laws of physics it's
[597]
amazing let's have the bias pointing
[599]
down to start and now here we go
[602]
i'm gonna try to toggle that bias
[605]
curve back
[607]
i gotta get to the other side
[609]
[Music]
[612]
oh no oh no don't hit the water
[615]
now you can see if i just keep toggling
[616]
it over and over again i'm that's
[618]
essentially how i'm getting to go in a
[619]
straight line and can i make it to the
[621]
other side
[623]
come on keep going
[625]
[Music]
[627]
we did it horizontal directional
[630]
drilling coding challenge complete so
[633]
what's next here certainly i shouldn't
[634]
be allowing the drill to sort of orbit
[636]
in this strange way i need to add
[638]
collision detection to see if it hits
[640]
water maybe the scene should be
[642]
procedurally generated in a different
[644]
every time i could have an easy mode and
[646]
a hard mode if you're actually doing
[648]
this in real life you don't see
[650]
everything underground and have a
[652]
complete view of the entire world of
[653]
what you're doing so i would encourage
[655]
you to take this as a code base and see
[658]
how you can expand it share it with me
[660]
on the coding train website there's a
[661]
form you can submit links to your
[663]
versions of horizontal directional
[664]
drilling but i also have made hopefully
[667]
not right now but by the time you're
[668]
watching this a fully playable version
[670]
of this game
[671]
that code will be on github i've made a
[674]
list of all sorts of new features and
[676]
ideas that could be added to this by the
[678]
time you're watching this a lot of those
[679]
will be implemented maybe there'll be
[680]
some new ones so come check out play the
[683]
demo enjoy it contribute to this make
[685]
your own version share it with me and
[688]
see you next time on the coding train
[692]
[Music]