Callum Luke Perks
Callum Luke Perks

Reputation: 49

Rotation isn't running smoothly

I have a function that is meant to take a give angle and rotate the coordinates of a triangle by that angle. However, it pauses slightly then jumps to a new position rather than running smoothly.

The function works by translating the original coordinates to the origin, rotating them by the angle that has already been moved plus the specified angle. Then it translates the coordinates back to the unmodified positions before translating them to where the image currenty is.

void rotateTriangle(double angle_){
int ang = angle_ + angle;
triCoords[0]=((triCoords_[0] - triPos_[0])*cos(ang) - (triCoords_[1] - triPos_[1])*sin(ang)) + triPos_[0] + triPos[0];
triCoords[1]=((triCoords_[1] - triPos_[1])*cos(ang) + (triCoords_[0] - triPos_[0])*sin(ang)) + triPos_[1] + triPos[1];
triCoords[2]=((triCoords_[2] - triPos_[0])*cos(ang) - (triCoords_[3] - triPos_[1])*sin(ang)) + triPos_[0] + triPos[0];
triCoords[3]=((triCoords_[3] - triPos_[1])*cos(ang) + (triCoords_[2] - triPos_[0])*sin(ang)) + triPos_[1] + triPos[1];
triCoords[4]=((triCoords_[4] - triPos_[0])*cos(ang) - (triCoords_[5] - triPos_[1])*sin(ang)) + triPos_[0] + triPos[0];
triCoords[5]=((triCoords_[5] - triPos_[1])*cos(ang) + (triCoords_[4] - triPos_[0])*sin(ang)) + triPos_[1] + triPos[1];

}

Upvotes: 0

Views: 36

Answers (1)

unwind
unwind

Reputation: 399999

Well, this:

int ang = angle_ + angle;

will of course truncate the floating-point angle to an integer. Since sin() and cos() work with radians, that doesn't work out too well.

You should have:

const double ang = angle_ + angle;

It's not clear what angle is here (angle_ is the function's argument, but the two names are confusingly similiar), I guess it's a global variable.

If this is is for on-screen rendering, you might consider limiting yourself to float precision and use sinf() and cosf() instead.

Upvotes: 1

Related Questions