如果我有起点、线的角度和长度,我如何计算线的端点?

How I can calculate the endpoint of a line, if I have the starting point, the angle and length of the line?

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

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

您应该考虑使用 Intersector class 检查演员的线条是否与 body 形状相交。

要计算“视线”线的末端,请使用 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 有方法来检查与圆等的交点,所以你的 body 形状不需要是多边形

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

在互联网上有一些关于绕另一个点旋转一个点的数学教程,例如 this one,并且由于您无法指定 Vector2 的原点,因此翻译版本预览 link 到 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参数有这个文档:

Called for each fixture found in the query. You control how the ray cast proceeds by returning a float: return -1: ignore this fixture and continue return 0: terminate the ray cast return fraction: clip the ray to this point return 1: don't clip the ray and continue. The Vector2 instances passed to the callback will be reused for future calls so make a copy of them!

** 添加了重点。

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

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

希望你觉得这很有用。