ios7 - How to calculate vector of force to apply to my physics body in Sprite Kit? -
ios7 - How to calculate vector of force to apply to my physics body in Sprite Kit? -
i'm making lular lander game. have lander object, simplicity let's imagine simple square.
when tap screen apply forcefulness said square, default cgvectormake(0,40) makes square move up.
now want rotate sprite based on accelerometer, so:
// update method cgfloat updatedaccelx = accelerx; cgfloat updatedaccely = -accelery; cgfloat angle = vectorangle(cgpointmake(updatedaccelx, updatedaccely)) - 1.61; self.lander.zrotation = angle; // end of update static inline cgfloat vectorangle(cgpoint v){ homecoming atan2f(v.y, v.x); } now rotation has changed, want apply forcefulness relative new orientation. example, if lander rotation 90 degrees right want apply (40,0) impulse.
how calculate new vector of forcefulness apply physics body?
adding box2d tag since underneath box2d physics.
considering have rotation
float rotation = lander.zrotation; you can calculate vector lander follows:
rotation += m_pi_2; float intensity = 40.0; cgvector newvec = cgvectormake(intensity * cosf(rotation), intensity * sinf(rotation)); [lander.physicsbody applyforce:newvec]; ios7 box2d sprite-kit
Comments
Post a Comment