Exemple #1
0
    void Update()
    {
        // Apply Gravity
        if (this.transform.position.y > 0)
        {
            velocity = VectorLibrary.addVectors(velocity, VectorLibrary.getScalarMultiple(gravity, Time.deltaTime));
        }

        if (deccelarate)
        {
            // Get direction
            if (!(Mathf.Abs(velocity.x) < 0.1f && Mathf.Abs(velocity.z) < 0.1f))
            {
                direction = VectorLibrary.getUnitDirection(velocity, VectorLibrary.zeroVector());
            }
            // Apply deccelaration
            deccelaration = VectorLibrary.getScalarMultiple(-direction, frictionFactor);

            // Calculate new Velocity: v = v_old + a * deltaT
            velocity = VectorLibrary.addVectors(velocity, VectorLibrary.getScalarMultiple(-deccelaration, Time.deltaTime));
        }
        // p = p_old + vel * deltaT
        Vector3 velTimesdeltaT = VectorLibrary.getScalarMultiple(velocity, Time.deltaTime);

        this.transform.position = VectorLibrary.addVectors(this.transform.position, velTimesdeltaT);

        // When ground is hit
        if (this.transform.position.y < 0)
        {
            if (velocity.y < 0)
            {
                // H = e*h -1 So it goes up again
                velocity.y = velocity.y * e * -1;
            }

            if (velocity.y < 0.1f)
            {
                velocity    = new Vector3(velocity.x, 0, 0);
                deccelarate = true;
            }
            if (Mathf.Abs(velocity.x) < 0.1f && Mathf.Abs(velocity.z) < 0.1f)
            {
                velocity = new Vector3(0.0f, 0.0f, 0.00f);
            }
        }
    }
Exemple #2
0
    void FixedUpdate()
    {
        direction = VectorLibrary.getUnitDirection(velocity, VectorLibrary.zeroVector());

        // Apply deccelaration
        deccelaration = VectorLibrary.getScalarMultiple(-direction, frictionFactor);

        // Calculate new Velocity: v = v_old + a * deltaT
        velocity = VectorLibrary.addVectors(velocity, VectorLibrary.getScalarMultiple(-deccelaration, Time.deltaTime));

        // Calculate new Position: p = p_old + v * deltaT
        position = VectorLibrary.addVectors(this.transform.position, VectorLibrary.getScalarMultiple(velocity, Time.deltaTime));

        // Bounce Back the Cue Ball from boundaries
        boundaryCollision();

        // Ball Collisions
        ballCollision();

        // Assign new position to the current object's position
        this.transform.position = position;
    }