#vectors to curve

1 messages · Page 1 of 1 (latest)

spring galleon
#

simplified version by waveplayz
#1167420026291245086 message
use generateCurve(vectors, numPoints)
numPoints: number of point between each vector (vectors[i] and vectors[i+1])
vectors: array of vectors
||example:

const vec = [{x:-96 ,y:-59 ,z: 218},{x:-104,y: -59,z: 224},{x:-111,y: -59,z: 215},{x:-119,y: -59,z: 224}]
const pointes = generateCurve(vec, 20)
for(let point of pointes){
    player.dimension.spawnParticle("mba:line", point)
}
```||
code:
```js
function Vector3(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
}

function generateCurve(vectors, numPoints) {
    let curve = [];
    for (let i = 0; i < vectors.length - 1; i++) {
        for (let j = 0; j < numPoints; j++) {
            let t = j / numPoints;
            let t2 = t * t;
            let t3 = t2 * t;
            let v0 = vectors[i - 1] || vectors[i];
            let v1 = vectors[i];
            let v2 = vectors[i + 1] || vectors[i];
            let v3 = vectors[i + 2] || vectors[i + 1] || vectors[i];
            let x = 0.5 * ((2 * v1.x) + (-v0.x + v2.x) * t +
                (2 * v0.x - 5 * v1.x + 4 * v2.x - v3.x) * t2 +
                (-v0.x + 3 * v1.x - 3 * v2.x + v3.x) * t3);
            let y = 0.5 * ((2 * v1.y) + (-v0.y + v2.y) * t +
                (2 * v0.y - 5 * v1.y + 4 * v2.y - v3.y) * t2 +
                (-v0.y + 3 * v1.y - 3 * v2.y + v3.y) * t3);
            let z = 0.5 * ((2 * v1.z) + (-v0.z + v2.z) * t +
                (2 * v0.z - 5 * v1.z + 4 * v2.z - v3.z) * t2 +
                (-v0.z + 3 * v1.z - 3 * v2.z + v3.z) * t3);
            curve.push(new Vector3(x, y, z));
        }
    }
    return curve;
}
#

the bigger numPoints is the smother the curve is

#

the curve with 4000 point 🙂

open wraith
#

why here

spring galleon
open wraith
#

thats basic math

#

can find on net

wide pine
#

Very nice!

charred kiln
#

👌 very cool

charred kiln
open wraith
#

i simpilified it ```js
function cmCofs (t) {
let tt = t * t
let ttt = tt * t

let a = -ttt + 2*tt - t
let b = 3*ttt -5*tt + 2
let c = -3*ttt + 4*tt + t
let d = ttt - tt

return [ a, b, c, d ]

}

function cmCal (p0, p1, p2, p3, cofs) {
let [a,b,c,d] = cofs

return .5 * ( p0*a + p1*b + p2*c + p3*d )

}

function cm (ps, s) {
let i = Math.floor(s)
let t = s - i

let cofs = cmCofs(t)

let p1 = ps[ i ]
let p0 = ps[ i - 1 ] || p1
let p2 = ps[ i + 1 ] || p1
let p3 = ps[ i + 2 ] || p2

let x = cmCal( p0.x, p1.x, p2.x, p3.x, cofs )
let y = cmCal( p0.y, p1.y, p2.y, p3.y, cofs )
let z = cmCal( p0.z, p1.z, p2.z, p3.z, cofs )

return { x,y,z }

}


no more loops can be used with a pointer so u give a value s and give point on that time 

if we have 4 point  then 0 is point 1 and 3 is point 4 so u can can get any point on curve b/w 0 <= s <= 3
#
const ps = [
  {x:-96 ,y:-59 ,z: 218},
  {x:-104,y: -59,z: 224},
  {x:-111,y: -59,z: 215},
  {x:-119,y: -59,z: 224}
]

let step = 0.05

for(let s = 0; s <= ps.length; s += step) {
   let p = cm(ps, s)

   player
      .dimension
      .spawnParticle("mba:line", p)
}
sturdy zealot
spring galleon
#

generate only part of it and return the error

open wraith
#

game issue

#

not my

spring galleon
#

not sure it is
the returned vectors not even in the path

elfin eagle
spring galleon
#

the vectors i tested it with is loaded
my original code is working fine

open wraith
#

gI've me pack with my code

#

@spring galleon

spring galleon
#

sure
wait i need to delete some files

open wraith
#

@spring galleon how to use

spring galleon
#

just load the world
i put it in runInterval

#

and tp to location
-96 -59 218

open wraith
#
function cmCofs(t) {
    let tt = t * t
    let ttt = tt * t

    let a = -ttt + 2 * tt - t
    let b = 3 * ttt - 5 * tt + 2
    let c = -3 * ttt + 4 * tt + t
    let d = ttt - tt

    return [a, b, c, d]
}
#

i forgot 3 xd

spring galleon
spring galleon
#

also i think your script skip the first and last vec

open wraith
#

no

spring galleon
#

yeah it doesn't

elfin eagle
spring galleon
#

tried that

#

got it : )

dusk stratus
leaden pasture
#

sick

supple lion
#

at school and in internet as well

wild obsidian
#

waiting to learn cos and sin function by minecraft :PP

pallid crown