Last active
December 16, 2015 23:19
-
-
Save LearnCocos2D/5513061 to your computer and use it in GitHub Desktop.
KoboldTouch physics collision demo code
http://www.koboldtouch.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PhysicsCollisionSceneViewController.m | |
// _Feature-Demo-Template_ | |
// | |
// Created by Steffen Itterheim on 01.05.13. | |
// Copyright 2013 __MyCompanyName__. All rights reserved. | |
// | |
#import "PhysicsCollisionSceneViewController.h" | |
@implementation PhysicsCollisionSceneViewController | |
-(void) createTest | |
{ | |
KTPhysicsController* physicsController = [KTPhysicsController physicsControllerWithBox2D]; | |
physicsController.gravity = CGPointMake(0, -10); | |
[physicsController constrainWorldToWindow]; | |
[self addSubController:physicsController]; | |
[self createBouncingIcon]; | |
[self createSensorBody]; | |
[self createFilteredBody]; | |
KTPhysicsDebugViewController* debugVC = [KTPhysicsDebugViewController physicsDebugViewControllerWithPhysicsController:physicsController]; | |
[self addSubController:debugVC]; | |
} | |
-(void) createBouncingIcon | |
{ | |
KTPhysicsBodyModel* bodyModel = [KTPhysicsBodyModel physicsBodyModelWithPhysicsController:self.sceneViewController.physicsController]; | |
KTPhysicsBody* body = bodyModel.physicsBody; | |
body.contactDelegate = self; | |
body.position = CGPointMake(150, 40); | |
body.linearVelocity = CGPointMake(0, 5); | |
KTPhysicsShape* shape = [KTPhysicsCircleShape circleShapeWithRadius:22]; | |
KTPhysicsFixture* fixture = [body createFixtureWithShape:shape]; | |
fixture.restitution = 1.1f; | |
[fixture setContactCategory:@"icon"]; | |
[fixture disableCollisionWithCategory:@"noIconContact"]; | |
_iconSprite = [CCSprite spriteWithFile:@"Icon.png"]; | |
KTViewController* vc = [KTViewController controller]; | |
vc.rootNode = _iconSprite; | |
vc.model = bodyModel; | |
[self addSubController:vc]; | |
} | |
#pragma mark Contact Callbacks (in the order they are being called) | |
-(void) beginContact:(KTPhysicsContact*)contact body:(KTPhysicsBody*)body otherBody:(KTPhysicsBody*)otherBody | |
{ | |
//NSLog(@"beginContact"); | |
_iconSprite.color = ccRED; | |
if (body.model.position.y > 250) | |
{ | |
body.linearVelocity = CGPointZero; | |
body.angularVelocity = CCRANDOM_MINUS1_1() * 20.0f; | |
// get the body's only fixture | |
KTPhysicsFixture* fixture = [body.fixtures anyObject]; | |
[fixture enableCollisionWithCategory:@"noIconContact"]; | |
// only do this once because TTF string updates are sloooooow | |
if (_didChangeFilterString == NO) | |
{ | |
_didChangeFilterString = YES; | |
[_filterLabel setString:@"Now I can Contact Icon!"]; | |
} | |
} | |
} | |
-(void) preSolveContact:(KTPhysicsContact*)contact body:(KTPhysicsBody*)body otherBody:(KTPhysicsBody*)otherBody oldManifold:(KTPhysicsManifold*)oldManifold | |
{ | |
//NSLog(@"preSolveContact"); | |
// to ignore a specific contact until the next update you would do this: | |
//contact.enabled = NO; | |
} | |
-(void) postSolveContact:(KTPhysicsContact*)contact body:(KTPhysicsBody*)body otherBody:(KTPhysicsBody*)otherBody impulse:(KTPhysicsContactImpulse*)impulse | |
{ | |
//NSLog(@"postSolveContact"); | |
} | |
-(void) endContact:(KTPhysicsContact*)contact body:(KTPhysicsBody*)body otherBody:(KTPhysicsBody*)otherBody | |
{ | |
//NSLog(@"endContact"); | |
_iconSprite.color = ccWHITE; | |
} | |
#pragma mark other colliding bodies | |
-(void) createSensorBody | |
{ | |
KTPhysicsBodyModel* bodyModel = [KTPhysicsBodyModel physicsBodyModelWithPhysicsController:self.physicsController]; | |
KTPhysicsBody* body = bodyModel.physicsBody; | |
body.position = CGPointMake(230, 170); | |
body.gravityScale = 0; // unaffected by gravity | |
body.allowSleeping = NO; | |
CCLabelTTF* label = [CCLabelTTF labelWithString:@"I'm a sensor body" fontName:@"GurmukhiMN-Bold" fontSize:40]; | |
KTPhysicsShape* shape = [KTPhysicsRectShape rectShapeWithSize:label.contentSize]; | |
KTPhysicsFixture* fixture = [body createFixtureWithShape:shape]; | |
fixture.isSensor = YES; | |
[fixture setContactCategory:@"sensor"]; | |
// this is where it all comes together | |
KTViewController* vc = [KTViewController controller]; | |
vc.rootNode = label; | |
vc.model = bodyModel; | |
[self addSubController:vc]; | |
} | |
-(void) createFilteredBody | |
{ | |
KTPhysicsBodyModel* bodyModel = [KTPhysicsBodyModel physicsBodyModelWithPhysicsController:self.physicsController]; | |
KTPhysicsBody* body = bodyModel.physicsBody; | |
body.position = CGPointMake(200, 60); | |
body.gravityScale = 0; // unaffected by gravity | |
body.allowSleeping = NO; | |
_filterLabel = [CCLabelTTF labelWithString:@"My contacts are filtered out" fontName:@"GurmukhiMN" fontSize:20]; | |
KTPhysicsShape* shape = [KTPhysicsRectShape rectShapeWithSize:_filterLabel.contentSize]; | |
KTPhysicsFixture* fixture = [body createFixtureWithShape:shape]; | |
fixture.isSensor = YES; | |
[fixture setContactCategory:@"noIconContact"]; | |
// this is where it all comes together | |
KTViewController* vc = [KTViewController controller]; | |
vc.rootNode = _filterLabel; | |
vc.model = bodyModel; | |
[self addSubController:vc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment