Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.1 kB
4
Indexable
Never
void UpdateState(Vector3 lcenter, Vector3 rcenter, float dt, ref float[] particle_state)
    {
        //attraction force1 (c1-x)/(||c1-x||)
        float dist = Mathf.Sqrt((rcenter.x - particle_state[0])*(rcenter.x - particle_state[0]) + (rcenter.y - particle_state[1])*(rcenter.y - particle_state[1]) + (rcenter.z - particle_state[2])*(rcenter.z - particle_state[2]));
        //float dist = Mathf.Sqrt(Math.Pow((float)(rcenter.x-particle_state[0]), 2.0f) + Mathf.Pow((float)(rcenter.y-particle_state[1]), 2.0f) + Mathf.Pow((float)(rcenter.z-particle_state[2]), 2.0f));
        Vector3 force1 = new Vector3(((rcenter.x-particle_state[0])/dist), ((rcenter.y-particle_state[1])/dist), ((rcenter.z-particle_state[2])/dist));

        //attraction force2
        float dist2 = Mathf.Sqrt((lcenter.x - particle_state[0])*(lcenter.x - particle_state[0]) + (lcenter.y - particle_state[1])*(lcenter.y - particle_state[1]) + (lcenter.z - particle_state[2])*(lcenter.z - particle_state[2]));
        //float dist2 = Mathf.Sqrt(Math.Pow((lcenter.x-particle_state[0]), 2.0f) + Mathf.Pow((lcenter.y-particle_state[1]), 2.0f) + Mathf.Pow((lcenter.z-particle_state[2]), 2.0f));
        Vector3 force2 = new Vector3(((lcenter.x-particle_state[0])/dist2), ((lcenter.y-particle_state[1])/dist2), ((lcenter.z-particle_state[2])/dist2));

        //linear drag function
        Vector3 drag = -0.012f*new Vector3(particle_state[3], particle_state[4], particle_state[5]);
        string name = transform.gameObject.name;
        Vector3 allforce = force1 + force2;
        if (name.Contains("bad")){

        }else{
            allforce += drag;
        }
        allforce /= mass; 

        //so all force is now the derivative of v, to get v, must integrate allforce (v') this is part c
        //we can use forward euler's
        particle_state[3] += allforce.x * dt;
        particle_state[4] += allforce.y * dt;
        particle_state[5] += allforce.z * dt;
        
        //part d
        for (int d = 0; d < 3; d++){
            particle_state[d] += particle_state[d+3] * dt;
        } 
    }
Leave a Comment