SHARE
TWEET

Untitled

a guest Feb 9th, 2015 14 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* AMS_Trail.h
  2.  */
  3.  
  4. #import <Foundation/Foundation.h>
  5. #import <SpriteKit/SpriteKit.h>
  6.  
  7. @interface AMS_TrailPoint : NSObject
  8.  
  9. @property CGVector m_vPosition;
  10. @property double m_fDistanceToNext;
  11.  
  12. @end
  13.  
  14. @interface AMS_Trail : NSObject
  15.  
  16. @property int m_iMaxPoints;
  17.  
  18. @property NSMutableArray* m_aPoints;
  19.  
  20. - (void)addVector:(CGVector)vec;
  21. - (void)addPoint:(AMS_TrailPoint*)pt;
  22.  
  23. - (CGVector)positionFromHead:(double)fDistance;
  24.  
  25. @end
  26.  
  27.  
  28.  
  29.  
  30. /* AMS_Trail.m
  31.  * (CGVectorAdditions is this: https://github.com/samgreen/CGVector-Awesome)
  32.  */
  33.  
  34. #import "AMS_Trail.h"
  35. #import "../CGVectorAdditions.h"
  36.  
  37. @implementation AMS_TrailPoint
  38.  
  39. - (instancetype)init
  40. {
  41.         if(self = [super init]) {
  42.                
  43.         }
  44.         return self;
  45. }
  46.  
  47. @end
  48.  
  49. @implementation AMS_Trail
  50.  
  51. - (instancetype)init
  52. {
  53.         if(self = [super init]) {
  54.                 self.m_iMaxPoints = 1000; // should be enough.. 1000 * (4 + 4 + 8) = up to 16 kB ^.^
  55.                 self.m_aPoints = [[NSMutableArray alloc] init];
  56.         }
  57.         return self;
  58. }
  59.  
  60. - (void)addVector:(CGVector)vec
  61. {
  62.         AMS_TrailPoint* tp = [[AMS_TrailPoint alloc] init];
  63.         tp.m_vPosition = vec;
  64.         if(self.m_aPoints.count > 0) {
  65.                 AMS_TrailPoint* tpFirst = [self.m_aPoints firstObject];
  66.                 tp.m_fDistanceToNext = CGVectorDistance(vec, tpFirst.m_vPosition);
  67.         }
  68.         [self addPoint:tp];
  69. }
  70.  
  71. - (void)addPoint:(AMS_TrailPoint*)tp
  72. {
  73.         if(self.m_iMaxPoints != 0 && self.m_aPoints.count >= self.m_iMaxPoints) {
  74.                 int iFromIndex = self.m_iMaxPoints - 2;
  75.                 [self.m_aPoints removeObjectsInRange:NSMakeRange(iFromIndex, self.m_aPoints.count - iFromIndex)];
  76.         }
  77.         [self.m_aPoints insertObject:tp atIndex:0];
  78. }
  79.  
  80. - (CGVector)positionFromHead:(double)fDistance
  81. {
  82.         for(int i=0; i<self.m_aPoints.count - 1; i++) {
  83.                 AMS_TrailPoint* tp = [self.m_aPoints objectAtIndex:i];
  84.                 if(fDistance > tp.m_fDistanceToNext) {
  85.                         fDistance -= tp.m_fDistanceToNext;
  86.                 } else {
  87.                         // lerp between last and current points
  88.                         AMS_TrailPoint* tpNext = [self.m_aPoints objectAtIndex:i + 1];
  89.                         CGVector vOffset = CGVectorDifference(tpNext.m_vPosition, tp.m_vPosition);
  90.                         vOffset = CGVectorNormalize(vOffset);
  91.                         vOffset = CGVectorMultiplyByScalar(vOffset, fDistance);
  92.                         return CGVectorSum(tp.m_vPosition, vOffset);
  93.                 }
  94.         }
  95.        
  96.         // is last point?
  97.         if(!self.m_aPoints.count == 0) {
  98.                 AMS_TrailPoint* tpLast = [self.m_aPoints lastObject];
  99.                 return tpLast.m_vPosition;
  100.         }
  101.        
  102.         // list is empty
  103.         return CGVectorMake(0, 0);
  104. }
  105.  
  106. @end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top