Beispiel #1
0
 public VerletConstraint(VerletPoint _p0, VerletPoint _p1, float _minDist, float _maxDist)
 {
     p0      = _p0;
     p1      = _p1;
     minDist = _minDist;
     maxDist = _maxDist;
 }
    void Start()
    {
        _balls = new List <GameObject>();

        _points = new List <VerletPoint>();
        _sticks = new List <VerletStick>();

        _prefab.gameObject.SetActive(false);

        for (int i = 0; i < _num; i++)
        {
            var g = Instantiate(_prefab, transform, false);
            g.gameObject.SetActive(true);
            g.transform.position = new Vector3(
                0, (float)i * 0.4f, 0
                );
            _balls.Add(g);

            var p = new VerletPoint(g.transform.position);
            _points.Add(p);
        }

        for (int i = 0; i < _num - 1; i++)
        {
            var s = new VerletStick(
                _points[i + 0],
                _points[i + 1],
                0.4f,
                0.1f
                );
            _sticks.Add(s);
        }

        //verlt pointをつくる
    }
 public VerletStick(
     VerletPoint p0, VerletPoint p1, float length, float elasticity
     )
 {
     _point0     = p0;
     _point1     = p1;
     _length     = length;
     _elasticity = elasticity;
 }
    void ghostCollisionResolution(VerletPoint a, VerletPoint b)
    {
        if (LineSegmentsIntersect(new Vector2(a.xnext, a.ynext), new Vector2(b.xnext, b.ynext), boundBox.a, boundBox.b)) //intersect on left side
        {
            normal = new Vector2(-(boundBox.b.y - boundBox.a.y), boundBox.b.x - boundBox.a.x);
            normal.Normalize();

            if (a.xnext < b.xnext) //then point2.x must be changed
            {
                b.xnext = pn.xMin - 0.01f;
            }
            else
            {
                a.xnext = pn.xMin - 0.01f;
            }
        }
        if (LineSegmentsIntersect(new Vector2(a.xnext, a.ynext), new Vector2(b.xnext, b.ynext), boundBox.b, boundBox.c)) //intersect on top
        {
            if (a.ynext < pn.yMax && b.ynext < pn.yMax)
            {
                a.ynext = pn.yMax + 0.01f;
                b.ynext = pn.yMax + 0.01f;
            }

            else if (a.ynext < b.ynext)
            {
                a.ynext = pn.yMax + 0.01f;
            }
            else
            {
                b.ynext = pn.yMax + 0.01f;
            }
        }
        if (LineSegmentsIntersect(new Vector2(a.xnext, a.ynext), new Vector2(b.xnext, b.ynext), boundBox.c, boundBox.d)) //intersect on right


        {
            if (a.xnext < b.xnext)
            {
                a.xnext = pn.xMax + 0.01f;
            }
            else
            {
                b.xnext = pn.xMax + 0.01f;
            }
        }
    }
Beispiel #5
0
 void GeneratePoints()
 {
     points = new VerletPoint[sizeX * sizeY];
     for (int i = 0, y = 0; y < sizeY; y++)
     {
         for (int x = 0; x < sizeX; x++, i++)
         {
             // Vector3 np = new Vector3( (float)x / sizeX, (float)y / sizeY, 0 );
             // Vector3 pointAlongTop = Vector3.Lerp( topLeftControlPoint.position, topRightControlPoint.position, np.x );
             // Vector3 pointAlongBottom = Vector3.Lerp( bottomLeftControlPoint.position, bottomRightControlPoint.position, np.x );
             // Vector3 pos = Vector3.Lerp( pointAlongBottom, pointAlongTop, np.y );
             // var p = new VerletPoint( new Vector3( pos.x, pos.y, pos.z ) );
             var p = new VerletPoint(new Vector3(x, y, 0f));
             points[x + sizeX * y] = p;
         }
     }
 }
    void collisionWithCannonballs(VerletPoint p)
    {
        GameObject cbtoRemove = new GameObject();

        foreach (GameObject cb in cannon.cannonballs)
        {
            if (Vector2.Distance(new Vector2(cb.transform.position.x, cb.transform.position.y), new Vector2(p.xnext, p.ynext)) <= 0.5f)
            {
                //collision detected
                p.xnext = p.xcurr + Time.deltaTime * cb.GetComponent <CannonBall>().vix;
                p.ynext = p.ycurr + Time.deltaTime * cb.GetComponent <CannonBall>().viy;

                cbtoRemove = cb;
                break;
            }
        }

        if (cbtoRemove != null)
        {
            cannon.cannonballs.Remove(cbtoRemove);
            Destroy(cbtoRemove);
        }
    }
Beispiel #7
0
 public Stick(VerletPoint p0, VerletPoint p1)
 {
     this.p0  = p0;
     this.p1  = p1;
     distance = (p0.current - p1.current).magnitude;
 }
Beispiel #8
0
    private void generateGeometry(float scale, Vector3 vel)
    {
        float   minX      = CannonGame.wallWidth + CannonGame.wallStart + scale * 1.5f;
        float   maxX      = CannonGame.terrainStart - scale;
        Vector3 anchorPos = new Vector3(Random.Range(minX, maxX), scale * 1.65f, 0);

        anchor = new VerletPoint(anchorPos, vel, true, false);

        //temp storage
        Vector3          p;
        VerletPoint      vP;
        VerletConstraint vC;

        //create round part of body
        float theta    = Mathf.PI * 0.75f;
        float thetaEnd = -Mathf.PI * 0.5f;
        float delta    = (thetaEnd - theta) / 9.0f;

        while (theta >= thetaEnd)
        {
            p  = new Vector3(scale * Mathf.Cos(theta) + anchor.pos.x, scale * Mathf.Sin(theta) + anchor.pos.y);
            vP = new VerletPoint(p, vel, false, true);
            points.Add(vP);

            vC = new VerletConstraint(vP, anchor, scale - scale * 0.05f, scale + scale * 0.05f);
            constraints.Add(vC);
            theta += delta;
        }

        //Create flat bottom of body
        float       dist = scale / 3;
        VerletPoint last = points[points.Count - 1];

        rLegStartIdx = points.Count;
        lLegStartIdx = points.Count + 1;
        for (int i = 0; i < 3; i++)
        {
            p    = last.pos;
            p.x -= dist;
            vP   = new VerletPoint(p, vel, false, true);
            points.Add(vP);

            vC = new VerletConstraint(vP, last, dist, dist);
            constraints.Add(vC);
            last = vP;
        }

        //Create chest
        p    = last.pos;
        p.x -= scale * 0.55f;
        p.y += scale * 0.75f;
        dist = (last.pos - p).magnitude;
        vP   = new VerletPoint(p, vel, false, true);
        points.Add(vP);

        vC = new VerletConstraint(vP, last, dist, dist);
        constraints.Add(vC);
        last = vP;

        // Create left part of neck
        p    = last.pos;
        dist = scale * 0.55f;
        p.y += dist;
        vP   = new VerletPoint(p, vel, false, true);
        points.Add(vP);

        vC = new VerletConstraint(vP, last, dist, dist);
        constraints.Add(vC);
        last = vP;

        // Create wottle
        p    = last.pos;
        p.x -= 0.075f * scale;
        p.y -= 0.65f * scale;
        dist = (last.pos - p).magnitude;
        vP   = new VerletPoint(p, vel, false, true);
        points.Add(vP);
        vC = new VerletConstraint(vP, last, dist, dist);
        constraints.Add(vC);
        last = vP;

        p    = last.pos;
        p.x -= 0.075f * scale;
        p.y += 0.675f * scale;
        dist = (last.pos - p).magnitude;
        vP   = new VerletPoint(p, vel, false, true);
        points.Add(vP);
        vC = new VerletConstraint(vP, last, dist, dist);
        constraints.Add(vC);
        last = vP;

        // Create beak
        p    = last.pos;
        p.x -= 0.25f * scale;
        p.y += 0.075f * scale;
        dist = (last.pos - p).magnitude;
        vP   = new VerletPoint(p, vel, false, true);
        points.Add(vP);
        vC = new VerletConstraint(vP, last, dist, dist);
        constraints.Add(vC);
        last = vP;
        p    = last.pos;
        p.x += 0.35f * scale;
        p.y += 0.25f * scale;
        dist = (last.pos - p).magnitude;
        vP   = new VerletPoint(p, vel, false, true);
        points.Add(vP);
        vC = new VerletConstraint(vP, last, dist, dist);
        constraints.Add(vC);
        last = vP;


        // Top of head
        p    = last.pos;
        p.x += 0.15f * scale;
        dist = (last.pos - p).magnitude;
        vP   = new VerletPoint(p, vel, false, true);
        points.Add(vP);
        vC = new VerletConstraint(vP, last, dist, dist);
        constraints.Add(vC);
        last = vP;

        // Right of neck
        p    = last.pos;
        p.x += 0.05f * scale;
        p.y -= 0.55f * scale;
        dist = (last.pos - p).magnitude;
        vP   = new VerletPoint(p, vel, false, true);
        points.Add(vP);
        vC = new VerletConstraint(vP, last, dist, dist);
        constraints.Add(vC);
        vC = new VerletConstraint(points[0], vP, dist, dist);
        constraints.Add(vC);

        bodyVertexCount = points.Count;
        // Left leg;
        last = points[lLegStartIdx];
        p    = last.pos;
        p.y -= scale * 0.35f;
        dist = (last.pos - p).magnitude;
        vP   = new VerletPoint(p, vel, false, true);
        points.Add(vP);
        vC = new VerletConstraint(vP, last, dist, dist);
        constraints.Add(vC);
        last = vP;

        p    = last.pos;
        p.y -= scale * 0.35f;
        p.x -= scale * 0.2f;
        dist = (last.pos - p).magnitude;
        vP   = new VerletPoint(p, vel, false, true);
        points.Add(vP);
        vC = new VerletConstraint(vP, last, dist, dist);
        constraints.Add(vC);
        vC = new VerletConstraint(points[lLegStartIdx], vP, dist, dist);
        constraints.Add(vC);
        last = vP;

        dist = (points[lLegStartIdx].pos - vP.pos).magnitude;
        vC   = new VerletConstraint(points[lLegStartIdx], vP, dist, dist);
        constraints.Add(vC);
        legVertexCount = points.Count - bodyVertexCount;

        // Right leg;
        last = points[rLegStartIdx];
        p    = last.pos;
        p.y -= scale * 0.35f;
        dist = (last.pos - p).magnitude;
        vP   = new VerletPoint(p, vel, false, true);
        points.Add(vP);
        vC = new VerletConstraint(vP, last, dist, dist);
        constraints.Add(vC);
        last = vP;

        p    = last.pos;
        p.y -= scale * 0.35f;
        p.x -= scale * 0.2f;
        dist = (last.pos - p).magnitude;
        vP   = new VerletPoint(p, vel, false, true);
        points.Add(vP);
        vC = new VerletConstraint(vP, last, dist, dist);
        constraints.Add(vC);
        last = vP;

        dist = (points[lLegStartIdx].pos - vP.pos).magnitude;
        vC   = new VerletConstraint(points[rLegStartIdx], vP, dist, dist);
        constraints.Add(vC);
    }