Game Development Reference
In-Depth Information
// reset rotation of the ball
physicsBody- > SetTransform(physicsBody- > GetWorldCenter(), 0.0f);
}
I'll get to the applyForceTowardsFinger method next. But while we're here,
notice how you check to see whether the ball has gone down the drain. The sprite's y
position is compared with the sprite image's height multiplied by 10. Why the multi-
plication? That's just to give the impression that it takes a short moment for the ball to
roll back before it reappears. If the ball has fallen down far enough outside the screen
area, the setBallStartPosition resets the ball's position, and the fun begins
anew.
Problem is, that doesn't seem to work initially. Unfortunately, the PhysicsSprite
class doesn't update the sprite's position property at all. But that's an easy fix. You only
need to add one line near the end of the nodeToParentTransform method in the
PhysicsSprite class:
-(CGAffineTransform) nodeToParentTransform
{
b2Vec2 pos = physicsBody- > GetPosition();
float x = pos.x * PTM_RATIO;
float y = pos.y * PTM_RATIO;
. . .
self.position = CGPointMake(x, y);
// Rot, Translate Matrix
transform_ = CGAffineTransformMake(c, s, -s, c, x, y);
return transform_;
}
The Ball's update method also ensures that the ball has a maximum speed that it will
never exceed. I've chosen the maxSpeed value to be 6.0 merely by trial and error. The
length of the linear velocity vector of the ball's body is the ball's current speed. If the
body's speed exceeds maxSpeed , the body should be slowed down to maxSpeed
without changing its direction. You achieve that by first normalizing the velocity vec-
tor, which results in a vector that still points in the same direction but has a length of
Search Nedrilad ::




Custom Search