Game Development Reference
In-Depth Information
+(id) bumperWithWorld:(b2World*)world position:(CGPoint)pos;
@end
Once more, the Bumper class is derived from BodySprite . The initialization looks
very much like Listing 13-9 , in which the ball was initialized, so I'll just focus on the
important part in Listing 13-13 .
Listing 13-13 . Initializing the Bumper
#import "Bumper.h"
@implementation Bumper
-(id) initWithWorld:(b2World*)world position:(CGPoint)pos
{
if ((self = [super initWithShape:@"bumper" inWorld:world]))
{
// set the body position
physicsBody- > SetTransform([Helper toMeters:pos], 0.0f);
}
return self;
}
+(id) bumperWithWorld:(b2World*)world position:(CGPoint)pos
{
return [[self alloc] initWithWorld:world position:pos];
}
@end
The only key ingredient for the Bumper class is to set its restitution parameter to
above 1.0f—in this case you've already set it to 1.5f in PhysicsEditor. This gives any
rigid body touching the surface of the bumper an impulse that is 50 percent higher than
the force with which the bumper was hit. The result is something that's not possible in
the real world (except in real pinball): the impacting object increases its velocity after
hitting the bumper's surface. It's physics engine magic, and in this case it's very desir-
able because you save yourself a lot of headaches in implementing the bumper's logic.
Box2D does it for you.
 
 
Search Nedrilad ::




Custom Search