ios - Cocos2D - Make sprite smoothly follow & rotate to touch -


i'm trying make sprite smoothly follow & rotate according touch on screen. player not forced touch on sprite make move, can control 1 sprite @ time. wherever touch on screen, player must follow movement. here's have far :

in gamescene.m :

- (bool) cctouchbegan:(uitouch *)touch withevent:(uievent *)event {     cgpoint location = [touch locationinview:[touch view]];     location = [[ccdirector shareddirector] converttogl:location];      lasttouchlocation = location;      return yes; }  - (void) cctouchmoved:(uitouch *)touch withevent:(uievent *)event {     cgpoint location = [touch locationinview:[touch view]];     location = [[ccdirector shareddirector] converttogl:location];      //moveby determine translate vector     cgpoint moveby = ccpsub(lasttouchlocation, location);     [player moveby:moveby];      lasttouchlocation = location; } 

and here's player.m :

- (float) calculateanglefornextpoint:(cgpoint) point {     cgpoint nextpoint = point;     cgpoint pos = self.position;      float offx = nextpoint.x - pos.x;     float offy = nextpoint.y - pos.y;      float angle = atan2f(offy, offx);     angle = cc_radians_to_degrees(angle);     angle = -angle + 90;      return angle; }  - (void) moveby:(cgpoint)_pos {     cgpoint newpos = ccpsub(self.position, _pos);     float angle = [self calculateanglefornextpoint:newpos];      self.position = newpos;     self.rotation = angle;     } 

this code works well, rotation not smooth @ all, when player moves finger on screen, sprite goes crazy! tried many things, setting actions, touchmoved method called rapidly use actions.

what should solve this? lot!

one option use physics engine (chipmunk, box2d, ...) , have damped spring between finger position , sprite.

if can't afford physics engine in game, , want smooth real-life movement, should keep sprite position, velocity , acceleration (idem angle, angular velocity, angular acceleration). finger movement update accelerations (like spring do), , start scheduler update velocity , position 60 times/second.

that's how i'd it


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -