but in a shooter game you can shoot the ball to which cannot be represented by fear it should be a plane the wall is a plane actually a quad so there is forza there is four vertex and that’s a quad ok plane can be represented by a normal vector and a point the NX NY and NZ or the normal vector XY and z coordinate the X 0 y 0 and that 0 is a point on the surface not matter which point so you will get the same not matter which point you choose but you will choose that point and yeah that’s it ok and and that’s about it how we can calculate a plane point obviously here the X Y and that will be put on this on the plane any point you want depend on what you use all right and in the Ray it is basically the same as earlier again we can do the same trick as previously so I just copy these x2 here in the place of L the Y to the place of Y and that place of that ok here is it as you can see I just copied the x y&z to the place of x y&z this is how this is counts so it is pretty simple until this point now I do the multiplication so I multiply the NS by an X the t times XD with the tire with an X and yeah that is how we do it ok and after we do that we get this long line ok but we want to know the T so actually be first so I just pack everything which is not see to the other side first of all I start with the nun the negative expressions like this and there are three more of these then I subtract the other expression life is an extra time at excess so everything I moved here so everything I subtract and add which doesn’t contain a tea which continuity I will divide with it so everything which is not tea so in this case for example I divide the feed and the time and X okay and after I do that with the whole thing I get this line so we only have to put everything in here so X 0 is the x coordinate of a point and so on so I just put it here and then that’s about it we get the teeth actually quite easily easier than the previous one now we actually need to know that is it the quad or it is drugged the plane because the plane is an endless thing ok if this is 0 so every time and except that blah blah blah so this is if this is 0 then we cannot do the division because we don’t like to divide by 0 so it will basically mean that the Ray is parallel to the plane there is no collision never and ever okay that’s what I said ok now we get the teeth we have to put it back to these free equation so I get the T I know the XS and the XD I put the DT in here and I get the XY and that coordinate now I know the core coordinate so I need some way to find out is it the quad or is it the endless plane okay there is a lot of writing in here which basically I do in here that I get the collision point and I know the quad it has four points now I divide this quad by two two triangle by draw the diagonal in here as you can see it is two triangle and the quad can be separated into two triangles why do I use triangle because it’s simple okay so I have a collision point right here so in here I could draw it if I want

so here is the collision point okay and now there is the collision point and with this collision point and the other point of the triangle I can make three more triangle so I have one quad I divide this one quad to two triangle and I separate this one triangle into three other triangle now the trick in here is I calculate the area of the this triangle this triangle and this triangle and the main triangle – and I compare them if the area of this free little triangle is the same as the area of this big triangle then the point is inside is the point would be outside so for example let me draw a point outside for example in here then the free triangle will be one this okay and this as you can see it is obviously bigger so the area of this free triangle is bigger all right now yeah let’s see so here is what I said okay I will calculate the area of the triangle by something called the Hyrum formula this is actually very simple I just get the circumference of the circle I divided by two so here I represent with as here is it okay and I use this formula to calculate the area so I get the square root and I use s times s of minus eight times s minus B times s minus C this is the Hyrum formula and this will give the area of the triangle okay let’s see it is in eighth in code now that we saw the theory behind the rifling collision detection let’s start coding so it will return a bool as well and it will be R a plane the name and we need a normal vector so load an X float Y and Y and float and that okay and we need the star point of the Ray so float this will be the excess float X as Y L float as that o YF + a – okay and then we need the reduction vector so float the other ray obviously float as the load why they are float that deep okay and and I believe only the point of the quad is remain so instead of write 12 more float variables I include the object loader here so include who be jailed older and now that I included the object loader header file I can use the coordinate structure which is basically just free float numbers so instead of twelve more variable within just four more variable so coordinate this is the name of the structure and I call it p1 and coordinate be too cold be free and cold minute p4 that’s right and we already read the prototype so now I just copy this to save time under this other ray tracing algorithm and paste it now first of all as you can see in here we need to calculate this so xt type and XYZ time and Y and that D time and and Z which is basically is the dot product of the direction vector and the normal vector and if it is zero then it means that the Ray is parallel to the plane there is never and there will be never a collision so that’s why I’m making variable because we use the two times first to examine that if it’s zero or

not and second times in the division if we do so throat I don’t know call it what should call it a nevermind and I just type it an XD tram an X or Y a + yd x + y + Z D times and said that’s right I think I wrote it good so now if it is 0 so if if Oh what am i doing so if a is zero then we just return lit fault because if it is parallel then it will never touch the train so yeah that’s the case now we need to actually calculate the T so I just write this long line to here okay I maybe cut this because the it’s long that I just it is long that I just type in and boring okay I just typed in this line actually it is almost the same as the line in here there is too little difference one if the X nor Y new and that moon is not exist instead I will use the p1 as a point I could use p1 p2 p3 and p4 I will get the same result so I use the p1 dot s p1 dot y and p1 dot that instead of X nor Y new and sadness okay and the other difference is that this long expression which is the normal vector time the director dot direction vector is I represent with a because I’ve already calculated it I don’t want to calculate it again and now if the T is less than zero it means that the Ray is the color collide with this plane in the negative direction so for example a veteran don’t shoot the ballast in front and in back just in front so that’s why we return with force again if the T is zero less than zero like here is an example as you can see this is the plane the vector is goes in this way so the rate goes in this way but because it is negative it will actually do the collision so the collision happened actually in here so behind this behind the Ray and we don’t want that so that’s why we returned with force because there is no collision now we calculate the x y&z coordinate of the collision so where does this happen so float X and the X equal to the torque point so X s plus T times eggs if it is the rate equation if you remember okay I do the same with the Y and that 2y and that so I call it Y I said why why okay I calculated a new point and this is where the collision happens so this X Y at that coordinate this is this is what I will use to create a free new triangle okay now before I move on I create a new function which will calculate the area of the triangle so float it will return with area and I call it triangle area okay and it will wait free point coordinates P 1 coordinate P 2 and coordinate P 3 ok and it will return the area as I said okay so now I just implement this function somewhere and really matter here I implemented okay so haha we used the hiren formula to calculate but first we need to calculate the length between the two points so we use the Pythagoras theorem for that again so float a and the length of a will be

square root s QR t and so first I need to create vector of this and I create vector of DS by subtract the start point from the end point so in this case for example P 2 dot X minus P 1 P 1 dot X and I do it again so I multiply it by P 2 dot X minus P 1 dot else because we want to we want the square of of this because that’s how the photographer instead a square plus B square plus C square and this all under the squirrel okay and I add the same with the y-coordinate so I just copied to face time okay so this will be y Y and again y y plus and again with the dead coordinate so see the the V that’s right so I just calculated the length of a with the Pythagoras theorem and I created a vector by subtract the endpoints from the start point or or the start point from the end point never mind so you can exactly see what is going on if you draw right triangle in the paper and then you can see how this comes okay I do the same with the BC and BC so be B and C called B not will be and this is called see okay the only difference will be that this is the other two sides so this is P 3 minus P 2 P 3 minus P 2 P 3 minus P 2 and yeah that’s what we do so P free minus P 2 ok so this is the other side which is goes from P 2 a 2 P 3 point and we have one more side this is the point which goes to from P 3 to P 1 okay that’s right so I just rewrite this actually the order is not matter that P 3 to P 1 or P 1 to P free because we get the square of them and that if it was negative then it will be positive and yeah the length will be the same anyway now we need to calculate the half of the circumference so float a and not a call it D ok and this will be yes okay so a plus B plus C and this is / – okay and now we will just earth return with the S key or T s times s minus a times s minus B times s minus C okay that is how the hero formula looked like so this will get the area of a triangle now get back here and let’s calculate it okay as I said the XY and that point will be inside the triangle if the big triangle so where is it oh here is it so if this big triangle area is the same as the three small triangles rheya okay so I just go back to that to hear a plane and I use an if and actually before I do that I create a new coordinate from this X Y and that coordinates so coordinate and I call it coalition point CP and this will be X Y and well because I can only give

coordinate to this there where is it triangle area function that’s why I have to create that and now actually okay and now it’s time to calculate the area of the triangles I did a little drawing here which may help to visualize their what triangle area should we add and so on so yeah this is our quad and I divided it to two triangle and this two triangle is divided by divided two three more triangles so for example if P 1 P 3 and P 4 area is equal to P 1 P 3 c p p1 p3 CP + 3 4 P 3 CP area then the point is inside that yeah the point is inside ok by the way one more thing before we go on is that I don’t use that the main area is equal to the area of this free instead I calculate the main triangles area and I subtract all of the other areas I put it in absolute value and I see what the value is that’s because for example if there is a number one point zero zero zero one that’s not equal to one point zero zero zero so there is very small difference but it makes it not equal I just want to be a not equal okay and and yeah that’s a problem because the precision is sometimes called some very small changes but if I do that this one’s point a lot of zero one minus one point zero zero zero and I check what what we remain after this subtraction so if it is less than 0.001 then it will be equal that is the threshold precision what we want so for example if I write zero there 0.1 in here that will mean that one point one is the yes the one point one is equal to 1 so that is the precision we obviously need some higher precision so like that approximately and I put in in absolute value so abs so it’s not matter that the first one is the greater or the second one it will be a positive number because the absolute value is in case of for example minus 5 it becomes 5 in case of 5 is 3 minus 5 so not matter that is it will be the first is the bigger number or the second the absolute value will be a positive number so that’s why we can use this left our sign okay that’s so let’s calculate it where is it oh so first of all I put an if and then absolute value and then I get the main triangle area so in this case for example P P 1 P 3 P 4 so P 1 P 3 P 4 okay and then I subtract the oh this one to be try and go Rhea so if the triangle area P 1 P 3 P 4 which is the main triangle as you can see – triangle area P 1 P 4 CP even before she peeped – and then another one P 1 P 4 P 1 P 3 C P okay – triangle area this will be the last month P 3 P 4 CP e free before CP and I closed the absolute value parentheses is less than I don’t know 0 0 1 so just a precision because usually there is just small such small differences between the two but it will not give an echo that’s

why I use this method instead of using an equal sign here and additions here so – okay then now we need an or because we have two triangle and not matter that the the collision point is in this triangle or in this triangle so yeah it’s not matter because they are the same quad so I use an or or and again I just type it now I don’t show you where the that that come from just type it quickly maybe I cut it even I don’t know so if again I start with absolute value triangle area and again I get the main triangle so P 1 P 2 P 3 oh yeah yeah I wrote it good okay and then triangle area P 1 P 2 CP – triangle RIA p2 p3 c p- triangle area and the last one is p1 p3 CP okay then and if it is less than zero zero zero one okay so if it is true so one of them is true so the collision point is inside one of the triangle then we know that there is a collision so we return with true as we just returned with fourth that’s right and our function is ready to use so now I just go to the further TPP and I draw a train right here for example under this line not really matter there so I draw the plane toward G L begin GL quads had always and then gr and okay and here I just draw the plane so just I will be back I just find a plane okay here I’ve just found the plane so I hope the keyframes are okay so this is just if I would run the program now I think we can run the program now oh by the way I think I forget to show you that there was an error right here this T parenthesis wasn’t closed I think that was the only error I got earlier but never mind so now maybe we may get new errors it’s Anna great so I just compile the program yeah compiling okay and run the program and here is it as you can see this is just a big plane the length of all side is 10 you have already drawn trains okay so here is the four vertex as you can see you can copy it if you want you can make other planes okay so now I just called this rape time function which I did a little bit earlier but I will call it again so if rape claims and now we can give first of all the normal vector if I remember well so ray plane there is the normal vector normal vector of the plane in this case it is zero point zero zero point zero and 1 point 0 so it is parallel to the said axis and it’s looking toward us okay and that the next parameters or the third point of the Ray this will be the race toward the x-ray towards God why and race towards that okay then and then we need the what we need for the direction vector of the Ray this will be a little tricky because now we the Ray will go in a different direction than earlier so instead of one point O it will go minus one point oh yeah not really matter basically just so there will be an intersection okay and then where is the next oh the coordinate

of the plane oh I forget to show you that that I’ve created free coordinate with these are the same coordinates as the plane coordinate as you can see so these are the same numbers I just created the for structure with it for instance of a structure if I want to be more precise and now I just give these parameters to the Ray plane okay so I give p1 p2 p3 p4 and that’s it okay so if it is true then we creative leverage change the color free F we change the color to red again okay then and now I think we can run the program if I don’t forget anything else yeah I move the race toward that coordinate from minus five to zero that’s all I think that all that I did so nothing really interesting now we can try to compile a program which is successful which which I spat seeded in that and now I can move the ray with the Keith as earlier okay I can start the program but as you can see there is a little bit of problems although I changed the direction of the Ray I don’t change the direction of the line which represented Ray I can pick that very easily by here that I draw the lines of the Ray I set it’s minus 1 so the new normal vector not the old one so now if I run the program you can see the direction of the line is the opposite as it was now I can move up down right maybe that’s better view for right left so as you can see it’s approximately work maybe we should keep the threshold a little bit smaller but you see work approximately in every direction okay then by the way probably it was a quite boring chapter because there it was nothing too interesting in it there was just a few line and sphere and plane but it is a very important chapter because the tutorial actually not chapter okay so it’s a very important tutorial because it will be the base of the sphere plane collision detection – and it will be it can be used for ray tracing obviously for lighting great racing I mean then it can be used for ballets and quickly moving objects – and so on if you don’t understand the math try to find yourself so try to find out yourself in a paper that’s definitely help sometimes if someone else do some something in math I don’t understand that when I have to do the same thing I I understand so that’s why I recommend to use a paper pan and try to find yourself these simple formulas okay and if you don’t understand completely not help the drawing paper don’t worry the math will work even if you don’t understand okay that’s what this tutorial thank you for watching and have a great