Jasmine Roberts - MIT Fab Lab
using UnityEngine;

public class CantileverBeam {

    int nodeCount = 2;

    GameObject origin;

    public Node[] nodes;

    /// 
    /// 
    /// 
    /// Gameobject used as origin
    /// Node count
    public CantileverBeam(GameObject o, int nc)
    {
        origin = o;
        nodeCount = nc;
        nodes = new Node[nodeCount];
    }

    public struct Node
    {
        public Vector3 position;
    }

    /// 
    /// Updates each node with new parameters.
    /// 
    /// Force
    /// Span (Lenght)
    /// Elasticity
    /// Inertia
    /// Multiplier for both the x and y axis
    public void UpdateNodes(float force, float span, float elasticity, float inertia, float multiplier)
    {
        for (int i = 0; i < nodes.Length; i++)
        {
            Vector3 nodePos = ProcessNode(i, force, span, elasticity, inertia, multiplier);

            nodes[i].position = nodePos;

            nodes[i].position = origin.transform.rotation * nodes[i].position + origin.transform.position;
        }
    }

    Vector3 ProcessNode(int _nodeIndex, float f, float s, float e, float i, float m)
    {
        Vector3 deflection = Vector3.zero;
        float x = _nodeIndex * (s / (nodes.Length - 1));

        deflection.y = ((f * Mathf.Pow(x, 2)) / (6 * e * i)) * (3 * s - x) * m;
        deflection.x = x * m;

        return deflection;
    }

    /// 
    /// Sets the number of nodes
    /// 
    /// 
    public void SetNodeCount(int count)
    {
        nodes = new Node[count];
    }

    public int NodeCount
    {
        get { return nodes.Length; }
    }

    public Vector3 GetStartNode()
    {
        return nodes[0].position;
    }

    public Vector3 GetEndNode()
    {
        return nodes[nodes.Length - 1].position;
    }

    public Vector3 GetNode(int index)
    {
        return nodes[index].position;
    }

}