ToyPetersItemRotateUtils.cpp 1 KB

//
//  ToyPetersItemRotateUtils.cpp
//  HalloweenSpaceInvaders-mobile
//
//  Created by Katarzyna Kalinowska-Górska on 13/09/2019.
//

#include <stdio.h>
#include <math.h>       /* atan2 */
#include "ToyPetersItemRotateUtils.h"

/**
 Gives the change of positions and the new rotation
 of object in next iteration.
 Saves result in pre-allocated array output.
 dx, dy - change of coordinates in x and y
 teta - new angle of rotated object
 
 Assuming the beginning of coordinates system is
 in the middle of the circle. Thus, it is important
 to give valid (x0,y0). But (dx,dy) is not affected.
 
 The start of the rotation should be at (x=0,y=-r).
 */
void RotateUtils::nextPosition(float* output, float r,float x0,float y0,float dphi){
    float phi0 = atan2(y0,x0);
    float phi1 = phi0 + dphi;
    
    float teta = phi1 + M_PI/2;
    
    float x1 = r * cos(phi1);
    float y1 = r * sin(phi1);
    
    float dx = x1 - x0;
    float dy = y1 - y0;
    
    output[0] = dx;
    output[1] = dy;
    output[2] = teta;
}