Simple parabola?

  • How would I go about moving an object in a simple parabola from point A to B? No acc/dec, no varying speed, no platform behavior, etc. Just a simple linear tween but with a curve.

    I've searched around but everything I found has bizarre quirks or the above.

    I see the timeline supports bezier curves now but this is for an enemy behavior with dynamic points to jump to so I don't think that'll work either.

  • Let’s see. The simplest way to move in a parabola from one point to another would be to use the qarp expression. Of course you’ll need a third point as well for the middle but we can calculate that with a midpoint between the two points and move it up by some distance such as 100.

    Basically give your enemy five variables: t=1, x0,y0,x1,y1

    On some trigger to start the tween

    — enemy: set t to 0

    — enemy: set x0 to self.x

    — enemy: set y0 to self.y

    — enemy: set x1 to destination x

    — enemy: set y1 to destination y

    Enemy: t <1

    — enemy: set t to min(1, self.t+dt)

    — enemy: set x to qarp(self.x0, (self.x0+self.x1)/2-100, self.x1, self.t)

    — enemy: set y to qarp(self.y0, (self.y0+self.y1)/2, self.y1, self.t)

    Now since this is a parabola the speed will be faster at the start and end than at the middle. Making the speed constant is tricky. The simplest way is to store a list of points and treat it as a polyline. You’d do a loop to move over each line until a specified distance was moved per frame. Alternatively you could store the previous tick location and iteratively adjust t till the new calculated location from qarp is always approximately the same distance from the previous location.

    There is also the math way to do it. I think the terms are function reparameterization to length. It involves calculus with integrals which I’m a bit rusty on. It could simplify to a reasonable equation though. Typically I’ve seen it approximated with the other two methods.

    Or instead of a parabola you could use an arc instead. Arcs can be moved over with constant speed easily. You just need a center.

    Give the enemy these variables: t=1, cx,cy, a, r

    On some trigger

    — enemy: set t to 0

    — enemy: set cx to (self.x+destination.x)/2

    — enemy: set cy to (self.y+destination.y)/2

    — enemy: set a to angle(destination.x, destination.y, self.x, self.y)

    — enemy: set r to distance(self.x,self.y,destination.x,destination.y)/2

    Enemy: t<1

    — enemy: set t to min(1, self.t+dt)

    — enemy: set x to*cos(self.a+180*self.t)

    — enemy: set y to*sin(self.a+180*self.t)

    That will make the enemy move in a half circle arc. More subtle arcs could be possible too with further tweaks of the equations.

    Also note both methods will complete the motion in one second. To make it complete in say 0.5 seconds change the *dt to *2*dt in the equations.

  • A parabola can describe quite a few final shapes. You mentioned constant speed, so rather than a fixed duration you would have a proportional duration based on distance traveled. Do you care if it swings out to the left or right? How wide do you want your curve? Will the width vary depending the distance traveled as well, or fixed?

    After giving this a shot, the problem is harder than it seems... especially considering the distance traveled is also affected (non linearly) by the shape of the curve, making it difficult to get a fixed speed across all situations as far as lerp and qarp are concerned. Tween also has difficulties when non linear factors come into play. Maybe I can try again with custom movement.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • rojohound Thank you so much! You never fail to impress with this stuff. I think the arc is more in line with what I'm thinking. Forgive my terminology my math is (obviously) quite rusty as well. I'll give this a shot soon.

    oosyrag I will try r0j0hound's method but yes ideally the object will move accurately and at a constant rate from A to B with C being the curve. All 3 points will be adjustable (but not during motion). I think bezier curve or arc might be more appropriate terms.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)