2015-09-07 67 views
1

點1,說它是(0,0),我有另一個點應該在10f的距離轉身。然後,我將添加度角以使其旋轉。我可以想知道如何計算這一點,這脫困對方..如果我有起點,線的角度和長度,我如何計算線的終點?

我會用光線投影,我需要旋轉射線(順時針)來檢測碰撞

+0

那麼另一個點會像彈丸一樣移動? (對不起,我不太確定這裏發生了什麼) –

+0

我有一個演員,嗨的旋轉自己「看」的一切。然後我想知道它的視線是否與使用光線投射的身體坍塌在一起。我只有演員輪換代碼,可以有用嗎? –

+0

哦,所以你想知道一個演員是否正在觀看像它的視野那樣的光線的物體? –

回答

1

所以你說你有point1point2,都是由這裏point2將左右旋轉point110f分開一定距離,並且想知道,如果這種分離之間在某一時刻的對象相交的他們,像下面的圖片:

view

有教程來獲得旋轉一個點在互聯網上的另一個點的數學,如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:忽略這個燈具並繼續返回0:終止光線投射返回分數:將光線剪輯到此點返回1:不剪輯光線並繼續。傳遞給回調函數的Vector2實例將被重用以用於將來的調用,因此請複製它們!

**強調增加。

基本上它說你可以逐個檢查所有的交叉點,但是如果你只關心第一個,立刻返回0。當你想知道對象是否被另一個對象阻擋時,這很有用。在這種情況下,我將返回0並將point的值複製到collisionPoint,以便讓您對此值執行任何操作。

一個非常好的例子可以在this video找到。

希望你覺得這個有用。

+0

謝謝!我解決了我的問題,我只需要添加半徑現在可以工作 –

0

您應該考慮使用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有方法檢查與圓等交點也使您的身體形狀不需要是多邊形

+0

問題是這兩個向量之間的界限不能超越一些box2d的機構(牆),對不起,但我沒有說過.. –

相關問題