ios - Finding a desired point based on an objects velocity and rotation -
i'm looking find point 200 pixels in front of enemy object. method try calculate point this:
//all sprites start facing down, begin point 200 pixels infront of sprite current pos -200 on y axis. cgpoint predictedpoint = cgpointmake(self.position.x, self.position.y - 200); //get direction of vector current position. predictedpoint = [utilities minusvector:predictedpoint vector2:self.position]; predictedpoint = [utilities cgpointnormalize:predictedpoint]; //multiply 200 200 pixels ahead. predictedpoint = [utilities multiplyvector:predictedpoint scalar:200]; //work out way rotate enemy based on velocity. (this code works enemies face way move!) cgpoint facingvector = [utilities minusvector:self.position vector2:cgpointmake(self.position.x + self.velocity.x, self.position.y + self.velocity.y)]; float theta = (atan2f(facingvector.y, facingvector.x) - sk_degrees_to_radians(90.0f)); //rotate float cs = cosf(theta); float sn = sinf(theta); float px = predictedpoint.x * cs - predictedpoint.y * sn; float py = predictedpoint.x * sn + predictedpoint.y * cs; cgpoint thepoint = cgpointmake(px, py); nslog(@"player x: %f. thepoint x: %f. player y: %f. thepoint y: %f.", self.position.x, px, self.position.y, py);
so calculation should be
green.center.x = triangle.center.x + 200 * cos( theta ); green.center.y = triangle.center.y + 200 * sin( theta );
where theta
current rotation angle of triangle. assumes theta == 0
has triangle pointing right. if 0
angle has sprite pointing down, think need subtract m_pi_2
, e.g.
green.center.x = triangle.center.x + 200 * cos( theta - m_pi_2 ); green.center.y = triangle.center.y + 200 * sin( theta - m_pi_2 );
Comments
Post a Comment