arcos en valores incorrectos?

i am trying to calculate an angle of a body in my game.

tengo esto :

power= sqrt (   pow( ( Xs-x),2)+pow((Ys-y),2)  );
    angle=( asin( (Ys-y ) / power )  )-90;
    cannonBody->SetTransform(cannonBody->GetPosition(), (-angle ) );
    NSLog(@"%f",(  x));
    NSLog(@"%f",( y));
    NSLog(@"%f",( Xs-x));   
    NSLog(@"%f", CC_RADIANS_TO_DEGREES( -angle));

somehow i get POWER y (Ys-y) good values( 68/82 which is 0.82=56 degrees) and the log always shows angle values of -5100 , even if i convert from radians.. whats wrong here ?


preguntado el 27 de agosto de 11 a las 14:08

Can you post the exact number you put in asin and the exact number it outputs? -

WAG: Are all the variables you're printing actually floats, or are you doing some kind of data conversion? -

1 Respuestas

angle=( asin( (Ys-y ) / power )  )-90;

This looks deeply suspicious. The asin function returns an angle in radians -- subtracting 90 from that is about guaranteed not to make any sense.

Por cierto, atan2() is easier and much more stable for finding the direction of a 2D vector. Using an arcsine risks large rounding errors when the angle is close to right. (Most math libraries actually compute arcsines by doing the inverso of your calculation and applying a hardware atan2 instructions).

Respondido 27 ago 11, 19:08

This is definitely the answer. 90 radians is ~5157 degrees, so 56-5157=-5100 es correcto. - ughoavgfhw

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.