OpenGL (SDL,C++) tutorial 17 – ray tracing (ray-plane intersection)

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