Example #1
0
    // Use this for initialization
    void Awake()
    {
        _enemy = GetComponent <Enemy>();

        _rb2d  = GetComponent <Rigidbody2D>();
        _mover = GetComponent <PhysicsMover>();
    }
Example #2
0
    // Use this for initialization

    void Awake()
    {
        _seed  = Random.Range(0, 100f);
        _enemy = GetComponent <Enemy>();
        _rb2d  = GetComponent <Rigidbody2D>();
        _mover = GetComponent <PhysicsMover>();
    }
Example #3
0
    // Use this for initialization

    /// <summary>
    /// Awake is called when the script instance is being loaded.
    /// </summary>
    void Awake()
    {
        _jumpTimer = Random.Range(0, JumpDelay);
        _enemy     = GetComponent <Enemy>();
        _rb2d      = GetComponent <Rigidbody2D>();
        _mover     = GetComponent <PhysicsMover>();
        NextJump   = transform.position;
    }
Example #4
0
 public void UnRegisterMover(PhysicsMover mover)
 {
     if (!PhysicsMovers.Contains(mover))
     {
         return;
     }
     PhysicsMovers.Remove(mover);
     OnPhysicsMoverUnRegistered?.Invoke(mover);
 }
Example #5
0
 public void RegisterMover(PhysicsMover mover)
 {
     if (PhysicsMovers.Contains(mover))
     {
         return;
     }
     PhysicsMovers.Add(mover);
     OnPhysicsMoverRegistered?.Invoke(mover);
 }
Example #6
0
 void Start()
 {
     _camera   = Camera.main;
     _shooter  = GetComponent <Shooter>();
     _mover    = GetComponent <PhysicsMover>();
     _renderer = GetComponent <SpriteRenderer>();
     _spawner  = GameObject.FindObjectOfType <Spawner>();
     AddScore(0);
 }
Example #7
0
 /// <summary>
 /// Awake is called when the script instance is being loaded.
 /// </summary>
 void Awake()
 {
     AllowedToMatch = false;
     Health         = MaxHealth;
     _rb2d          = GetComponent <Rigidbody2D>();
     _mover         = GetComponent <PhysicsMover>();
     _audio         = GetComponent <AudioSource>();
     _renderer      = GetComponent <SpriteRenderer>();
 }
Example #8
0
    void Start()
    {
        mover           = new PhysicsMover();
        mover.transform = transform;

        float s = 0.35f;
        // .7 wide 1.4 tall
        // so 2 blocks wide, 3 tall
        AABB shape;

        shape.minX  = -s;
        shape.minZ  = -s;
        shape.maxX  = s;
        shape.maxZ  = s;
        shape.minY  = -1.0f;
        shape.maxY  = 0.4f;
        mover.shape = shape;

        BlonkPhysics.AddMover(mover);

        ToggleFlyMode();
    }
Example #9
0
    void FixedUpdate()
    {
        for (int moverIndex = 0; moverIndex < movers.Count; ++moverIndex)
        {
            PhysicsMover mover = movers[moverIndex];
            if (!mover.simulate)
            {
                continue;
            }
            mover.pos = mover.transform.position;
            if (mover.obeysGravity)
            {
                mover.vel += gravity * Time.deltaTime;
            }

            // loop thru and build list of aabbs with all possible blocks mover could collide with this frame
            // take into account velocity
            AABB swept = AABB.GetSwept(mover.GetWorldAABB(), mover.vel * Time.deltaTime);
            // cast min and max to block positions and add all nearby block AABBs to boxes list
            const float bs    = 0.01f;
            Vector3i    minBP = WorldUtils.GetBlockPos(new Vector3(swept.minX - bs, swept.minY - bs, swept.minZ - bs));
            Vector3i    maxBP = WorldUtils.GetBlockPos(new Vector3(swept.maxX + bs, swept.maxY + bs, swept.maxZ + bs));
            Assert.IsTrue(maxBP.x >= minBP.x && maxBP.y >= minBP.y && maxBP.z >= minBP.z);
            boxes.Clear();
            for (int y = minBP.y; y <= maxBP.y; ++y)
            {
                for (int z = minBP.z; z <= maxBP.z; ++z)
                {
                    for (int x = minBP.x; x <= maxBP.x; ++x)
                    {
                        if (world.GetBlock(x, y, z).ColliderSolid())
                        {
                            AABB b;
                            b.minX = x * Chunk.BLOCK_SIZE;
                            b.minY = y * Chunk.BLOCK_SIZE;
                            b.minZ = z * Chunk.BLOCK_SIZE;
                            b.maxX = (x + 1) * Chunk.BLOCK_SIZE;
                            b.maxY = (y + 1) * Chunk.BLOCK_SIZE;
                            b.maxZ = (z + 1) * Chunk.BLOCK_SIZE;
                            boxes.Add(b);
                        }
                    }
                }
            }

            // do a sweeptest against each one and find closest time of collision if any
            // collide against that and zero out velocity on that axis (or bounce)
            // keep going until remainingDelta is very small
            int       loopCount      = 0;
            const int maxLoops       = 10;
            float     remainingDelta = Time.deltaTime; // other game is hardcoded 0.5 because 20 ticks per second i think
            while (remainingDelta > 1e-10f && ++loopCount < maxLoops)
            {
                // find nearest collision out of all nearby blocks
                float nearestTime = 1.0f; // works worse if set to remainingDelta like minetest...
                //int nearestIndex = -1; // will prob need this later for stuff
                int nearestAxis = -1;
                for (int i = 0; i < boxes.Count; ++i)
                {
                    AABB box = boxes[i];

                    //int axis = AABB.SweepTest2(mover.GetWorldAABB(), box, mover.vel * remainingDelta, out float t);
                    int axis = AABB.SweepTest(mover.GetWorldAABB(), box, mover.vel * remainingDelta, out float t);

                    if (axis == -1 || t >= nearestTime)
                    {
                        continue;
                    }
                    //Assert.IsTrue(t <= 1.0f && t >= 0.0f);
                    nearestTime = t;
                    //nearestIndex = i;
                    nearestAxis = axis;
                }

                if (nearestAxis == -1)   // no collision
                {
                    mover.pos     += mover.vel * remainingDelta;
                    remainingDelta = 0;
                    break;
                }
                else     // collision!

                {
                    if (nearestTime < 0)   // handle negative nearest caused by d allowance
                    {
                        if (nearestAxis == 0)
                        {
                            mover.pos.x += mover.vel.x * nearestTime * remainingDelta;
                        }
                        else if (nearestAxis == 1)
                        {
                            mover.pos.y += mover.vel.y * nearestTime * remainingDelta;
                        }
                        else if (nearestAxis == 2)
                        {
                            mover.pos.z += mover.vel.z * nearestTime * remainingDelta;
                        }
                    }
                    else
                    {
                        // move to the point of collision and reduce time remaining
                        mover.pos      += mover.vel * nearestTime * remainingDelta;
                        remainingDelta -= nearestTime * remainingDelta;
                    }

                    // zero out velocity on collided axis
                    if (nearestAxis == 0)
                    {
                        mover.vel.x = 0;
                        //Debug.Log("hit x " + remainingDelta);
                    }
                    else if (nearestAxis == 1)
                    {
                        mover.vel.y = 0;
                        //Debug.Log("hit y " + remainingDelta);
                    }
                    else if (nearestAxis == 2)
                    {
                        mover.vel.z = 0;
                        //Debug.Log("hit z " + remainingDelta);
                    }
                }
            }

            // lastly, now check if mover is grounded
            AABB moverBox = mover.GetWorldAABB();
            mover.grounded = false;
            for (int i = 0; i < boxes.Count; ++i)
            {
                AABB box = boxes[i];

                if (box.maxX - AABB.d > moverBox.minX && box.minX + AABB.d < moverBox.maxX &&
                    box.maxZ - AABB.d > moverBox.minZ && box.minZ + AABB.d < moverBox.maxZ)
                {
                    if (Mathf.Abs(box.maxY - moverBox.minY) < 0.1f)
                    {
                        mover.grounded = true;
                    }
                }
            }

            if (loopCount >= maxLoops)
            {
                Debug.LogWarning("physics loop count exceeded!");
            }

            mover.transform.position = mover.pos; // update transform
        }
    }
Example #10
0
 // add object to physics simulation
 public static void AddMover(PhysicsMover mover)
 {
     movers.Add(mover);
 }
 // Start is called before the first frame update
 void Start()
 {
     PhysicsMover = GetComponent <PhysicsMover>();
     Turtle       = this.GetComponentInParent <Parametric_L_System>().Turtle;
 }
Example #12
0
 private void Awake()
 {
     physicsMover = gameObject.GetComponentInChildren <PhysicsMover>();
     physicsMover.MoverController = this;
     SetupMover(physicsMover);
 }