c++ - Sprite tracking touch -


i want implement tracking (following) touch sprite rotation in cocos2d-x. can see effect here: https://www.youtube.com/watch?v=rzoumyyngg8 (2:10).

here's code in touchmove:

_destinationx = touchpoint.x; _destinationy = touchpoint.y;  _dx = _destinationx - draggeditem->getpositionx(); _dy = _destinationy - draggeditem->getpositiony();  _vx = _dx; _vy = _dy;  float d = sqrtf((_dx*_dx)+(_dy*_dy));  //nice easing when near destination if (d < 50 && d > 3){     _vx *= d / 50.0f;     _vy *= d / 50.0f; } else if(d <= 3){     _vx = _vy = 0; }  draggeditem->setposition(draggeditem->getposition() + point(_vx, _vy));  float rad = atan2(_dy, _dx); float rotateto = cc_radians_to_degrees(-rad); if (rotateto > draggeditem->getrotation() + 180) rotateto -= 360; if (rotateto < draggeditem->getrotation() - 180) rotateto += 360;  draggeditem->setrotation(rotateto); 

it works, sprite destination point on it's center, if on touchbegin won't start it's center it's not looking well.

so i'm calculating offset in touch begin initial rotation:

offsetpoint = point(touchpoint.x - draggeditem->getpositionx(), touchpoint.y - draggeditem->getpositiony()); offsetrotation = atan2(offsetpoint.y, offsetpoint.x); 

i added these lines in touch move (on top):

float alfa = offsetrotation; float beta = cc_degrees_to_radians(draggeditem->getrotation()); float gamma = alfa + beta;  float radius = offsetpoint.length(); float ax = cosf(gamma) * radius; float ay = sinf(gamma) * radius; 

so destination be:

_destinationx = touchpoint.x + ax; _destinationy = touchpoint.y + ay; 

but doesn't work, where's issue? sprite jumping around.

as i'm not familiar @ obj-c or c++, i'm gonna give answer in swift. then, you'll able either insert project or 'translate' obj-c/c++. anyway:

inside file represents gameplay scene, declare variable keeps track of whether user touching screen or not. i'll assume sprite want follow touch represented variable (named 'sprite' here):

var touchingscreen:bool = false // needs 'false' initial value. weak var sprite:ccsprite!; // represents connection sprite. 

then, you'll want variable represents difference between value of point being touched , point sprite located in x , y axises:

var difftouchspritex:cgfloat!; // not necessary initialize default value. var difftouchspritey:cgfloat!; // same. 

the next step update these 2 variables' values when necessary. lets create method that:

func updatediff(touchx: cgfloat, touchy: cgfloat) {   self.difftouchspritex = touchx - self.sprite.position.x;   self.difftouchspritey = touchy - self.sprite.position.y; } 

now it's time make use of touch methods provided handling touch input @ specific moments. let's assign value difftouchspritex , difftouchspritey variables , set touchingscreen true when user touches screen, update difftouchspritex , difftouchspritey values when user moves finger on screen , set touchingscreen false once touch interrupted:

override func touchbegan(touch: cctouch!, withevent event: cctouchevent!) {   self.updatediff(touch.locationinworld().x, touch.locationinworld().y);   self.touchingscreen = true; }  override func touchmoved(touch: cctouch!, withevent event: cctouchevent!) {   self.updatediff(touch.locationinworld().x, touch.locationinworld().y); }  override func touchended(touch: cctouch!, withevent event: cctouchevent!) {   self.touchingscreen = false; }  override func touchcancelled(touch: cctouch!, withevent event: cctouchevent!) {   self.touchingscreen = false; } 

finally, last step. inside cocos2d method fixedupdate(), add conditional move character if screen being touched:

override func fixedupdate(delta: cctime) {   if (self.touchingscreen) {     self.sprite.runaction(ccactionmoveby(duration: 1/ 60, position: cgpoint(x: self.difftouchspritex / 60, y: self.difftouchspritey / 60)));   } } 

the fixedupdate() cocos2d method runs @ constant rate (1 time each second, if remember well). runaction() method here update sprite position adding 'x' 'x' value of current position , 'y' 'y' value of current position. so, @ rate take 1 second sprite reach current touch location, of course can manipulate.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -