【问题标题】:How I can calculate the endpoint of a line, if I have the starting point, the angle and length of the line?如果我有起点、直线的角度和长度,我如何计算直线的终点?
【发布时间】:2015-09-07 14:17:50
【问题描述】:

点 1,假设它是 (0, 0),我还有另一个点应该在 10f 的距离处转身。然后我将添加度角以使其旋转。我想知道如何计算这一点,这相互转身..

我将使用光线投射,我需要旋转光线(顺时针)以检测碰撞

【问题讨论】:

  • 所以另一点会像弹丸一样移动? (抱歉,我不太清楚这里发生了什么)
  • 我有一个演员,他在自己周围旋转“观看”一切。然后我想知道视线它是否正在使用光线投射与身体折叠。我只有演员轮换的代码,有用吗?
  • 哦,所以你想知道一个演员是否正在观看一个像它的视野一样具有这种光的物体?
  • 没错,这就是我想要的

标签: libgdx box2d raycasting


【解决方案1】:

所以你说你有point1point2,两者之间的距离为10f,其中point2 将围绕point1 旋转,并且你想知道在这种分离之间是否存在某个对象点与它们相交,如下图所示:

有一些教程可以在互联网上获得围绕另一个点旋转的数学知识,例如 this one,并且由于您无法指定 Vector2 的原点,因此预览中建议的代码的翻译版本指向 java 的链接应该类似于:

public Vector2 rotatePoint(Vector2 center, Vector2 point, float angle) {
    angle = angle * MathUtils.degreesToRadians; // Convert to radians
    float rotatedX = MathUtils.cos(angle) * (point.x - center.x) 
            - MathUtils.sin(angle) * (point.y - center.y) + center.x;
    float rotatedY = MathUtils.sin(angle) * (point.x - center.x) 
            + MathUtils.cos(angle) * (point.y - center.y) + center.y;

    // rotated new position:
    return new Vector2(rotatedX, rotatedY);
}

至于剩下的代码(对象之间的交集),我猜你在找RayCastCallback接口:

// initial position
Vector2 point1 = new Vector(0, 0);
// Max lenght of view
Vector2 point2 = new Vector(0, 10);
// Position of collision if occur
final Vector2 collisionPoint = new Vector();

@Override
public void render(float delta) {
    //...
    point2 = rotatePoint(point1, point2, 10); // rotate 10º
    // to detect if object at position point1 is seeing something
    world.rayCast(new RayCastCallback(){
        @Override
        public float reportRayFixture(Fixture fixture,  Vector2 point, 
                 Vector2 normal, float fraction) {
            // what do the object saw?     -> fixture
            // where do the object saw it? -> point

            collisionPoint.set(point);

            return 0; // <- return 0 to stop raycasting
        }
    }, point1, point2);
    //...  rotation and other stuffs...
}

reportRayFixture的返回参数有这个文档:

为查询中找到的每个夹具调用。您可以通过返回一个浮点数来控制光线投射的进行方式: return -1:忽略此夹具并继续 return 0:终止光线投射 return fraction:将光线剪辑到这一点 return 1:不要剪辑射线并继续。传递给回调的 Vector2 实例将被重用于未来的调用,因此请复制它们!

** 已添加重点。

基本上它说你可以一个一个地检查所有的交叉点,但是如果你只关心第一个,立即返回 0。当您想知道一个对象是否被另一个对象阻挡时,这很有用。在这种情况下,我返回 0 并将 point 的值复制到 collisionPoint 以让您对这个值做任何您想做的事情。

一个很好的例子可以在this video找到。

希望你觉得这很有用。

【讨论】:

  • 谢谢!我用这个解决了我的问题,我只需要添加半径就可以了
【解决方案2】:

您应该考虑使用Intersector 类来检查您的演员的线条是否与身体形状相交。

要计算“视线”线的末端,请使用 Vector2,您将根据您的演员旋转来旋转(这实际上是您问题的答案)

它应该看起来像:

    Vector2 sightVector = new Vector2(10f, 0); //the 10f is actually your sight max distance
    sightVector.rotate(actor.getRotation());
    
    ...
    
    @Override
    pblic void render(float delta) //it can be also act of the actor
    {
        sightVector.rotate(actor.getRotation());
    
        Vector2 endOfLine = new Vector2(actor.getX() + sightVector.x, actor.getY() + sightVector.y); //here you are calculating the end of line
    
        Polygon bodyShape = getBodyShape( theBody ); //you should create a method that will return your body shape
        
        if( Intersector.intersectLinePolygon(new Vector2(actor.getX(), actor.getY()), endOfLine, bodyShape) )
        {
            //do something
        }
            
        ...
        
    }

Intersector 具有检查与圆等相交的方法,因此您的身体形状不需要是多边形

【讨论】:

  • 问题是这两个向量之间的线不能越过一些box2d物体(墙壁),对不起,我之前没说..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 2021-07-20
  • 2011-11-27
  • 1970-01-01
  • 2012-12-27
相关资源
最近更新 更多