Placeholder Image

字幕列表 影片播放

  • (bell rings)

  • - Hello and welcome to a black hole coding challenge.

  • Time has really slowed down for me,

  • or maybe it's sped up.

  • I can't remember which is which because

  • I'm actually now recording this many days

  • after the actual livestreamed coding challenge.

  • In a little bit, I will change my clothes and

  • travel into the past through the black hole.

  • I don't know how it's all going to work,

  • but you will see me live coding,

  • writing the code to do this simulation.

  • In truth, it's less simulation than visualization.

  • On April 10th,

  • the first ever image of a black hole was published.

  • This image was put together by a team of scientists

  • known at the Event Horizon Telescope.

  • It was compiled from data from many telescopes

  • all over the Earth all synchronized.

  • I looked at this image and I thought,

  • it looks kind of fuzzy and glowy and

  • maybe there's some way I could reproduce

  • this image through a simulation.

  • I started to sort of dig into this.

  • I quickly realized I'm many, many steps away from doing that

  • and I wanted to find the place to start,

  • somewhere where I could at least begin to

  • simulate or visualize the behavior of

  • space time and black holes.

  • Here are the resources that I used to learn about this.

  • First, let me thank Veritasium's video,

  • the how to understand the image of a black hole.

  • This video was actually published

  • the day before the image was revealed,

  • which is really kind of amazing.

  • If you watch this video, the explanation is superb.

  • In particular, there's a short animation in the video

  • that depicted beams of light traveling

  • towards the black hole,

  • some disappearing into the black hole,

  • some wrapping around in an orbit.

  • That's my starting point where I was interested in.

  • I also learned quite a bit from Chris Orbin

  • and the STEM Coding YouTube channel.

  • STEM Coding, if you're not familiar

  • with that YouTube channel,

  • you should definitely check it out.

  • They have a lot of videos about physics and science

  • taught through the lens of coding and visualization.

  • In particular, they have a video called

  • Slingshot with Gravity and Chris Orbun

  • published an article explaining how that

  • code example could be tweaked a little bit

  • to kind of get at some of the ideas

  • around how gravity and black holes.

  • Finally, there's a wonderful paper from 1978.

  • Thank you to Veritasium also for this reference

  • called Image of a Spherical Black Hole

  • with Thin Accretion Disk.

  • This paper has diagrams and the mathematics

  • behind the photon trajectories around a black hole.

  • It gives you a lot of background into

  • what you would want to do to visualize a black hole.

  • Of course, I'm not the first one to try this.

  • Many people have made beautiful visualizations

  • and artistic renderings of black holes.

  • There's the one that you might remember

  • from the move Interstellar.

  • Kip Thorn, a Caltech physicist,

  • was actually an advisor on that film,

  • but there's a lot of artistic license there.

  • I also want to point out to you Ricardo Antonelli

  • who's written this wonderful article

  • How to Draw a Black Hole,

  • Geodesic Raytracing in Curved Space Time.

  • In the article he goes through step-by-step

  • a bunch of different computer graphic tricks and techniques

  • along with the sort of physics of black holes themselves

  • to create a 3D visual of what a black hole might look like.

  • If you've watched me before,

  • you know I am not a physicist, I'm not a scientist.

  • There are many caveats.

  • I don't play a physicist on YouTube.

  • I'm just here wanting to make something

  • in 2D Canvas JavaScript.

  • In fact, I already did it.

  • What I'm here right now,

  • let me talk to you about the pieces

  • that I want to put in this visualization

  • as a sort of reference point for when I start coding.

  • The black hole that I want to visualize is in the galaxy M87.

  • It previously didn't have a name.

  • It was just called M87 star, the star for black hole,

  • but it was recently named Powehi.

  • I'm not sure if I'm pronouncing that correctly,

  • but it is from a Hawaiian chant

  • and it means something like adorn, dark,

  • fathomless creation, something like that.

  • Very appropriate for a black hole.

  • This is what's known as a super massive black hole.

  • Not all black holes are super massive,

  • but this one is and its mass is equivalent to

  • 2.6 billion solar masses, or suns.

  • Take the sun, our sun,

  • the one up in the sky that shines on us,

  • and put together 2.6 billion of those

  • and you have a black hole.

  • It's so massive, we can't see it.

  • Why?

  • Because the gravitational pull is so strong,

  • there's so much matter in there,

  • that any light traveling towards it,

  • once it gets to a certain proximity, can no longer escape.

  • You couldn't be inside the black hole

  • and shine a flashlight.

  • You could be there, but outside the black hole,

  • you couldn't see it 'cause the light can't get out.

  • Of course, you couldn't also be there because

  • you would be dead, very, very dead in the black hole,

  • or you'd just be like Matthew McConaughey,

  • one or the other is true.

  • This little ring here,

  • this distance from the center of black hole

  • at which nothing can escape, not even light,

  • not even the fastest thing we know about light can escape,

  • is known as the event horizon.

  • There's actually a formula for calculating

  • the distance from the center of black hole

  • to that event horizon itself,

  • the Schwarzschild radius, or R sub S.

  • The Schwarzschild radius is calculated as two times G,

  • the universal gravitational constant, times M,

  • the mass of the black hole itself,

  • remember, 2.6 billion solar masses,

  • divided by C squared where C is the speed of light.

  • Of course, the event horizon isn't really a circle.

  • It's a sphere, but for us in our 2D simulation,

  • we're going to make it flat.

  • In order to actually calculate this,

  • I need some of these values.

  • I have the mass, I also need C, the speed of light,

  • which I'm looking over there, I don't have this memorized,

  • which is 299,792,458 meters per seconds squared.

  • That's very, very fast.

  • Not seconds squared.

  • I don't know why I put seconds squared there.

  • It's just meters per second.

  • This number, meters per second.

  • That's the speed of light.

  • I also need G, the universal gravitational constant,

  • which is 6.67 times 10 to the negative 11th power.

  • Now, with these values, with the mass of the black hole,

  • with the universal gravitational constant,

  • with the speed of light, you can actually calculate this.

  • I will leave that to you to calculate

  • and leave your answer in the comments,

  • or you could probably looks it up

  • because people are calculating this stuff all the time.

  • Another element that I want to include

  • in my visualization is the accretion disk.

  • The accretion disk is a whole lot of matter

  • that's outside of the event horizon

  • orbiting the black hole and sort of feeding the black hole.

  • This is a particularly active one.

  • Again, a black hole isn't emptiness.

  • It's we think of it as emptiness.

  • There's just so much matter there

  • that the light cannot escape so it's nothingness.

  • So crazy.

  • The accretion disk is this orbit that's

  • outside of matter orbiting.

  • It has a specific measurement where it is,

  • which is three times the Schwarzschild radius.

  • From the center, one, two, three.

  • You can see not drawn to scale,

  • but this is the accretion disk, three RS.

  • What about light?

  • The accretion disk is full of matter.

  • Matter has mass and it's orbiting at some speed

  • and we could calculate gravitational attraction

  • and all that stuff,

  • but light travels at the speed of light,

  • photons of light travel the speed of light, that speed,

  • and have the mass of zero.

  • It so happens that at 1.5, right around here,

  • at 1.5 times the Schwarzschild radius,

  • this is where light will orbit, but not in a stable orbit.

  • Any photons, any light that's orbiting there

  • will eventually either spiral out forever or spiral in

  • pass the event horizon,

  • never to be seen or heard from again.

  • These are the elements I want in my visualization.

  • I want to depict the black hole right here,

  • really the shadow of the black hole,

  • measured with the event horizon.

  • I want this photon ring,

  • but both the event horizon is really a sphere

  • and this photon ring is really a photon sphere,

  • this is the place where light is orbiting,

  • it's not a stable orbit.

  • It doesn't slow down so it's always

  • eventually going to shoot out or shoot in, but ultimately,

  • if you were standing there,

  • the light would actually draw a circle

  • and you would see the back of your head,

  • which is not what's happening to me right now

  • and I'm trying to think of that.

  • My mind doesn't work that way.

  • I want to depict that and then the accretion disk as well.

  • These are the elements that I want in my visualization.

  • However, I want to animate something.

  • I want to look at what happens when

  • light itself is traveling at the black hole.

  • What does it do?

  • We could be pretty sure that if I

  • take a beam of light, a photon,

  • and point it directly at the center of the black hole,

  • it's going to travel up to the black hole,

  • go past the event horizon, and never come back.

  • We're never going to hear from it again.

  • It sucked into the event horizon.

  • What happens if though there's a beam of light over here,

  • or over here, or over here?

  • What happens there?

  • We can actually see a diagram of this

  • in Ricardo Antonelli's tutorial.

  • This is really the core inspiration for

  • what it is that I want to create in p5.js.

  • What you see here is basically my visualization.

  • There's the black hole, there's the photon ring,

  • here are beams of light, they're bending around,

  • they sort of temporarily end up in that orbit,

  • and then spiral out or spiral in.

  • The thing is, are they really bending,

  • going in a curved path,

  • or are they simply traveling in a

  • straight line through the curve of space time?

  • This is where it would be useful to talk about relativity.

  • The theory of relativity will help to explain

  • why the space time bends around an object,

  • a super massive object.

  • You can think of it as space time being this trampoline

  • and there's a big bowling ball sitting in the center

  • and it's just weighing it down and the

  • space time around it starts to bend

  • and things start to fall in towards it.

  • That's ultimately what's happening with a black hole.

  • The theory of relativity is beyond the scope

  • of what I'm doing in this video.

  • I just want to look at is there a way

  • that I can create the effect.

  • What if I take these beams of light, photons,

  • one starting out pointing directly

  • at the center of the black hole and outwards,

  • how can I look at a way that I could simulate

  • how they would bend around, end up in the photon ring,

  • and then spiral in or spiral out?

  • If you saw the diagram that looks

  • something like this in Veritasium's video,

  • you will also see that there is a radius here,

  • 2.6 times the Schwarzschild radius,

  • there's a distance here from this photon

  • pointed directly at the center outwards

  • at which the light will spiral out

  • as opposed to spiraling in.

  • That is 2.6.

  • In theory, if I were to do all of my math correctly

  • in my visualization, which spoiler alert, I do not,

  • you would see this behavior exactly.

  • The reason why what you're going to watch does not do that

  • is I am simulating sort of this idea

  • with newtonian gravitational attraction.

  • I'm going to approximate the speed of light

  • and these photons will never go faster or never slow down.

  • I don't actually get to that 'til the very end.

  • I kind of fudge that for awhile,

  • but get it a little bit closer,

  • and just use raw gravitational attraction.

  • The coding challenge is about to begin.

  • I'm going to try to program exactly this.

  • You'll see where the code ends up at the end

  • and then I'll come back wearing this shirt again,

  • that's how you'll know how time changed,

  • and I'm going to talk to you a little bit about

  • some things that you might try to account for relativity,

  • to make the behavior of these photons more accurate

  • and I hope that you'll be inspired

  • to make your own visualization of a black hole

  • and share it with me.

  • Let's see, we need some variables.

  • Number one is we need the speed of light.

  • 299, 792, 458, there we go.

  • I don't need the commas because this is code.

  • Now we got the speed of light.

  • Excellent.

  • Things are going very well so far in my coding.

  • Now we need the universal gravitational constant.

  • G, which is 6.67 times power 10 to the negative 11.

  • What could go wrong there?

  • This seems exactly right.

  • Now I need to have my RS, my Schwarzschild radius,

  • which is two times G times.

  • The mass!

  • Now I need a constant.

  • M is the mass.

  • That was 6.5 billion, move two zeroes, some more zeroes,

  • some more zeros.

  • I don't know, how much is the mass of the sun?

  • Mass of the sun.

  • (man laughs)

  • You see the folly here!

  • One folly here is while these numbers

  • are incredibly meaningful and important,

  • they're not going to do me so much good here

  • in my p5.js web editor JavaScript program.

  • What I'm going to do is make up some numbers.

  • We're going to create our own two-dimensional universe

  • that has a speed of light,

  • that has a universal gravitational constant.

  • In fact, rather than put the mass of

  • the black hole right here,

  • I'm going to make a black hole class.

  • Let's just arbitrarily say do something approximating this.

  • The speed of light is 30 and the

  • universal gravitational constant is six, why not right?

  • Also go and make a black hole.js file.

  • Then I'm going to say class black hole and

  • I need a constructor, just an XY.

  • Let me use a P-vector for this.

  • I'm going to say this dot position equals create vector,

  • X comma Y.

  • Now I need a mass.

  • This dot mass equals 6.5.

  • Our measurements are in billions.

  • Then its radius, the Schwarzschild radius,

  • is two times G times this dot mass divided by C times C.

  • I need a little more space here

  • to be able to see more code,

  • but fortunately actually I can do this.

  • Let's put these in parentheses.

  • Now what I can do is I can write show,

  • and what am I going to do?

  • Let's make fill zero,

  • let's fill in the black hole with the color black,

  • let's draw a circle at this dot pos dot X,

  • this dot pos dot Y with the Schwarzschild radius times two.

  • I guess I should might as well pass in

  • an argument here for mass.

  • If I were to in my main program say, let's call it M87,

  • we're going to call our black hole M87,

  • M87 equals a new black hole.

  • I'm going to place it at sort of the top.

  • Actually, the left of my canvas.

  • Let's place it at 100 comma 320

  • and we're going to give it a mass of, what'd I say, 6.5,

  • and then I'm going to say M87 dot show.

  • We have a black hole.

  • RS is not defined.

  • This dot

  • This dot

  • This dot, this dot son

  • There we go, that fixes that.

  • Where is my black hole?

  • Let's take a look at what this dot RS is.

  • Way too tiny.

  • My units are really kind of all over the place.

  • Let's make some units that make more

  • sense for our pixel space.

  • Let's make the mass of the black hole

  • probably much bigger, 6,500.

  • There we go, look!

  • (bell rings)

  • Thank you!

  • (upbeat jazz music) Goodnight.

  • I have now made my simulation of a black hole.

  • This is the first ever known image

  • of a black hole made in p5.js.

  • Just because I don't have a lot of

  • pixel space to work with right now,

  • let's make the mass a tenth as large.

  • We can see, there's my much smaller black hole.

  • Okay fine.

  • This is looking good to me.

  • That's the size of the black hole that I want.

  • Let's now draw the accretion disk.

  • Let's make some sort of accretion disk.

  • I'm going to say no fill stroke and

  • let's make it kind of grayish.

  • I'm going to draw the accretion disk as a circle.

  • This dot pause dot X, this dot pause dot Y,

  • this dot RS times.

  • This just in from the chat, I forgot about this.

  • I forgot about this.

  • I can say ellipse mode radius,

  • which should now allow me to, let me comment this out,

  • just use the radius as the ellipse.

  • Perfect.

  • Just used the radius value as the size of the circle.

  • Thank you Simon Tiger for that.

  • Great correction.

  • Now I'm going to say this circle is at the radius times three

  • and maybe I will give it a stroke weight of eight.

  • I want no stroke here.

  • Let's make it stroke weight of 24 or 64.

  • Let's give it some alpha.

  • There we go.

  • That's kind of my visualization of the accretion disk.

  • The truth of the matter is,

  • this is not exactly correct 'cause I have a feeling

  • that distance is right here in the center

  • whereas the stroke weight kind of

  • fans out the thickness there.

  • It's a little bit off and I probably should

  • be a little bit more accurate about that.

  • I guess what I could do is just add to the radius itself

  • the half of the stroke weight.

  • If I add 32 there, there we go.

  • I think that's accurate now.

  • Nothing about this is really accurate.

  • Now let's draw the unstable photon orbit.

  • That would be at 1.5 of the radius.

  • I can do the same exact thing, but this time at 1.5.

  • Maybe I want to make that one a little less thick.

  • Give myself a little more space here.

  • Make the 16 and make this 32 and maybe this one

  • should be some kind of color 'cause I'm so good at design.

  • Some kind of orange.

  • It was better before I guess.

  • Some kind of orangeish color.

  • Look at this!

  • We now have the black hole, the accretion disk,

  • and a sort of photon orbit ray.

  • The other thing that I want to do is

  • I want to start a bunch of photons,

  • I want to have them coming from over here

  • towards the black hole.

  • I need to know how far,

  • I can't jump up 'cause my green screen only goes this high,

  • I want to know how far up does it need to be

  • for it to not really curve,

  • but travel in a straight line around

  • the path of the curved path of space and time

  • to end up in an orbit to either spiral out to infinity

  • or end up into the black hole.

  • That we said was 2.6 Schwarzschild radius.

  • What I'm going to do is I am going to create another class.

  • I'm going to call this photon.js.

  • Photon.js will be a photon, make a constructor,

  • this will also have a position, give it an X and a Y,

  • and let's make sure we include it in our HTML file,

  • and let's also write a show function.

  • Let's just make this stroke weight four,

  • point this dot pause dot X, this dot pause dot Y.

  • Let's make this distinctly red just for right now.

  • Now what I could do potentially is let me make an array.

  • I'm going to call it light,

  • maybe just call it photons particles,

  • I'll call it particles, whatever,

  • and I want to say four let Y equal 320,

  • and really this is height divided by two,

  • Y is greater than height divided by two minus,

  • I'm actually going to put these into variables

  • 'cause it'll make more sense.

  • Let's say start is height divided by two,

  • end is height divided by two minus

  • M87's Schwarzschild radius times 2.6.

  • Height divided by two.

  • Actually, I can start it at the end.

  • It's less than start and Y plus equal every 10 pixels.

  • Let's do that, every 10 pixels.

  • What I want to do, particles index I equals

  • a new photon at where?

  • X will be just with minus some amount,

  • with minus 20 comma Y.

  • What's wrong there?

  • I is not defined.

  • I'll just say particles dot push.

  • I can add things to the array with a push function

  • 'cause I'm not using I to iterate.

  • That makes more sense.

  • Here in the draw function,

  • I can say four every photon of particles P dot show.

  • There we go.

  • I have all of my points of light that

  • I want to send out at the speed of light

  • towards the black hole and watch them

  • follow their straight line path through the

  • curved space time thing.

  • Now let me give them a velocity.

  • If I go back into my photon,

  • I'm also going to say this dot velocity equals create vector,

  • traveling at the speed of light in the negative X direction,

  • negative C comma zero.

  • Then I am going to do, not constructor,

  • an update function in which case

  • this dot position dot add, this dot velocity.

  • What I can do now is in sketch.js,

  • is I can say P dot update.

  • Ready?

  • Here we go everybody.

  • There they go!

  • What is the speed of light here?

  • 30.

  • 30 pixels per frame is going to be

  • going quite slowly actually.

  • The frame rate seems slow.

  • 30 pixels per frame,

  • that's not really a nice way to watch an animation

  • so I'm going to include another variable.

  • I'm going to call it DT.

  • This is the delta time step.

  • Even though my frame, my animation frame,

  • goes one at a time, 30 frames per second,

  • maybe I only want to travel ahead a little bit in time.

  • Let's try .1, a tenth.

  • What I need to adjust by that delta T

  • is the photons' velocity.

  • I guess I'm going to make a copy of this.

  • I will just say this dot delta V equals

  • this dot velocity dot copy and then this dot delta,

  • this is a little bit awkward about using P-vector,

  • multiply by DT.

  • Not this dot.

  • I just want a delta V and then multiply that by DT

  • and then add that instead.

  • Now you can see, there we go.

  • There are the photons traveling at the speed of light,

  • but they're not curving, they're not changing.

  • Now we just need to have them bend.

  • We need to have them bend according to

  • the curvature of space time.

  • This is now the moment where it makes sense

  • for me to go read one of those papers

  • and try to look at those formulas,

  • but I'm going to actually see what happens

  • if I just apply newtonian gravitational attraction.

  • I need this photon to basically

  • have a force pointed in the direction

  • of the center of the black hole that it

  • accelerates its velocity towards it,

  • but an interesting side note here

  • and I guess this is an approximation of relativity,

  • is that we can't go faster than the speed of light.

  • We're really only adjusting the direction

  • so the speed is going to remain constant

  • at the speed of light.

  • Again, major caveats to how inaccurate this is,

  • but it's a starting point.

  • I want to add a function.

  • Basically, I want to be able to say I have some sort of force,

  • which is M87 curve, pull, I don't know what to say.

  • I could say attract.

  • I'm just going to say pull as a kind of arbitrary word.

  • Pull that particle.

  • Then I want to say particle apply force that force.

  • This is very similar to how I approach

  • a very basic physics simulation in my

  • nature of code examples.

  • You could refer back to chapters one and two of that book,

  • which is essentially what I've got here.

  • What I need is a function now in the black hole object

  • called pull that expects a photon.

  • First thing I need to calculate this force vector

  • is a vector that points from the particle itself

  • towards the center of the black hole.

  • To make that vector,

  • I'm going to make variable called force,

  • and I'm going to say P5 vector dot subtract, what is it?

  • This dot position, the position of the black hole,

  • minus photon dot position,

  • the position of that particular photon.

  • That's the vector.

  • What I need to calculate the force of gravity

  • according to the newtonian laws of motion

  • is the force of gravity equals the

  • universal gravitational constant, G,

  • times the mass of one object.

  • Guess what?

  • That's this.

  • Times the mass of another object.

  • What's the mass of this?

  • We've got a problem now.

  • The mass of this is, well, zero, but the force isn't zero.

  • This makes kind of no sense.

  • The good news for us is we're going to

  • use this in acceleration.

  • Because we also have force equals mass times acceleration

  • and I want to know the acceleration of this,

  • acceleration equals force divided by mass.

  • This mass will just get divided out.

  • Of course I couldn't divide by zero.

  • This is the essence of why I'm kind of

  • going down the wrong direction here to do this accurately,

  • but it's a starting point.

  • I can just consider this right now.

  • G times the mass of the black hole itself

  • divided by R squared,

  • or the distance between the photon and the black hole,

  • R squared or distance squared.

  • I have that value actually in my code

  • because I have R is the magnitude of the force.

  • Then I could actually calculate that force magnitude,

  • which is the force of gravity equals G times this dot mass

  • divided by R times R.

  • Now I could say force dot set magnitude F of G, photon.

  • Then I can just say this dot velocity add force.

  • Photon dot velocity.

  • Then I just want to add that force to the photon's velocity.

  • P dot apply force is not a function.

  • Actually, I've simplified this.

  • What I can just do is get rid of this whole

  • extra step or returning the vector and I can just do this.

  • (man yelling)

  • Did you see those photons?

  • Let's be able to see this a bit better.

  • We'll see why this is not correct.

  • First, let me do a couple things.

  • One, let me make these variables.

  • Let me draw lines.

  • Let me say stroke zero line from zero start zero end.

  • Sorry.

  • Zero start width start.

  • Let me say stroke weight one.

  • Then let me also do the same for the end.

  • I just want to be able to see where these are.

  • This is showing that spot.

  • Guess what?

  • The photon should always be traveling at the speed of light.

  • I forgot about that so this is a huge hack here.

  • I'm going to say right here when I do that pull

  • in the black hole, I do that pull,

  • I'm going to say photon dot velocity dot set magnitude C.

  • Maybe I should just limit it.

  • Let's just limit it.

  • It could slow down I suppose.

  • (man laughs)

  • It could slow down.

  • Let's just limit it and see what happens.

  • Everything got sucked in!

  • Look at that!

  • The black hole sucked it all in!

  • Couple things.

  • This is good.

  • In other words, this is kind of a nice little simulation

  • in the sense that the force is so strong,

  • even in my very crude simulation,

  • that it's never going to escape.

  • Let's add a little trail here.

  • The photon, I'm going to give it a variable called

  • this dot history.

  • Every time it updates its position,

  • or every time before it updates its position,

  • I'm going to say history dot push this dot pause dot copy.

  • Then also this dot history.

  • Then also let's just say if this dot history

  • dot length is greater than 100, this dot history dot splice,

  • let's just remove the oldest element.

  • Now I can say stroke zero, stroke weight one,

  • begin shape, end shape, no fill,

  • for every vector in history, vertex V dot X, V dot Y.

  • This dot history.

  • Now we're able to see those paths as well as they bend.

  • Again, I don't have it right and we can

  • give ourselves a much longer history here.

  • What I'm going to do here now also is

  • I am going to start some particles from higher than end.

  • I'm just going to start all the way from zero like the top

  • and then let's space them out by a little bit more,

  • and here we go.

  • Nothing escaped.

  • Look at that, nothing escaped my black hole!

  • Again, it's not accurate.

  • I've got this wrong because I'm

  • not taking to account general relativity,

  • which is kind of a thing.

  • It's a thing.

  • I'm visualizing the idea here.

  • What happens if I make the gravitational pull

  • constant smaller?

  • Let's see if we can get anything to escape.

  • There we go.

  • You can see it's there in the photon orbit,

  • but back into the center.

  • We're getting the idea.

  • Do we have something sort of similar to this?

  • (bell rings)

  • I think we do!

  • Change the color to red.

  • Try shrinking the mass.

  • Let's go back and change.

  • You want the color to be red of the photons' paths.

  • I can just take this out.

  • Let's also make this stroke weight a little bit wider

  • so I can see it.

  • Let me give actually give myself much more space here.

  • I'm going to say window width whatever space I have,

  • window height, and let's also make this even less.

  • Full screen, here we go.

  • Here is my black hole simulation.

  • Let's move it over so we can have

  • a little bit more space to work with.

  • 300.

  • There we go.

  • Of course, you can see this should be

  • where they're able to escape,

  • but my simulation is completely off

  • so I should take out those start and end lines

  • and just watch this go.

  • There it is, the curvature of space and time.

  • Center the black hole please.

  • That's actually not a bad idea.

  • I'm also just going to unfortunately take out these lines

  • just 'cause it really shows how incorrect it is.

  • For me to have some plausible deniability here.

  • Let's see what we've got.

  • It's running quite slow because of

  • drawing all these trails.

  • Some things that I could do to make this better,

  • flow faster, number one is in the photon itself,

  • I don't have to add every single spot,

  • I could just say if frame count module is 10 equals zero.

  • (bell rings)

  • This video's not over yet.

  • Thanks to the chat,

  • I think this is actually kind of problematic.

  • The way that I'm doing this is the

  • photons are actually slowing down and they should stay.

  • I shouldn't limit them to the speed of light.

  • I should actually just keep their

  • magnitude at the speed of light.

  • Again, it's sort of inaccurate anyway

  • so what's the difference.

  • I've also added more photons here.

  • You can look at it this way.

  • This is going to be a slightly different result

  • because now I'm locking them at the speed of light.

  • I think there's also a big performance

  • improvement that I can make,

  • which is that once they're sucked into the black hole,

  • I don't need to keep tracking their position

  • and keeping that history going.

  • I should really shut them off as soon

  • as they've been sucked in.

  • Another thing that I can do here is

  • since I have that distance,

  • I can say if R is less than the event horizon,

  • I could remove the photon, just delete it,

  • but I want to still see its path.

  • I think I'll say photon dot stop.

  • That doesn't mean anything,

  • but I can then in the photon class,

  • I can have a variable called stopped equals false

  • and then I can write a function called stop where I say

  • this dot stopped equals true.

  • Then what I can do is I don't want to continue updating it.

  • As long as it's not stopped, I can just freeze its history.

  • I still want to draw it where it last was,

  • but I can freeze its history.

  • Here we go.

  • There's my photons flying at the black hole,

  • space time starts to bend, the photons spin around,

  • and some will get sucked in, some curve, and there we go.

  • (bell rings)

  • I am back a few days later again.

  • Thank you for watching the coding challenge.

  • This is the code that's actually

  • linked in the video's description right here

  • and I've made some further corrections.

  • There's some pretty significant somewhat trivial errors,

  • but errors nonetheless.

  • Number one is in the code that I wrote,

  • this was actually called ER, this dot ER for event horizon,

  • which is not a variable I'm using anywhere.

  • It should actually be RS for Schwarzschild radius.

  • I even had a typo here, photo dot stop instead of photon.

  • Now this is actually stopping the motion

  • of those photons as they enter the event horizon.

  • Now in the simulation, none of them have escaped.

  • Why have none of them escaped?

  • I was actually drawing things the wrong size as well.

  • I didn't realize that ellipse mode,

  • which I love this new function I discovered ellipse mode,

  • which allows me to specify the size of an ellipse

  • by a radius instead of diameter.

  • It doesn't actually work, this is a P5 bug,

  • with the circle function.

  • You'll notice if I just change this to circle,

  • suddenly it's drawing it a different size.

  • I like to use the circle function

  • 'cause it's kind of a nice simple word

  • as opposed to ellipse, but it was actually a mistake.

  • Maybe now that I've put this out there into the universe,

  • this bug has been fixed in P5.

  • Of course the main thing about this simulation

  • that I did in the coding challenge is that

  • it's wildly inaccurate.

  • I am using newtonian gravitational attraction formulas

  • with this very crude physics time step 2D canvas thing.

  • With this, I've actually now drawn a line at this distance,

  • 2.6 Schwarzschild radius,

  • I've drawn a line there and that is the place

  • at which the photon should enter the photon ring orbit

  • and escape outwards.

  • You can see even at that spot right there, this one,

  • comes on in and disappears, you can't really see that,

  • into the black hole itself.

  • In fact, all of these do all the way up to there.

  • None of them make it out.

  • This is inaccurate.

  • Thank you to Chris Orbin from STEM coding

  • who sent me an email over the weekend with some formulas

  • to instead of calculating using

  • newtonian gravitational attraction,

  • you could actually just calculate the

  • angle of velocity for that photon

  • and just set its magnitude to the speed of light.

  • Again, this isn't superbly accurate,

  • but it will draw the paths more accurately

  • than what I did with this just using

  • gravitational attraction.

  • I have that, I will also link this.

  • You can see the formulas here.

  • I might refactor this a bit,

  • but the whole idea is that I just have an angle

  • that's between the current photon's position

  • and the black hole's position.

  • Then I calculate the change in angle

  • based on the relativistic curvature of space time

  • and then adjust the angle based on that change in the angle

  • and then set a new velocity in that way.

  • I'm cheating a little bit 'cause the

  • angle stuff is so strong,

  • it spirals some stuff out that shouldn't

  • so I just give the Schwarzschild radius

  • a little bit of bigger room there to capture the stuff.

  • You'll see now when I run this.

  • This is the one we want to follow.

  • You can see it's right there at that 2.6 measurement.

  • Here it comes, here it comes.

  • That one goes into the photon ring and escapes,

  • it's that one, while this one gets caught inside.

  • I'm not suggesting this is now a

  • scientifically precise simulation of

  • the curvature of space time around a black hole.

  • Once again, this is JavaScript Canvas 2D with P5,

  • but we've gotten something closer there.

  • I will try to include links to the explanation

  • of how these formulas work.

  • I hope to hear from you.

  • How can you play with this?

  • How can you turn this into 3D?

  • What kind of steps might I take next

  • if I wanted to work on this further and

  • actually look more at how that image was

  • actually created itself,

  • or maybe model the accretion disk

  • and how it's sort of bending.

  • I don't know.

  • There's so much more that I could do.

  • I hope you make a version of this.

  • Share with me in your comments.

  • Somebody who knows way more about black holes and science

  • I'm sure will help me out there.

  • I'll see you in a future coding challenge.

  • I'm looking for my train whistle.

  • I'm standing on top of a train that's moving very fast,

  • but you are also and so to you,

  • it looks like we're standing still.

  • Relativity.

  • (train whistle blows)

  • (upbeat music)

(bell rings)

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

編碼挑戰#144:2D黑洞可視化 (Coding Challenge #144: 2D Black Hole Visualization)

  • 15 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字