LayaBox中获取游戏的8个方向和角度

    getangleOrWay ( p1: Laya.Point, p2: Laya.Point )

    {

        // p1 开始坐标 p2结束坐标

        /**与x轴顺时针的方向 */

        /**方法1 */

        let xx = p2.x - p1.x;

        let yy = p2.y - p1.y;

        // 斜边

        let hypotenuse = Math.sqrt( Math.pow( xx, 2 ) + Math.pow( yy, 2 ) );

        // yy >= 0 弧度在于 0 到 π 之间。(0~180°)

        // yy < 0 弧度在于 π 到 2π 之间。(180°~360°)

        let rad = yy >= 0 ? ( Math.acos( xx / hypotenuse ) ) : ( Math.PI * 2 - Math.acos( xx / hypotenuse ) );/**弧度 */

        let angle = rad * 180 / Math.PI;/**角度 */

        let dir = Math.floor( rad * 4 / Math.PI );/**八个方向 */

    }

    getangleOrWay2 ( p1: Laya.Point, p2: Laya.Point )

    {

        /**与x轴顺时针的方向 */

        /**方法1 */

        let xx = p2.x - p1.x;

        let yy = p2.y - p1.y;

        /**方法2 */

        // yy >= 0 弧度在于 0 到 π 之间。(0~180°)

        // yy < 0 弧度在于 π 到 2π 之间。(180°~360°)

        let rad = yy >= 0 ? Math.atan2( yy, xx ) : ( Math.PI * 2 + Math.atan2( yy, xx ) );/**弧度 */

        let angle = Math.floor( rad * 180 / Math.PI );/**角度 */

        let dir = Math.floor( rad * 4 / Math.PI );/**八个方向 */

    }

    /**

     * 八方向的

     */

    public static getWay ( p1: Laya.Point, p2: Laya.Point ): number

    {

        /**与轴顺时针的方向 */

        var a: number = Math.atan2( p1.y - p2.y, p1.x - p2.x );

        var d: number = Math.floor( ( ( a + Math.PI * 3.625 ) % ( Math.PI * 2 ) ) / ( Math.PI / 4 ) );

        return d;

    }

    /**

     * 四方向的

     */

    public static getWayInFour ( p1: Laya.Point, p2: Laya.Point )

    {

        /**与轴顺时针的方向 */

        var a: number = Math.atan2( p2.y - p1.y, p2.x - p1.x );

        var angle: number = a * 180 / Math.PI;

        if ( angle < 0 )

        {

            angle = ( angle + 360 ) % 360;

        }

        if ( angle < 90 ) return 3;

        else if ( angle >= 90 && angle < 180 ) return 5;

        else if ( angle >= 180 && angle < 270 ) return 7;

        else return 1;

    }

  • 15
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值