Home > Back-end >  I would like to simulate gravitation between particles, but I think I forgot something?
I would like to simulate gravitation between particles, but I think I forgot something?

Time:07-29

So, for each star, i compare this one to all other stars to calculate his speed, velocity, etc. But that didn't work, I'm not too strong in maths and I think my formula is maybe wrong? idk why that didn't work here my code :

//for each star I compare to all other stars
for(let i = 0; i < pos.length; i   ) {
    for (let j = 0; j < pos.length; j   ){
        if (i !== j){
            // Formula part
            const vector = compute_interaction(pos[i], pos[j], 1.0);
            accelerations[i].x  = vector.x;
            accelerations[i].y  = vector.y;
            accelerations[i].z  = vector.z;
            break;
        }
    }

}
for (let i = 0 ; i<accelerations.length ; i  ){
    speedStars[i].x  = accelerations[i].x * 0.001;
    speedStars[i].y  = accelerations[i].y * 0.001;
    speedStars[i].z  = accelerations[i].z * 0.001;
}
for (let i = 0 ; i<speedStars.length ; i  ){
    const i3 = i*3;
    starsPositions[i3]  = speedStars[i].x * 0.001;
    starsPositions[i3   1]  = speedStars[i].y * 0.001;
    starsPositions[i3   2]  = speedStars[i].z * 0.001;
}

function compute_interaction(currentPosition, positionOtherStar, smoothing_length)
{
    const vector = new THREE.Vector3(positionOtherStar.x - currentPosition.x, positionOtherStar.y - currentPosition.y, positionOtherStar.z - currentPosition.z).normalize();
    let x = vector.x / (Math.pow(positionOtherStar.x,2.0) - Math.pow(currentPosition.x,2.0)  smoothing_length)
    let y = vector.y / (Math.pow(positionOtherStar.y,2.0) - Math.pow(currentPosition.y,2.0)  smoothing_length)
    let z = vector.z / (Math.pow(positionOtherStar.z,2.0) - Math.pow(currentPosition.z,2.0)  smoothing_length)
    return new THREE.Vector3(x, y, z);
}

Here the CodePen: enter image description here

CodePudding user response:

Probably, the compute_interaction() function should be:

function compute_interaction(currentPosition, positionOtherStar, smoothing_length)
{
    //const vector = new THREE.Vector3(positionOtherStar.x - currentPosition.x, positionOtherStar.y - currentPosition.y, positionOtherStar.z - currentPosition.z).normalize();
    //let x = vector.x / (Math.pow(positionOtherStar.x,2.0) - Math.pow(currentPosition.x,2.0)  smoothing_length)
    //let y = vector.y / (Math.pow(positionOtherStar.y,2.0) - Math.pow(currentPosition.y,2.0)  smoothing_length)
    //let z = vector.z / (Math.pow(positionOtherStar.z,2.0) - Math.pow(currentPosition.z,2.0)  smoothing_length)
    //return new THREE.Vector3(x, y, z);

    const vector = new THREE.Vector3().subVectors(positionOtherStar, currentPosition);
    return vector.normalize().divideScalar(vector.lengthSq()   smoothing_length);
}
  • Related