Friday, December 9, 2016

mathematics - How do you calculate if a line is facing a curve?


Given the 2 points describing a line segment, and the 3 points describing a quadratic bezier curve, how do you calculate if the line is facing towards or away from the curve?


The line has a fixed length, and is stored in 2D as x1/y1 and x2/y2. The curve is a quadratic bezier curve, and is stored as x1/y1 (control point), x2/y2 (bezier point), x3/y3 (control point).


Curves



Answer



First of all we need to know how a beizer curve is generated. according to it's wiki page, a Quadratic beizer curve is a function of t stated as follow: beizer formula
In this formula t is a variable between zero and one and all other parameters are vector values (it means B(t), P0, P1, and P2 have values for x and y in 2D world).



the next thing you should know is how to define a line based on two given points. again we can see in beizer wiki page that lines are some special kind of beizer curves with only two control points and no beizer point. and their formula is again defined using parameter t as follow: Line formula
again t is a scalar value while p0, p1, and L(t) are 2D vectors.


Now we want to know if our beizer curve collides with the ray started with an specific point in an specific direction. to do so we need to solve This equation:


formulas to solve



  • in this formulas t and t' are unknown variables and all the others are known

  • P0, P1 and P2 are representing Beizer curve while P'0 and P'1 are endpoints of the line.

  • we can't assume t=t' in most cases. and it's also possible to get two values for both t and t'. having two results only mean the line has two intersection points with the curve and it's nothing to worry about.


solving that equation is almost easy but the results of the equation will tell us about collision states:



if in a results we get t<0 or t>1 it means the ray does not collide with the curve itself but with it's trails which is a point we are not interested in.


if in a results we get t'<0: it means the line collides with the curve in the opposite direction.


if we get t>0: it means lines collides with the


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...