입사각과 반사각(총알의 튕김,당구)

2023. 2. 9. 11:57Public/Math

https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=falcon2026&logNo=221311522279 

 

빛의 반사와 입사각 반사각에 대하여!

안녕하세요 여러분~ 홍재룡수학전문학원입니다. 오늘은 살짝 과학과 관련된 주제입니다. '빛'에 대한 이야...

blog.naver.com

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShotGunBulletPiece : Bullet
{    
    public override float Damage => 10f;

    public override BulletType m_type => BulletType.Normal;

    private Rigidbody2D rigid;
    Vector2 lastVelocity;


    Vector3 myDirection;
    public void OnCollisionEnter2D(Collision2D coll)
    {
        BoxCollider2D bulletCollider = this.GetComponent<BoxCollider2D>();
        if (coll.gameObject.tag == "Bullet")
            bulletCollider.isTrigger = true;
        else
            bulletCollider.isTrigger = false;

        //반사 사용
        //Reflect(coll);
    }
    public void OnTriggerExit2D(Collider2D collision)
    {
        BoxCollider2D bulletCollider = this.GetComponent<BoxCollider2D>();
        bulletCollider.isTrigger = false;
    }    
    public override void Initialized(SpaceShip.PlayerNumber _number, int _index)
    {
        base.Initialized(_number, _index);
    }
    public void SetNormalizeDirection(Vector3 vec)
    {
        m_direction = vec;
    }
    private void UpdateDirectionRotate()
    {
        Vector3 dir = m_direction;
        dir = m_playerNumber == SpaceShip.PlayerNumber.P2 ? dir *= -1f : m_direction;

        float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;

        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);        
    }
    protected override void Move()
    {
        this.transform.position += m_direction * Time.deltaTime * m_speed;        
    }    
    private void FixedUpdate()
    {        
        Move();
        UpdateDirectionRotate();
    }        

    //반사 사용시 
    private void Reflect(Collision2D coll)
    {
        if (coll.gameObject.tag == "OutLine")
        {
            Vector3 normalVec = coll.contacts[0].normal;
            var dir = Vector3.Reflect(m_direction, normalVec);
            m_direction = dir;


            UpdateDirectionRotate();
            
        }
    }
}