2013-09-30 3 views
1

나는 나중에 액세스 할 수있는 3D 점 (벡터 3D http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Vector3D.html)이있는 찾아보기 테이블을 채워서 3D 데모를 최적화하려고합니다.AS3- 3D 공간에서 반복 가능한 무작위 경로를 만듭니다.

이 3D 점은 3D 공간에서 임의의 반복 가능한 경로를 정의합니다.

누구나이 방법을 알고 있습니까?

저는 Greensock Bezier 트윈을 수정하여 3D 공간에서 베 지어를 생성 한 다음 결과 트윈의 xyz 값을 어떻게 든 잡아낼 것으로 생각했습니다.

답변

3

좋아, 두 가지 작업을 수행해야합니다 :

  1. 을 대상으로 개체를 걸립니다 사용자 정의 트윈 함수를 만듭니다 여러 조각에서 루프 3D 차 베 지어 곡선 만들기 , 경로로 베 지어 루프 및 전체 루프 시간. 당신의 차 베 지어 곡선의

한 조각은 항상 전체 루프가 적어도 2 개 세그먼트를 포함해야합니다, 4 개 정점을 취할 것입니다, 그래서 당신은 무작위로 최소 7 개 정점을 만들어야합니다. 꼭지점의 수는 항상 3 * NumberOfSegments + 1 수 있지만 첫 번째 정점은 마지막

대부분의 간단한 경우와 동일하므로 우리는 (2 개 세그먼트, 6 개 정점) 단지 3 * NumberOfSegments을 저장합니다 : 이제 때

... 
private function generateRandomPoints():Vector<Vector3D> 
{ 
    var resultingVector:Vector<Vector3D> = new Vector<Vector3D>(); 
    for(var i:int = 0; i < 6; i++) 
    { 
     var x:Number = Math.random() * 10; 
     var y:Number = Math.random() * 10; 
     var z:Number = Math.random() * 10; 
     var currentPoint3D:Vector3D = new Vector3D(x, y, z); 
     resultingVector.push(currentPoint3D); 
    } 
    return resultingVector; 
} 
... 

을 우리는 우리의 경로를 가지고 있으며,이 트위닝 효과를 얻기 위해 그것을 파싱 할 수 있습니다. 새로운 좌표가 필요할 때마다이 함수를 호출 할 수 있습니다 (그러나 어딘가에 초기 시간을 저장해야 함). 또는 모든 것을 처리 할 tweener 객체를 만들 수 있습니다. 나는 가장 기본적인 예를 보여 드리겠습니다 - 자율 기능 : 당신은 트위닝 개체의 일부가이 기능을 확장 할 수 있습니다

public static function getNextCoordinates(loopStartTime:int, totalLoopTime:int, path:Vector.<Vector3D>):Vector3D 
    { 
     var resultingPoint:Vector3D = new Vector3D(); 
     var passedTime:int = getTimer() - loopStartTime; 

     //Total passed ratio 
     var passedRatio:Number = passedTime/totalLoopTime; 
     var totalSegments:int = path.length/3; 

     var totalTimePerSegment:Number = totalLoopTime/totalSegments; 

     //So it can loop forever 
     while (passedRatio > 1) 
     { 
      passedRatio -= 1; 
     } 
     //Let's find our current bezier curve segment number 
     var currentSegment:int = Math.floor(passedRatio * totalSegments); 
     var currentSegmentRatio:Number = (passedTime - currentSegment * totalTimePerSegment)/totalTimePerSegment; 
     //It can be optimized here 
     while (currentSegmentRatio > 1) 
     { 
      currentSegmentRatio -= 1; 
     } 

     var startingIndex:int = currentSegment * 3; 
     //our four path vertices 
     var point0:Vector3D = path[startingIndex]; 
     var point1:Vector3D = path[startingIndex + 1]; 
     var point2:Vector3D = path[startingIndex + 2]; 

     //if it's a last segment, we need to "connect" to the first vertex 
     if (startingIndex + 3 >= path.length) 
     { 
      var point3:Vector3D = path[0]; 
     } 
     else 
     { 
      point3 = path[startingIndex + 3]; 
     } 
     //At last, we find our coordinates 
     var tempRatio:Number = 1 - currentSegmentRatio; 
     resultingPoint.x = tempRatio * tempRatio * tempRatio * point0.x + 3 * tempRatio * tempRatio * currentSegmentRatio * point1.x + 3 * tempRatio * currentSegmentRatio * currentSegmentRatio * point2.x + currentSegmentRatio * currentSegmentRatio * currentSegmentRatio * point3.x; 
     resultingPoint.y = tempRatio * tempRatio * tempRatio * point0.y + 3 * tempRatio * tempRatio * currentSegmentRatio * point1.y + 3 * tempRatio * currentSegmentRatio * currentSegmentRatio * point2.y + currentSegmentRatio * currentSegmentRatio * currentSegmentRatio * point3.y; 
     resultingPoint.z = tempRatio * tempRatio * tempRatio * point0.z + 3 * tempRatio * tempRatio * currentSegmentRatio * point1.z + 3 * tempRatio * currentSegmentRatio * currentSegmentRatio * point2.z + currentSegmentRatio * currentSegmentRatio * currentSegmentRatio * point3.z; 

     return resultingPoint; 
    } 

합니다. 2D 공간에서 테스트 한 결과 무작위 멀티 세그먼트 베 지어 곡선을 따라 스프라이트의 움직임을 완벽하게 루프했습니다.

건배!

+0

와우! 이것이 효과가 있다면 당신은 천재입니다. 감사. MUCH –

+0

설명 할 수없는 것이 있는지 물어보십시오. – KumoKairo

관련 문제