I making a Iphone game like pinball, but I have great problem.
pinball paddle dose not operate collision detection.
I thinking, animated by a coordinate operation has problem....may be.
Is there anyone who has a good idea ? or editng this source?
I'm going crazy I need your help.
please somebody help me!
my project file link this (xcode, iphonesdk)
http://www.megaupload.com/?d=42E0K577
and here is my source.
MyScene.h
Code: Select all
//#import <Foundation/Foundation.h>
#import "cocos2d.h"
// Importing Chipmunk headers
#import "chipmunk.h"
@class TouchHandlingLayer;
@interface MyScene : CCLayer
{
cpSpace* space;
TouchHandlingLayer *touchHandlingLayer;
float bar_right_angle;
float bar_left_angle;
CCSprite *sprite_bar_left;
CCSprite *sprite_bar_right;
float bar_power;
}
@property (nonatomic, retain) TouchHandlingLayer *touchHandlingLayer;
@property (nonatomic) float bar_right_angle;
@property (nonatomic) float bar_left_angle;
@property (nonatomic) float bar_power;
@property (nonatomic, retain) CCSprite *sprite_bar_left;
@property (nonatomic, retain) CCSprite *sprite_bar_right;
-(void)setPower:(float) p;
@end
MyScene.m
Code: Select all
#import "MyScene.h"
#import "TouchHandlingLayer.h"
static float POWER;
void updateShape(void* ptr, void* unused)
{
cpShape* shape = (cpShape*)ptr;
CCSprite* sprite = shape->data;
if(sprite)
{
cpBody* body = shape->body;
[sprite setPosition:cpv(body->p.x, body->p.y)];
cpBodySetAngle(body, -CC_DEGREES_TO_RADIANS(sprite.rotation));
}
}
static int spriteToBarCollision(cpArbiter *arb, cpSpace *space, void *data)
{
cpShape *a, *b;
cpArbiterGetShapes(arb, &a, &b);
NSLog(@"충돌1:%f,%f 2:%f,%f",a->body->p.x, a->body->p.y,a->surface_v.x, a->surface_v.y);
NSLog(@"충돌2:%f,%",b->body->p.x, b->body->p.y);
if(POWER != 0)
{
cpVect r_vect;
CCSprite* sprite = b->data;
cpVect rot = cpv(cosf((CC_DEGREES_TO_RADIANS(sprite.rotation))), sinf((CC_DEGREES_TO_RADIANS(sprite.rotation))));
r_vect = cpvrotate(b->body->v, rot);
//r_vect = cpvadd(b->body->v, rot);
a->body->v = cpvmult(r_vect,POWER);
}
return 1;
}
static int spriteToWallCollision(cpArbiter *arb, cpSpace *space, void *data)
{
cpShape *a, *b;
cpArbiterGetShapes(arb, &a, &b);
a->body->p = ccp(20,100);
return 1;
}
@implementation MyScene
@synthesize touchHandlingLayer,bar_right_angle,bar_left_angle,sprite_bar_left,sprite_bar_right,bar_power;
enum
{
kTagBackground = 0,
kTagSpritePlayer = 1,
kTagSpriteBar = 2,
kTagTouchHandlingLayer = 3
};
-(void)setPower:(float) p
{
POWER = p;
}
-(void)tick:(ccTime) delta
{
int steps = 2;
cpFloat dt = delta/(cpFloat)steps;
// int steps = 3;
// cpFloat dt = 1.0f/60.0f/(cpFloat)steps;
for(int i=0; i<steps; i++)
{
cpSpaceStep(space, dt);
}
cpSpaceHashEach(space->activeShapes, &updateShape, nil);
cpSpaceHashEach(space->staticShapes, &updateShape, nil);
cpSpaceRehashStatic(space);
}
-(void)setupChipmunk
{
cpInitChipmunk();
space = cpSpaceNew();
space->gravity = cpv(0,-700);
space->elasticIterations = 1;
space->iterations = 1;
space->damping = 0.5;
[self schedule: @selector(tick:)];
// Collision between two sprites.
// cpSpaceAddCollisionHandler(space, 1, 3,nil,&spriteToBarCollision,nil,nil,self);
cpSpaceAddCollisionHandler(space, 1, 3,&spriteToBarCollision,nil,nil,nil,self);
cpSpaceAddCollisionHandler(space, 1, 2,nil,&spriteToWallCollision,nil,nil,self);
}
-(void)makeBackGround
{
CCSprite *back = [CCSprite spriteWithFile:@"pinball_back.png"];
back.anchorPoint = CGPointZero;
back.position = ccp(0,0);
[self addChild:back z:0 tag:kTagBackground];
}
-(void)makeBall
{
float ball_x = 40;
float ball_y = 200;
CCSprite *ballSprite = [CCSprite spriteWithFile:@"ball_red.png"];
[ballSprite setPosition:CGPointMake(ball_x, ball_y)];
[self addChild:ballSprite z:0 tag:kTagSpritePlayer];
//cpBody* ballBody = cpBodyNew(50.0, INFINITY);
cpBody* ballBody = cpBodyNew(20.0f, cpMomentForCircle(20.0f, 0.0f, 10.0f, CGPointZero));
ballBody->p = ballSprite.position;
ballBody->v_limit = 800;
ballBody->w_limit = 10;
cpSpaceAddBody(space, ballBody);
cpShape* ballShape = cpCircleShapeNew(ballBody, 10, cpvzero);
ballShape->e = 1;
ballShape->u = 0;
ballShape->data = ballSprite;
ballShape->collision_type = 1;
cpSpaceAddShape(space, ballShape);
}
-(void)makeWall
{
// Window Size
CGSize wins = [[CCDirector sharedDirector] winSize];
cpBody * staticBody_1 = cpBodyNew(INFINITY, INFINITY);
staticBody_1->v = ccp(0.7,0.3);
cpBody * staticBody_2 = cpBodyNew(INFINITY, INFINITY);
staticBody_2->v = ccp(-0.7,0.3);
cpBody * staticBody_down = cpBodyNew(INFINITY, INFINITY);
staticBody_down->v = ccp(0,1);
cpBody * staticBody_left = cpBodyNew(INFINITY, INFINITY);
staticBody_left->v = ccp(1,0);
cpBody * staticBody_right = cpBodyNew(INFINITY, INFINITY);
staticBody_right->v = ccp(-1,0);
cpBody * staticBody_up = cpBodyNew(INFINITY, INFINITY);
staticBody_up->v = ccp(0,-1);
cpShape *shape;
shape = cpSegmentShapeNew(staticBody_down, ccp(0,0), ccp(wins.width,0), 0.0f);
shape->e = 0.5f; shape->u = 0.5f;
shape->collision_type = 2;
cpSpaceAddStaticShape(space, shape);
shape = cpSegmentShapeNew(staticBody_up, ccp(0,wins.height), ccp(wins.width,wins.height), 0.0f);
shape->e = 0.5f; shape->u = 0.5f;
shape->collision_type = 4;
cpSpaceAddStaticShape(space, shape);
shape = cpSegmentShapeNew(staticBody_left, ccp(0,0), ccp(0,wins.height), 0.0f);
shape->e = 0.5f; shape->u = 0.5f;
shape->collision_type = 4;
cpSpaceAddStaticShape(space, shape);
shape = cpSegmentShapeNew(staticBody_right, ccp(wins.width,0), ccp(wins.width,wins.height), 0.0f);
shape->e = 0.5f; shape->u = 0.5f;
shape->collision_type = 4;
cpSpaceAddStaticShape(space, shape);
shape = cpSegmentShapeNew(staticBody_1, ccp(0,100), ccp(70,60), 0.0f);
shape->e = 0.5f; shape->u = 0.5f;
shape->collision_type = 4;
cpSpaceAddStaticShape(space, shape);
shape = cpSegmentShapeNew(staticBody_2,ccp(wins.width,100),ccp((wins.width-70),60), 0.0f);
shape->e = 0.5f; shape->u = 0.5f;
shape->collision_type = 4;
cpSpaceAddStaticShape(space, shape);
}
-(void)makeBar
{
cpVect verts1[] =
{
cpv(0,-10),
cpv(0,10),
cpv(90,0)
};
cpVect verts2[] =
{
cpv(-90,0),
cpv(0,10),
cpv(0,-10),
};
sprite_bar_left = [CCSprite spriteWithFile:@"bar_left.png"];
sprite_bar_left.anchorPoint = ccp(0,0.5);
[sprite_bar_left setPosition:ccp(70-5,60-10)];
sprite_bar_left.rotation = bar_left_angle;
[self addChild:sprite_bar_left z:0 tag:kTagSpriteBar];
sprite_bar_right = [CCSprite spriteWithFile:@"bar_right.png"];
sprite_bar_right.anchorPoint = ccp(1,0.5);
[sprite_bar_right setPosition:ccp(320-70+5,60-10)];
sprite_bar_right.rotation = bar_right_angle;
[self addChild:sprite_bar_right z:0 tag:kTagSpriteBar];
cpBody* bar_left_Body = cpBodyNew(INFINITY,INFINITY);
bar_left_Body->p = sprite_bar_left.position;
bar_left_Body->v = ccp(1,9);
cpBody* bar_right_Body = cpBodyNew(INFINITY,INFINITY);
bar_right_Body->p = sprite_bar_right.position;
bar_right_Body->v = ccp(-1,9);
cpShape* bar_left_shape = cpPolyShapeNew(bar_left_Body, 3, verts1, ccp(0,0.5));
bar_left_shape->e = 0.5;
bar_left_shape->u = 0.9;
bar_left_shape->data = sprite_bar_left;
bar_left_shape->collision_type = 3;
cpSpaceAddStaticShape(space, bar_left_shape);
cpShape* bar_right_shape = cpPolyShapeNew(bar_right_Body, 3, verts2, CGPointMake(1,0.5));
bar_right_shape->e = 0.5;
bar_right_shape->u = 0.9;
bar_right_shape->data = sprite_bar_right;
bar_right_shape->collision_type = 3;
cpSpaceAddStaticShape(space, bar_right_shape);
}
-(id)init
{
bar_left_angle = 30;
bar_right_angle = -30;
bar_power = 0;
self = [super init];
if(nil != self)
{
[self setupChipmunk];
[self makeBackGround];
[self makeBall];
[self makeWall];
[self makeBar];
touchHandlingLayer = [[TouchHandlingLayer alloc] init];
touchHandlingLayer.myscene = self;
[self addChild:touchHandlingLayer z:0 tag:kTagTouchHandlingLayer];
}
return self;
}
-(void) dealloc
{
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
cpSpaceFree(space);
[sprite_bar_left release];
[sprite_bar_right release];
[touchHandlingLayer release];
[super dealloc];
}
@end
TouchHandlingLayer.h
Code: Select all
#import "cocos2d.h"
#import "MyScene.h"
@interface TouchHandlingLayer : CCLayer
{
MyScene *myscene;
}
@property (nonatomic,retain) MyScene *myscene;
@end
TouchHandlingLayer.m
Code: Select all
#import "TouchHandlingLayer.h"
@implementation TouchHandlingLayer
@synthesize myscene;
-(void) dealloc
{
[myscene release];
[super dealloc];
}
-(id) init
{
if( (self=[super init]) )
{
self.isTouchEnabled = YES;
}
return self;
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"Touches Began");
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
int x = location.x;
[myscene setPower:900000];
if(x<160)
{
[myscene.sprite_bar_left stopAllActions];
myscene.bar_left_angle = -60;
[myscene.sprite_bar_left runAction: [CCRotateTo actionWithDuration:0.05 angle: myscene.bar_left_angle]];
}
else
{
[myscene.sprite_bar_right stopAllActions];
myscene.bar_right_angle = 60;
[myscene.sprite_bar_right runAction: [CCRotateTo actionWithDuration:0.05 angle: myscene.bar_right_angle]];
}
[self performSelector:@selector(endBar) withObject:nil afterDelay:(0.05f)];
}
-(void) endBar
{
[myscene setPower:0];
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//NSLog(@"Touches Moved");
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// NSLog(@"Touches Ended");
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
int x = location.x;
if(x<160)
{
[myscene.sprite_bar_left stopAllActions];
myscene.bar_left_angle = 30;
[myscene.sprite_bar_left runAction: [CCRotateTo actionWithDuration:0.05 angle: myscene.bar_left_angle]];
}
else
{
[myscene.sprite_bar_right stopAllActions];
myscene.bar_right_angle = -30;
[myscene.sprite_bar_right runAction: [CCRotateTo actionWithDuration:0.05 angle: myscene.bar_right_angle]];
}
}
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
//NSLog(@"Touches Cancelled");
// return kEventHandled;
}
@end