Guye Incognito
Guye Incognito

Reputation: 2844

Maths in programming. Get the angle at point on a parabola

I'm learning Unity3d + some basic maths I've forgotten by messing around.
Heres what I'm doing now..

As you can probably tell the sides of this shape form a parabola. The distance they are out from the centre is the base radius + the height squared * by a constant (0.05 in this image)

The code generating this is very simple..

for (int changer = 1; changer > -2; changer-=2) {

        Vector3 newPos = new Vector3(
             transform.position.x
            ,transform.position.y + currentheight*changer
            ,transform.position.z - RadiusAtZero -(Mathf.Pow(currentheight,2)*CurveMultiplier)
            );

        var newFleck = Instantiate(Fleck, newPos, Quaternion.identity)as GameObject;
        newFleck.transform.RotateAround(transform.position,Vector3.up,angle*changer);

        FleckList.Add(newFleck );

        }

Btw the for loop and 'changer' mirror everything so 'currentheight' is really just the distance from the centreline of the parabola.

Anyway I'd like to make the cubes (or flecks as I've called them) be angled so that they are tangentional to the parabola I have made.
I need to determine the angle of a tangent to the parabola at particular point.
I found this

to find the line tangent to y=x^2 -3 at (1, -2) we can simultaneously solve y=x^2 -3 and y+2=m(x-1) and set the discriminant equal to zero

But I dont know how to implement this. Also I reckon my 'CurveMultiplier' constant makes my parabola equation different from that one.
Can someone write some code that determines the angle? (and also maybe explain it)

Update. Here is fixed version using the derivative of the equation. (Also I have changed from boxes to tetrahedrons and few other superficial things) enter image description here

Upvotes: 0

Views: 851

Answers (1)

lurker
lurker

Reputation: 58234

The easiest solution is to use a derivative for the parabolic equation.

In your picture then I'll assume Y is vertical, X horizontal, and Z in/out of the screen. Then the parabola being rotated, based upon your description, is:

f(h) = 0.05*h^2 + R

(h is height, R is base radius). If you imagine a plane containing the Y axis, you can rotate the plane around the Y axis at any angle and the dual parabola looks the same.

The derivative of a parabolic equation of the form f(x) = C*h^2 + R is f'(x) = 2*C*h, which is the slope of the tangent at h. In this specific case, that would be:

f'(h) = 0.1*h

Since the cross-sectional plane has an angle relative to X and Z axes, then that tangent will also have the same angular component (you have a rotated parabola).

Depending upon the units given for the constants in f(h), particularly the 0.05 value, you may have to adjust this for the correct results.

Upvotes: 2

Related Questions