Game Development Reference
In-Depth Information
Time for action - updating our rocket
sprite
The game's main loop will call the rocket's update method in every iteration.
1. Inside the empty update method in Rocket.cpp , add the following lines:
Point position = this->getPosition();
if (_rotationOrientation == ROTATE_NONE) {
position.x += _vector.x * dt;
position.y += _vector.y * dt;
} else {
float angle = _angularSpeed * dt;
Point rotatedPoint = position.rotateByAngle(_pivot,
angle);
position.x = rotatedPoint.x;
position.y = rotatedPoint.y;
float rotatedAngle;
Point diff = position;
diff.subtract(_pivot);
Point clockwise = diff.getRPerp();
if (_rotationOrientation == ROTATE_COUNTER) {
rotatedAngle = atan2 (-1 * clockwise.y, -1 *
clockwise.x);
} else {
rotatedAngle = atan2 (clockwise.y, clockwise.x);
}
_vector.x = _speed * cos (rotatedAngle);
_vector.y = _speed * sin (rotatedAngle);
this->setRotationFromVector();
if (this->getRotation() > 0) {
this->setRotation( fmodf(this->getRotation(),
360.0f) );
Search WWH ::




Custom Search