ActionScript 3.0 光线-形状相交

发布于 2024-10-21 06:59:10 字数 75 浏览 1 评论 0原文

给定一个包含矢量形状(从库加载)的 MovieClip - 有没有办法检查一条线(从点(x1,y2)到点(x2,y2)是否与该形状相交?

given a MovieClip containing a vectorshape (loaded from the library) - is there a way to check whether a line (from point(x1,y2) to point(x2,y2) intersects this shape?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2024-10-28 06:59:10

这实际上取决于形状。如果它是一个简单的几何形状(或者您可以使用一个进行碰撞),那么您可以使用网上数百种射线/形状碰撞方法之一。

如果它是一个非常具体的形状,那么您可以做的一件事就是沿着光线步进并对您的形状执行 hitTestPoint() 调用。像这样(粗略):

var ray:Point = new Point( x2 - x1, y2 - y1 ); // get the ray

// we want to check in, say 10 steps. depending on the length of the ray, you can increase/decrease this
var steps:int = 10;

// scale the ray
ray.x /= steps;
ray.y /= steps;

// step along the ray
var start:Point = new Point( x1, y1 );
for( var i:int = 0; i < steps; i++ )
{
    // hit test against the shape
    if( myShape.hitTestPoint( start.x, start.y, true ) )
    {
        // it's intersecting, do something
        break;
    }

    // it didn't intersect, keep going
    start.x += ray.x;
    start.y += ray.y;
}

It really depends on the shape. If it's a simple geometric shape (or you can use one for collision), then you can use one of the hundreds of ray/shape collision methods on the net.

If it's a very specific shape, then one of the things you can do is step along your ray and do hitTestPoint() calls on your shape. Something like this (rough):

var ray:Point = new Point( x2 - x1, y2 - y1 ); // get the ray

// we want to check in, say 10 steps. depending on the length of the ray, you can increase/decrease this
var steps:int = 10;

// scale the ray
ray.x /= steps;
ray.y /= steps;

// step along the ray
var start:Point = new Point( x1, y1 );
for( var i:int = 0; i < steps; i++ )
{
    // hit test against the shape
    if( myShape.hitTestPoint( start.x, start.y, true ) )
    {
        // it's intersecting, do something
        break;
    }

    // it didn't intersect, keep going
    start.x += ray.x;
    start.y += ray.y;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文