2011-09-07 9 views
1

문제는 Android 경로 모양과 관련됩니다. 그것은 화면 Canvas의 객체를 가리키는 화살표로 사용하는 삼각형입니다. 이것은 2d 게임을위한 것입니다. 화면 중앙에있는 플레이어, 그 주변의 물체 및 오프 스크린.캔버스 및 경로를 사용하여 한 지점을 2 차원 회전합니다.

이 화살표는 화면 중심을 중심으로 회전하며 플레이어 주변의 원을 그리며 회전합니다. 화살표는 플레이어가 앞으로 이동해야하는 대상을 가리 킵니다.

내가 지금하고있는 것은 다소 효과가 있지만, 화살표는 어리석은 속도로 원 주위를 돌아 다니고 있습니다. 재미있게도, 그들은 올바른 방향을 가리키고 있지만, 원의 오른쪽 지점에 머무르지는 않습니다. (화살표가 북동쪽을 가리키고 있으면 화살표가 원의 북동쪽 부분에 있어야 함)

나는 그것이 수학 때문일 것이라고 확신합니다. 아마 atan2 잘못 사용하고 있습니다. 아니면 canvas.translate wrong. 또는 atan2를 전혀 사용하지 않아야합니다. 도움! :) 여기

코드입니다 :

// set the shape of our radar blips 
    oBlipPath.moveTo(0, -5); 
    oBlipPath.lineTo(5, 0); 
    oBlipPath.lineTo(0, 5); 

    // Paint all the enemies and radar blips! 
    for(int i=0; i<iNumEnemies; i++){ 
     if (oEnemies[i].draw(canvas, (int)worldX, (int)worldY)){ 

      //calculate the degree the object is from the center of the screen. 
      //(user is the player. this could be done easier using iWidth and iHeight probably) 
      //we use a world coordinate system. worldY and worldX are subtracted 
      fDegrees = (float)Math.toDegrees(Math.atan2((oEnemies[i].getEnemyCenterY()-worldY)-user.getShipCenterY(), (oEnemies[i].getEnemyCenterX()-worldX)-user.getShipCenterX())); 

      canvas.save(); 

      //get to the center 
      canvas.translate((iWidth/2) , (iHeight/2)); 

      //move a little bit depending on direction (trying to make arrows appear around a circle) 
      canvas.translate((float)(20 * Math.cos(fDegrees)), (float)(20* Math.sin(fDegrees))); 

      //rotate canvas so arrows will rotate and point in the right direction 
      canvas.rotate(fDegrees); 

      //draw arrows 
      canvas.drawPath(oBlipPath, oBlipPaint); 
      canvas.restore(); 

     } 
    } 

답변

1

글쎄, 내가 원하는 것을하고있다. 그러나 나는 정말로 어떻게하는지 모른다. 내가 원하는 방식으로 화면에 상황이 나타날 때까지 임의의 숫자로 던졌습니다. 누군가가 이것을하기에 더 좋은 방법으로 나를 자극하고 싶다면, 나는 모든 귀입니다.

코드 : 어떤 이유로

// set the shape of our radar blips 
    oBlipPath.moveTo(0, -5); 
    oBlipPath.lineTo(6, 0); 
    oBlipPath.lineTo(0, 5); 


    oBlipMatrix.setRotate(45, 0, 0); 
    oBlipPath.transform(oBlipMatrix); 

    // Paint all the enemies and radar blips! 
    for(int i=0; i<iNumEnemies; i++){ 
     oEnemies[i].draw(canvas, (int)worldX, (int)worldY); 
     if (oEnemies[i].bActive){ 
      //calculate the degree the object is from the center of the screen. 
      //(user is the player. this could be done easier using iWidth and iHeight probably) 
      //we use a world coordinate system. worldY and worldX are subtracted 
      fDegrees = (float)Math.toDegrees(Math.atan2((oEnemies[i].getEnemyCenterY()-worldY)-(iHeight/2), (oEnemies[i].getEnemyCenterX()-worldX)-(iWidth/2))); 

      canvas.save(); 

      //get to the center 
      canvas.translate((iWidth/2 + 50) , (iHeight/2 + 50)); 

      //move a little bit depending on direction (trying to make arrows appear around a circle) 
      //canvas.translate((float)(20 * Math.cos(fDegrees)), (float)(20* Math.sin(fDegrees))); 

      //rotate canvas so arrows will rotate and point in the right direction 
      canvas.rotate(fDegrees-45, -50, -50); 

      //draw arrows 
      canvas.drawPath(oBlipPath, oBlipPaint); 
      canvas.restore(); 
     } 

    } 

, 나는 캔버스 회전에서 45 개도를 빼기하지만, 경로 형태의 매트릭스 회전에 45 개도를 추가해야합니다. 그것은 작동하지만 왜?! :)

+0

+1 나는 안드로이드 녀석이 아니지만,이게 더 나아 보인다. 명령문 명령이 다른 방법을 제시 할지라도, 회전이 _ 번역 전에 _ 생기는 것으로 나타납니다. – trashgod

1

아핀 변환은 하지 교환 법칙이 성립한다. 그들은 일반적으로 명백한 마지막 지정 first- 주문에 적용됩니다. 또는 점을 중심으로 회전하는 rotate() 변형을 고려해 볼 수 있습니다.

+0

나는 두 번째 번역본을 없애고 회전을 canvas.rotate (fDegrees, 20, 20)로 변경했습니다. 그냥 빨리 테스트 할 수 있습니다. 좋은 소식은 화살표가 미친 듯이 멈추었 고 올바른 방향을 가리키고 있다는 것입니다. 거의. 그들은 원의 반대쪽에 있습니다. 북동쪽을 가리키는 화살표, 동그라미의 남동쪽에있는 등. 다른 문제는 화살표의 잘못된 가장자리가 원을 따라 타고 있다는 것입니다. Path()를 수정하여 화살표를 수정하면 화살표가 올바른 객체를 가리 키지 않습니다. – Salx

+0

각도에서 45도를 빼고 패스 모양의 변형에서 45도를 더하여 작동 시켰습니다. 무슨 일이든, 맞지? 나는 내일 코드를 올릴 것이다. 저의 저소득층 수에 제한이 있습니다. 5 시간 정도 기다려야합니다. – Salx