Graphics Reference
In-Depth Information
Listing 11.3 Modeling a Falling Crate Using Realistic Physics
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "chipmunk.h"
@interface Crate : UIImageView
@property ( nonatomic , assign ) cpBody *body;
@property ( nonatomic , assign ) cpShape *shape;
@end
@implementation Crate
#define MASS 100
- ( id )initWithFrame:( CGRect )frame
{
if (( self = [ super initWithFrame :frame]))
{
//set image
self . image = [ UIImage imageNamed : @"Crate.png" ];
self . contentMode = UIViewContentModeScaleAspectFill ;
//create the body
self . body = cpBodyNew ( MASS , cpMomentForBox ( MASS , frame. size . width ,
frame. size . height ));
//create the shape
cpVect corners[] = {
cpv ( 0 , 0 ),
cpv ( 0 , frame. size . height ),
cpv (frame. size . width , frame. size . height ),
cpv (frame. size . width , 0 ),
};
self . shape = cpPolyShapeNew ( self . body , 4 , corners,
cpv (-frame. size . width / 2 ,
-frame. size . height / 2 ));
//set shape friction & elasticity
cpShapeSetFriction ( self . shape , 0.5 );
cpShapeSetElasticity ( self . shape , 0.8 );
//link the crate to the shape
//so we can refer to crate from callback later on
Search WWH ::




Custom Search