باد:كيفية نقل شبح الانزلاق والاستمرار في الذهاب وفقا ينزلق بسرعة

StackOverflow https://stackoverflow.com/questions/3235428

سؤال

لدي العفريت(صورة الكرة) أستطيع تحريكه باستخدام اللمس والانتقال.يجب أيضا تحديد معدل تغير موقع(محور x,y-axis) من هذا العفريت اعتمادا على انزلاق الفترة.

الآن أنا بحاجة إلى مواصلة هذا العفريت للذهاب وفقا السرعة والاتجاه.هنا هو بلدي رمز

Touch Event

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {

  CGPoint location = [touch locationInView: [touch view]];
  CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];

  self.touchStartTime = [event timestamp];
  self.touchStartPosition = location;

  if (YES == [self isItTouched:self.striker touchedAt:convertedLocation]) {
    self.striker.isInTouch = YES;
  }

  return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {

  CGPoint location = [touch locationInView: [touch view]];
  CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];

  self.touchLastMovedTime = [event timestamp];
  self.touchMovedPosition = convertedLocation;


  if(self.striker.isInTouch == YES){
    self.striker.position = self.touchMovedPosition;
  }

}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {

  CGPoint location = [touch locationInView: [touch view]];
  CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];

  self.touchEndTime = [event timestamp];
  self.touchEndPosition = location;

  if( self.striker.isInTouch == YES 
    && ( self.touchEndTime - self.touchLastMovedTime ) <= MAX_TOUCH_HOLD_DURATION )
  {
    float c = sqrt( pow( self.touchStartPosition.x - self.touchEndPosition.x, 2 ) 
        + pow( self.touchStartPosition.y - self.touchEndPosition.y, 2 ) );

    self.striker.speedx =  ( c - ( self.touchStartPosition.y - self.touchEndPosition.y ) ) 
                         / ( ( self.touchEndTime - self.touchStartTime ) * 1000 );

    self.striker.speedy =  ( c - ( self.touchStartPosition.x - self.touchEndPosition.x ) ) 
             / ( ( self.touchEndTime -   self.touchStartTime ) * 1000 );

    self.striker.speedx *= 4;
    self.striker.speedy *= 4;

    self.striker.isInTouch = NO;
    [self schedule:@selector( nextFrame ) interval:0.001];

  }

}

Scheduled Method to move Sprite

- (void) nextFrame {

  [self setPieceNextPosition:self.striker];
  [self adjustPieceSpeed:self.striker];

  if( abs( self.striker.speedx ) <= 1 && abs( self.striker.speedy ) <= 1 ){
    [self unschedule:@selector( nextFrame )];
  }
}

SET next Position

- (void) setPieceNextPosition:(Piece *) piece{

  CGPoint nextPosition;
  float tempMod;
  tempMod = ( piece.position.x + piece.speedx ) / SCREEN_WIDTH;
  tempMod = (tempMod - (int)tempMod)*SCREEN_WIDTH;
  nextPosition.x = tempMod;

  tempMod = ( piece.position.y + piece.speedy ) / SCREEN_HEIGHT;
  tempMod = (tempMod - (int)tempMod)*SCREEN_HEIGHT;
  nextPosition.y = tempMod;

  piece.position = nextPosition;
}

Set new Speed

- (void) adjustPieceSpeed:(Piece *) piece{

  piece.speedx =(piece.speedx>0)? piece.speedx-0.05:piece.speedx+0.05;
  piece.speedy =(piece.speedy>0)? piece.speedy-0.05:piece.speedy+0.05;
}

على الرغم من حاليا أنا باستخدام ثابت سرعة ضبط تقنية, ولكن آمل أن تجعل ديناميكية اعتمادا على السرعة الأولى( وأنا أقدر أي فكرة)

هل كانت مفيدة؟

المحلول 2

شكراall. لقد حصلت على حلاي على النحو التالي -

giveacodicetagpre.

نصائح أخرى

يبدو أن لديك قريبة جدا.أنت جدولة محدد على اتصال نهاية الحوسبة السرعة و التحرك العفريت ووفقا تلك السرعة حتى damps ، لذلك جميع القطع الميكانيكية هناك.

ما يبدو غير محدد هو الفيزياء.على سبيل المثال, كنت تفعل بعض الأشياء الغريبة مع سرعة بطيئة الكرة التي لن تبدو واقعية على الإطلاق.

ما تريد القيام به هو العثور على "ناقل السرعة" عن العفريت عندما الاصبع رفعت.في حين يمكنك محاولة السرعة اللحظية لهذا, لقد وجدت أنه يعمل بشكل أفضل على عينة بضع دورات ذهابا متوسط عليهم للحصول على السرعة النهائية.

ثم, مرة واحدة كنت قد حصلت على أن ناقلات (سيبدو x و y السرعة كما هو الحال الآن) ، تريد تخفيف السرعة عن طريق القيام بشيء من هذا القبيل (لم تختبر شبة الكود قبل):

 float speed = sqrt( speedx * speedx + speedy * speedy );
 if (speed == 0) {
     // kill motion here, unschedule or do whatever you need to
 }
 // Dampen the speed.
 float newSpeed = speed * .9;
 if (newSpeed < SOME_THRESHOLD) newSpeed = 0;
 speedx = speedx * newSpeed / speed;
 speedy = speedy * newSpeed / speed;
 //  Move sprite according to the new speed

هذا وسوف تبقي الكرة تسير في نفس الاتجاه عندما أطلق سراحه ، يتباطأ تدريجيا حتى يتوقف.للحصول على مزيد من المعلومات ، وخاصة إذا كنت تريد أن تفعل بعض كذاب أو أي شيء من جوجل مقدمة في المتجهات الجبر.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top