Friday, 15 February 2013

ios - How to determine closest CGPoint with an angle and another CGPoint -



ios - How to determine closest CGPoint with an angle and another CGPoint -

i calculate angle between 2 cgpoints :

//calculate radian , grade cgpoint diff = ccpsub(center, location);//return ccp(v1.x - v2.x, v1.y - v2.y); float rads = atan2f( diff.y, diff.x); float degs = -cc_radians_to_degrees(rads); nslog(@"rad %.2f degs %.2f",rads,degs);

now in function have pre known cgpoint , grade of above function, want calculate closest point satisfies degree.

i thinking maybe below code help me in below code start point , rotation point known, in situation know start point.

-(void) rotatearoundpoint:(cgpoint)rotationpoint angle:(cgfloat)angle { cgfloat x = cos(cc_degrees_to_radians(-angle)) * (self.position.x-rotationpoint.x) - sin(cc_degrees_to_radians(-angle)) * (self.position.y-rotationpoint.y) + rotationpoint.x; cgfloat y = sin(cc_degrees_to_radians(-angle)) * (self.position.x-rotationpoint.x) + cos(cc_degrees_to_radians(-angle)) * (self.position.y-rotationpoint.y) + rotationpoint.y;

lets have point 800,600 , have grade of 70, how can calculate closest point point , degree?

edit:::

normally in game sprites moved button hence rotation,movement,speed etc handled when button pressed [sprite movetopregivenpostion:cgpoint]

but compass added , when user take angle on compass need move sprite in direction of grade on compass, since [sprite movetopregivenpostion:cgpoint] handles rotation , other stuff want determine cgpoint should send movetopregivenpostion function.

as @trumpetlicks said cant find closest point that, guess understood want , function -(void) rotatearoundpoint:(cgpoint)rotationpoint angle:(cgfloat)angle trying utilize fine accomplish want.

all need take float radius.

you know current point , lets radius 1, can calculate previous point without degree, assuming 0 degrees left of point , lets point 200,200 1 radius 0 grade previous point automatically becomes 199,200. have reference point calculate point want move sprite:

//choose feasable radius float radius = 0.5; //position_ preknown position said //find point roate //position_.x-radius 0 degrees of current point cgfloat x = cos(rads) * ((position_.x-radius)-position_.x) - sin(rads) * ((position_.y)-position_.y) + position_.x; cgfloat y = sin(rads) * ((position_.x-radius)-position_.x) + cos(rads) * ((position_.y)-position_.y) + position_.y; //get new point cgpoint newlocation = ccp(x, y);

ios objective-c cocos2d-iphone angle

No comments:

Post a Comment