【问题标题】:As3: Get exact point of collision using hitTestPoint?As3:使用 hitTestPoint 获取准确的碰撞点?
【发布时间】:2014-03-12 01:23:00
【问题描述】:

我正在使用 hitTestPoint 来检测敌人和墙壁之间的碰撞。

我想要这样,如果敌人接触到墙壁,他的新目标方向应该更改为指向墙壁相反方向的点。要做到这一点,我想我需要知道敌人和墙壁碰撞的确切位置。

主要运动系统:

    private function move(event:Event):void
    {

        var dx = target.x - x;
        var dy = target.y - y;
        var angle = Math.atan2(dy, dx)/Math.PI*180;
        rotation = angle;


        this.x = x+Math.cos(rotation/180*Math.PI)*movementSpeed;
        this.y = y+Math.sin(rotation/180*Math.PI)*movementSpeed;

        var hyp = Math.sqrt((dx*dx)+(dy*dy));

        if(hyp <5)
        {
            target.x = Math.floor(Math.random() * (1750 - 50 + 1) + 50);
            target.y = Math.floor(Math.random() * (850 - 50 + 1) + 50);
        }
    }

还有墙壁检测系统:

        while (_root.wall.hitTestPoint(this.x, this.y+radius, true)) 
        {
            this.y--;
            target.y = Math.floor(Math.random() * (850 - 50 + 1) + 50);
        }

        while (_root.wall.hitTestPoint(this.x, this.y-radius, true))
        {
            this.y++;
            target.y = Math.floor(Math.random() * (850 - 50 + 1) + 50);
        }

        while (_root.wall.hitTestPoint(this.x-radius, this.y, true)) 
        {
            this.x++;
            target.x = Math.floor(Math.random() * (1750 - 50 + 1) + 50);
        }

        while (_root.wall.hitTestPoint(this.x+radius, this.y, true)) 
        {
            this.x--;
            target.x = Math.floor(Math.random() * (1750 - 50 + 1) + 50);
        }

“_root.wall”是一个带有一堆不同矢量矩形的影片剪辑。

谢谢

【问题讨论】:

    标签: actionscript-3 collision point hittest direction


    【解决方案1】:

    通过查看您的代码,您的敌人似乎总是根据他的轮换“向前”行走。这对你有利,因为当他走进墙壁时,墙壁的反方向将与敌人的旋转方向相反。

    soo,每当他撞到墙时,您可以通过类似于以下的方式更新目标位置: (可能不准确,因为 as3 轮换处理的是负数等等)

    var range:int = int(Math.random()*maxDistance);
    var r:Number = rotation/180*Math.PI + Math.PI;
    target.y = Math.sin(r) * (minDistance + range);
    target.x = Math.cos(r) * (minDistance + range);
    

    另外,如果您不希望它直接与墙相对,您可以为角度本身添加一些随机化:

    var range:int = int(Math.random()*maxDistance);
    var sign:int = (Math.random() > .5) ? 1 : -1;
    var r:Number = rotation/180*Math.PI + Math.PI + ((Math.random() > .5)? 1 : -1)*(Math.random()*Math.PI)/4;
    target.y = Math.sin(r) * (minDistance + range);
    target.x = Math.cos(r) * (minDistance + range);
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-03-12
      • 2013-11-22
      • 2018-06-27
      • 2012-09-05
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多