Intercept Form in Code

I got the intercept form working in code. It’s pretty interesting to see the curve working in real-time and how the root’s Y value doesn’t affect it.


Something I noticed while paying with the values is that setting a max height or modifying the value of ‘a’ directly have the same results, this can be used as optimization, since the following code:

    public float CurveIntercept(float x, Vector3 root1, Vector3 root2, float highestPoint)
        float midXPoint = (root1.x + root2.x) / 2;
        float a = highestPoint / ((midXPoint - root1.x) * (midXPoint - root2.x));

        return a * ((x - root1.x) * (x - root2.x));

Ends up with the exact same result as this one

    public float CurveIntercept(float x, float a, Vector3 root1, Vector3 root2)
        return a * ((x - root1.x) * (x - root2.x));

The main differences that I noticed are:

  • ‘a’ can be harder to manipulate because of how small the number has to be.
  • A max height is easier to understand.
  • Setting ‘a’ requires far less calculations.
  • It’s way harder to set perfect maximum height by directly modifying ‘a’.
  • To make a curve that goes down, ‘a’ needs to be positive, while a max height needs to be negative.

I think it all comes down to optimization vs not having to teach math to your designer :sweat_smile:


That’s cool! I have been trying to make this at the same time. How do you solve for a to make it an N or U shape curve without having it too convoluted? and is X the range between root1.x and root2.x?

There’s an easy way to manage curves without using raw math. Use animation curves instead, you can very easily create very fancy effects with it.

In this formula, X represents the X value of the midpoint between the two roots. For instance, if you have root point A located in (1,0) and root point B located in (5, 0), then the X value would be 3 because the midpoint would be (3, 0).

This becomes slightly more complex when dealing with 3D graphics, that’s why I recommend using Animation Curve instead.

Cool! thank you for your reply.

I am actually doing it in raw math. What I did was to make the range between points and defining coordinates on each iteration.

My code works but in order to make it work with u-shape parabolas (or n-shape depending on how the screen grid is configured as I’m playing with Pygame) I had to write a few lines more of code that made it a bit unreadable and I was wondering if there was a cleaner way to do it.

Amazing application!! how did you make the sphere move across the parabola?

I used the formula I posted in the first comment, that’s the Y value of the movement, so, it’s basically some sort of lerp, but I highly suggest using curve animation instead since it’s a lot easier to use and far more flexible.

What did you use exactly for the x value? I’m trying to use a Lerp between the two roots but it’s not working :((

This depends on how simple or complex you want to make it.

For example, if you are taking into account the angle of the object launching upwards as well, then you have velocity in horizontal and vertical directions. You then have to consider does gravity apply. Now, honestly, you could just put a plane down and apply the vector upwards, enabling gravity to let it fall back. Where’s the fun in that. Plus, Unity will do that all for you if you really want it to.

So, simple approach, use time and distance between roots.

Take your equation, and decide the travel time so, roots being 1 and 10 for example, it is traveling 9 in the x over 4 seconds
That gives you 2.25 distance units a second. Now, using this, your X is updated to x=[start root]+(delta time * ([end root] - [start root])/ time) which is x=1+([delta time]*((10-1)/4)) or x=1+([delta time]*2.25)

Now you have your X, so plug that into the equation and calculate y and you have your position.

This is incredibly simplified but it does work. You can adjust the time to get it moving the way you want.

So, given this approach, you can probably figure out the rest yourself.

Privacy & Terms