Easy Check Fps hit

  1. 通过简单的直线相交,判定玩家是否射击打中了敌人,在类似的射击游戏的碰撞检测中没有必要用到物理引擎,就可以通过这种方式判断是否击中的队友。

以下代码来源 smart fox server Fps Demo

// Checking if the player hits enemy using simple line intersection and 
    // the known players position and rotation angles
    private boolean checkHit(CombatPlayer player, CombatPlayer enemy) {
        if (enemy.isDead()) {
            return false;
        }

        // First of all checking the line intersection with enemy in top projection

        double radius = enemy.getCollider().getRadius();
        double height = enemy.getCollider().getHeight();
        double myAngle = player.getTransform().getRoty();
        double vertAngle = player.getTransform().getRotx();

        // Calculating an angle relatively to X axis anti-clockwise
        double normalAngle = normAngle(360 + 90 - myAngle);

        //Calculating the angle of the line between player and enemy center point
        double difx = enemy.getX() - player.getX();
        double difz = enemy.getZ() - player.getZ();

        double ang = 0;
        if (difx == 0) {
            ang = 90;
        }
        else {
            ang = Math.toDegrees(Math.atan(Math.abs(difz / difx)));
        }

        // Modifying angle depending on the quarter
        if (difx <= 0) {
            if (difz <= 0) {
                ang += 180;
            }
            else {
                ang = 180 - ang;
            }
        }
        else {
            if (difz <= 0) {
                ang = 360 - ang;
            }
        }
        ang = normAngle(ang);

        // Calculating min angle to hit
        double angDif = Math.abs(ang - normalAngle);
        double d = Math.sqrt(difx * difx + difz * difz);
        double maxDif = Math.toDegrees(Math.atan(radius / d));

        if (angDif > maxDif) {
            return false;
        }

        // Now calculating the shot in the side projection

        // Correction value to fit the model visually (as the collider may not totally fit the model height on client)
        final double heightCorrection = 0.3;

        if (vertAngle > 90) {
            vertAngle = 360 - vertAngle;
        }
        else {
            vertAngle = -vertAngle;
        }

        double h = d * Math.tan(Math.toRadians(vertAngle));
        double dif = enemy.getTransform().getY() - player.getTransform().getY() - h + heightCorrection;
        if (dif < 0 || dif > height) {
            return false;
        }

        return true;
    }

    private double normAngle(double a) {
        if (a >= 360) {
            return a - 360;
        }
        return a;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值