Game Development Reference
In-Depth Information
float circle_length = _lineContainer->getLineLength()
* 2 * M_PI;
int iterations = floor(circle_length /
_rocket->getSpeed());
_rocket->setAngularSpeed ( 2 * M_PI / iterations);
11. It grabs the length of the circumference about to be described by _rocket
(line length * 2 * PI) and divides it by the rocket's speed, getting in
return the number of iterations needed for the rocket to complete that length.
Then the 360 degrees of the circle is divided by the same number of iterations
(but we do it in radians) to arrive at the fraction of the circle that the rocket must
rotate at each iteration: its angular speed.
12. What follows next is even more math, using the amazingly helpful methods from
Cocos2d-x related to vector math ( Point.getRPerp , Point.dot ,
Point.subtract , to name a few) some of which we've seen already in the
Rocket class:
Vec2 diff = _rocket->getPosition();
diff.subtract(_rocket->getPivot());
Point clockwise = diff.getRPerp();
float dot =clockwise.dot(_rocket->getVector());
if (dot > 0) {
_rocket->setAngularSpeed (
_rocket->getAngularSpeed() * -1 );
_rocket->setRotationOrientation ( ROTATE_CLOCKWISE
);
_rocket->setTargetRotation (
CC_RADIANS_TO_DEGREES( atan2(clockwise.y,
clockwise.x) ) );
} else {
_rocket->setRotationOrientation ( ROTATE_COUNTER );
_rocket->setTargetRotation (
CC_RADIANS_TO_DEGREES (atan2(-1 * clockwise.y, -1 *
clockwise.x) ) );
}
_lineContainer->setLineType ( LINE_DASHED );
13. What they do here is determine which direction the rocket should rotate to: clock-
wise or counterclockwise, based on its current vector of movement.
Search WWH ::




Custom Search