# How do I calculate the cubic distance?

• I have an object moving along a path, but I need to get the distance it travelled. Not the linear distance it travelled between every tick, but the cubic distance per tick across several ticks?

Any idea how i go about that?

• Sure, but I don't think it'll be that much more accurate because the fit curve can deviate a lot.

Anyway, the first step is to find the equation of a curve through multiple points.

2 points would be a line,

4 would be cubic

So basically you'd keep track of the last 4 positions and then using these cubic equations:

x(t)=a*t^3+b*t^2+c*t+d

y(y)=e*t^3+f*t^2+g*t+h

you'd plug in all the x,y and t values and solve for the unknowns. you can probably pick evenly spaced values for t. Here would be all your equations:

x0=d

y0=h

x1=a+b+c+d

y1=e+f+g+h

x2=a*2^3+b*2^2+c*2+d

y2=e*2^3+f*2^2+g*2+h

x3=a*3^3+b*3^2+c*3+d

y3=e*3^3+f*3^2+g*3+h

Then you'd just solve for all the unknowns: a,b,c,d,e,f,g,h which would define the shape of the cubic curve that goes through the last four points.

The solving could be done with algebra as a system of equations. you could also use a matrix and guass-jordon to solve it but the result would be the same. You can get someone else more current with math to do it if it's not something you want to do.

Anyways after that you'd have the cubic equations through the last four points, and going from t=0.0 to t=1.0 would give a curve from the current position to the previous. Actually that's flawed because if you drew that curve every frame the resulting curve would break every frame. so a better solution would be to use a different curve such as catmoll-rom or hermite or bezier. basically using p(0)-p(1) and p(2)-p(3) as tangents a only looking at the curve between 1 and 2. I've seen equations that do that and keep the curve continuous, but it probably can be derived.

So the above gets you the equations for a curve, or at least gives some leads how to get it. Next the actual getting of the length. To get it perfectly you could use an integral, but that's not solvable in most cases. The simplest solution is to use the equations we found to get a bunch of positions between frames and add the distances between those together. The more you use the more accuracy you'd get.

• R0J0hound Thanks! At least i know where to start, but not sure it would be worth it, Or if I even need that level of accuracy.

The moving object would have to stop after travelling a certain distance on a oddly shaped curved path, and spawn a sprite at exact regular intervals so these spawned sprites are "exactly" equal distance apart. kind of like a dotted line at the object's path.

• I did a test here in C3.

https://www.dropbox.com/s/g4sn4ldt3hz30 ... t.c3p?dl=1

I'm trying to create a trail behind the object with dots that are equal lenght apart...

As you can see in this example, they ofter vary, and i can't get it much more accurate than this at the moment. I want the object to spawn dots with equal lenghts between them, no matter how fast or slow the object is moving.

My main approach was to adjust the rate of particle spawn based on distance travelled but it seems to have som hiccups.

• I also tried to accumulate distance travelled every tick, and when it reached a certain threshold told it to spawn a new dot, but that approach didn't seem to work.

• Rather than having your main object spawn the dots, you should have the dots positioned at predetermined locations along the path and reveal them at that location after your main object passes them.

The problem here is you want dots spaced evenly, but for example even if you spawned a dot every tick, your object doesn't necessarily travel the exact same distance every tick, especially on a curved path.

• Rather than having your main object spawn the dots, you should have the dots positioned at predetermined locations along the path and reveal them at that location after your main object passes them.

The problem here is you want dots spaced evenly, but for example even if you spawned a dot every tick, your object doesn't necessarily travel the exact same distance every tick, especially on a curved path.

But the path is not predetermined. and ticktime shouldn't matter because total acummulated distance should always be the same no matter how fast or slow time goes.

if you travel at 10 pixels a second, when you have reached 100 pixels you spawn a dot.

if you travel at 25 pixels a second... when you travelled 100 pixels you spawn a dot.

Problem is probably overshoot than time? If i travelled 110 pixels it places a dot. resets and starts over, maybe i need to compensate for pissbile overshoot. My C3 example above does exactly what I want but there are some irregularites that i need to figure out where they come from.

• Overshooting is indeed the problem, as ticks are not necessarily divided evenly into seconds, and if you spawn on the object, it spawns at the object's location at the given tick you call the spawn on, rather than any set distance.

You might try having the spawning object be the last placed dot instead of the moving object. Have the previous dot spawn a new dot at a set distance towards the target. But then you might have issues with sharp turns and cutting corners so I'm not really sure...

• ## Try Construct 3

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

Construct 3 users don't see these ads
• I'm getting the overshoot ratio.

(accumulated distance-target distane) / target distance

then i get the rate of overshoot.

....

or if i spawn a new one at the old location and just move it desired distance towards the current position?

• Managed to solve exactly what I was aiming to do without complex math and adjustments. Object now spawning trail with uniform distance between the dots.

and here is c3p if anyone finds it useful.

https://www.dropbox.com/s/ks7cnrv5thu58kf/streakstest2.c3p?dl=0

• Nice! Wonder if those can be combined into a smooth line with alpha clamping...

Edit: never mind, just need to stretch a rectangle between each one. Too much playing with metaballs for me ><

• last.X+ cos(angle(object.x,object.y,last.x,last.y)*offset

last.Y+ sos(angle(object.x,object.y,last.x,last.y)*offset

Pick closest even.

• Updated the project a little bit, and removed all the junk, and added a blur effect on the trail. Pretty lightweight nice looking trails and streaks following your object. The regular particle emitter can't place the streaks at an even distance so had to make my own

Here's a link to the c3p file.

https://www.dropbox.com/s/ks7cnrv5thu58kf/streakstest2.c3p?dl=0

And Here's a Demo

http://tunepunk.com/share/streaks/

EDIT: I was aiming to use a more accurate way by using the actual distance travelled using R0j0's math, but that was too complex calculations for me, and I couldn't get an even flow of spawns. So... This will do fine.

• 13 posts