SlideShare une entreprise Scribd logo
1  sur  42
Chipmunk Physics

Integrating A Physics Engine with UIKIT


             Carl Brown
           CocoaCoder.org
            12 July 2012
What is a Physics
    Engine?
What Physics
Engines are there?


‣Box2D
‣Chipmunk Physics
Why might one
 choose Box2D?

‣Larger Community
‣More Examples
‣Continuous Collisions ("Bullets")
‣Good Enough for Angry Birds
Why Chipmunk?
 ‣ C w/Objective-C* instead of C++
    ‣ //I *HATE* C++




*Objective-C Wrapper requires $89 indie license, C is MIT licensed/Free
Why Chipmunk?
 ‣ C w/Objective-C* instead of C++
    ‣ //I *HATE* C++
 ‣ Global (Space) Origin in the same
   place as UIView
    ‣(Seems different for Box2D)




*Objective-C Wrapper requires $89 indie license, C is MIT licensed/Free
Why Chipmunk?
 ‣ C w/Objective-C* instead of C++
    ‣ //I *HATE* C++
 ‣ Global (Space) Origin in the same
   place as UIView
    ‣(Seems different for Box2D)
 ‣ Units in pixels instead of meters



*Objective-C Wrapper requires $89 indie license, C is MIT licensed/Free
Why Chipmunk?
 ‣ C w/Objective-C* instead of C++
    ‣ //I *HATE* C++
 ‣ Global (Space) Origin in the same
   place as UIView
    ‣(Seems different for Box2D)
 ‣ Units in pixels instead of meters
 ‣ Box2D: body->DoThing(&shapeObject)
    ‣ //I find that annoying
*Objective-C Wrapper requires $89 indie license, C is MIT licensed/Free
Why Chipmunk?
 ‣ C w/Objective-C* instead of C++
    ‣ //I *HATE* C++
 ‣ Global (Space) Origin in the same
   place as UIView
    ‣(Seems different for Box2D)
 ‣ Units in pixels instead of meters
 ‣ Box2D: body->DoThing(&shapeObject)
    ‣ //I find that annoying
*Objective-C Wrapper requires $89 indie license, C is MIT licensed/Free
See http://www.cocoanetics.com/2010/05/physics-101-uikit-app-with-box2d-for-gravity/ for
Box2D code
What about
       Cocos2D?

‣ Cocos2D is a Gaming Framework
‣ Uses Sprites/Scenes (kinda like Views)
‣ Integrates Either Box2D or Chipmunk
What about
       Cocos2D?

‣ Cocos2D is a Gaming Framework
‣ Uses Sprites/Scenes (kinda like Views)
‣ Integrates Either Box2D or Chipmunk
‣ Still need to deal with Physics Engine
What about
       Cocos2D?

‣ Cocos2D is a Gaming Framework
‣ Uses Sprites/Scenes (kinda like Views)
‣ Integrates Either Box2D or Chipmunk
‣ Still need to deal with Physics Engine
‣ Might as well learn Physics w/UIKit
What about
      Cocos2D?

‣ Cocos2D is a Gaming Framework
‣ Uses Sprites/Scenes (kinda like Views)
‣ Integrates Either Box2D or Chipmunk
‣ Still need to deal with Physics Engine
‣ Might as well learn Physics w/UIKit
‣ Cocos2D + Physics Way too long a talk
Integrating
  Chipmunk with
      UIKit
‣Create Space
Integrating
  Chipmunk with
      UIKit
‣Create Space
‣Create Body+shape for UIView
Integrating
  Chipmunk with
      UIKit
‣Create Space
‣Create Body+shape for UIView
‣Iterate Space
Integrating
   Chipmunk with
       UIKit
‣Create Space
‣Create Body+shape for UIView
‣Iterate Space
‣Reset Frame Coordinates
 ‣ Happens Third, but we'll talk about it Last
Chipmunk Spaces

!self.space = [[ChipmunkSpace alloc] init];
!
[self.space addBounds:self.view.bounds
  thickness:10.0f
  elasticity:1.0f friction:1.0f
  layers:CP_ALL_LAYERS group:CP_NO_GROUP
  collisionType:borderType];

!self.space.gravity = cpv(0.0f, 10.0f);
Space Parameters
‣ Bounds
 ‣ Walls around the Space that things bounce off of
Space Parameters
‣ Bounds
 ‣ Walls around the Space that things bounce off of
‣ Elasticity
 ‣ "Bounciness"
Space Parameters
‣ Bounds
 ‣ Walls around the Space that things bounce off of
‣ Elasticity
 ‣ "Bounciness"
‣ Friction
Space Parameters
‣ Bounds
 ‣ Walls around the Space that things bounce off of
‣ Elasticity
 ‣ "Bounciness"
‣ Friction
‣ Layers/Group/CollisionType
 ‣ Think "Collision Filters"
 ‣ Allow for "Friendly Fire" type scenarios
Space Parameters
‣ Bounds
 ‣ Walls around the Space that things bounce off of
‣ Elasticity
 ‣ "Bounciness"
‣ Friction
‣ Layers/Group/CollisionType
 ‣ Think "Collision Filters"
 ‣ Allow for "Friendly Fire" type scenarios
‣ Gravity
Chipmunk Body/
          Shape
ChipmunkBody *body = [self.space add:[ChipmunkBody
bodyWithMass:mass andMoment:cpMomentForBox(mass,
view.bounds.size.width, view.bounds.size.height)]];

[body setData:view]; //Make retrievable

ChipmunkShape *shape = [self.space add:
[ChipmunkPolyShape boxWithBody:body
width:view.bounds.size.width
height:view.bounds.size.height]];

shape.friction = 0.8f;
shape.elasticity = 0.1f;
Chipmunk Body/
      Shape

‣ Body
 ‣ Used for motion
 ‣ Acceleration/Mass/Momentum/Energy/Rotation/Etc
Chipmunk Body/
      Shape

‣ Body
 ‣ Used for motion
 ‣ Acceleration/Mass/Momentum/Energy/Rotation/Etc
‣ Shape
 ‣ Used for Collisions and Intersections
Iterate Space
  _displayLink = [CADisplayLink displayLinkWithTarget:self
selector:@selector(update:)];
![self.displayLink addToRunLoop:[NSRunLoop mainRunLoop]
forMode:NSRunLoopCommonModes];


-(void) update:(id) sender {
!cpFloat dt =
self.displayLink.duration*self.displayLink.frameInterval;
![self.space step:dt];

    for (ChipmunkBody *body in self.space.bodies) {
      UIView *view = (UIView *) [body data];
      [view setTransform:body.affineTransform];
    }
}

         *DisplayLink requires the QuartzCore Framework
Display Link


‣ Calls a method on every screen refresh
‣ Used to update Motion/Position
‣ Can be set to every N frames
[Space Step]

‣ Tell the physics simulator to advance by
 a given timestep
‣ Simple multiplication here
‣ Using an accumulator is more accurate
 ‣ See: http://gafferongames.com/game-physics/fix-
  your-timestep/
setTransform


‣ Chipmunk creates an AffineTransform
 for each body on each step
‣ Use that value to set the "transform"
 property of the UIView
What the @#$% is
   a Transform?
‣ Remember Matrix Math?
 ‣ Yeah, I didn't think so
‣ It's a Mathematical representation (matrix)
 that's used to describe Position and
 Rotation of an object
‣ You don't have to understand them to use
 them
 ‣ Though it helps when debugging
 ‣ See http://iphonedevelopment.blogspot.com/2008/10/
  demystifying-cgaffinetransform.html for more
Reset Frame

body.pos =   cpv(view.center.x,view.center.y);

//Reset the frame to the origin and set the Transform
// to move the object back to where the frame had it
view.center=CGPointMake(0.0f, 0.0f);
view.transform=CGAffineTransformMakeTranslation(body.pos.x,
body.pos.y);
Why Reset Frame?




https://developer.apple.com/library/ios/#documentation/UIKit/Reference/
UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/transform
Wait, What?
‣Think of it like This:
‣ You can EITHER use a Frame OR a Transform
 to Position views
‣ NEVER BOTH
‣ IF you set both, they interact with each
 other in weird ways
Wait, What?
‣Think of it like This:
‣ You can EITHER use a Frame OR a Transform
 to Position views
‣ NEVER BOTH
‣ IF you set both, they interact with each
 other in weird ways
‣ To Switch: set one to Origin and then set the
 other to the same place
Wait, What?
‣Think of it like This:
‣ You can EITHER use a Frame OR a Transform
 to Position views
‣ NEVER BOTH
‣ IF you set both, they interact with each
 other in weird ways
‣ To Switch: set one to Origin and then set the
 other to the same place
‣ Happens fast enough, hard to notice
 ‣ You can Hide or remove subview first if it's noticeable
Now we're set up


Not much of a game so far.

       Now What?
Collision
        Callbacks

‣ "Kill Pigs"
‣ Add Score
‣ Play Sounds
‣ [Space setDefaultCollisionHandler:...]
Joints &
      Constraints

‣ "Build House to shoot Birds at"
‣ "Hook up Spring to Slingshot"
‣ [Space addConstraint:...]
‣ http://www.youtube.com/watch?v=ZgJJZTS0aMM
Apply Impulses


‣ "Shoot something out of a cannon"
‣ [Body applyImpulse:offset:...]
RayCasting
(ChipmunkSegmentQueryInfo)



 ‣ Does "Bullet" handling (Avoids
  Tunneling)
 ‣ [Space segmentQueryFirstFrom:to:...]
Where Do I get it?


‣ Downloads:
 ‣ http://chipmunk-physics.net/downloads.php
‣ Documentation:
 ‣ http://chipmunk-physics.net/documentation.php

Contenu connexe

Similaire à Chipmunk physics presentation

Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo KumperaAdvanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Xamarin
 

Similaire à Chipmunk physics presentation (20)

Box2D and libGDX
Box2D and libGDXBox2D and libGDX
Box2D and libGDX
 
Basics cocos2d
Basics cocos2dBasics cocos2d
Basics cocos2d
 
Box2D with SIMD in JavaScript
Box2D with SIMD in JavaScriptBox2D with SIMD in JavaScript
Box2D with SIMD in JavaScript
 
ITB2016 Converting Legacy Apps into Modern MVC
ITB2016 Converting Legacy Apps into Modern MVCITB2016 Converting Legacy Apps into Modern MVC
ITB2016 Converting Legacy Apps into Modern MVC
 
Writing JavaScript that doesn't suck
Writing JavaScript that doesn't suckWriting JavaScript that doesn't suck
Writing JavaScript that doesn't suck
 
Cross platform game development
Cross platform game developmentCross platform game development
Cross platform game development
 
Tools for developing Android Games
 Tools for developing Android Games Tools for developing Android Games
Tools for developing Android Games
 
Intro
IntroIntro
Intro
 
Boxen: How to Manage an Army of Laptops
Boxen: How to Manage an Army of LaptopsBoxen: How to Manage an Army of Laptops
Boxen: How to Manage an Army of Laptops
 
Custom view
Custom viewCustom view
Custom view
 
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo KumperaAdvanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
 
Meet Elcodi, the flexible e-commerce components built on Symfony2
Meet Elcodi, the flexible e-commerce components built on Symfony2Meet Elcodi, the flexible e-commerce components built on Symfony2
Meet Elcodi, the flexible e-commerce components built on Symfony2
 
Day 1
Day 1Day 1
Day 1
 
Introduction to modern c++ principles(part 1)
Introduction to modern c++ principles(part 1)Introduction to modern c++ principles(part 1)
Introduction to modern c++ principles(part 1)
 
Lecture2_practice.pdf
Lecture2_practice.pdfLecture2_practice.pdf
Lecture2_practice.pdf
 
Koin Quickstart
Koin QuickstartKoin Quickstart
Koin Quickstart
 
Dependency Injection Why is it awesome and Why should I care?
Dependency Injection Why is it awesome and Why should I care?Dependency Injection Why is it awesome and Why should I care?
Dependency Injection Why is it awesome and Why should I care?
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Android Jetpack + Coroutines: To infinity and beyond
Android Jetpack + Coroutines: To infinity and beyondAndroid Jetpack + Coroutines: To infinity and beyond
Android Jetpack + Coroutines: To infinity and beyond
 
Bronx study jam 1
Bronx study jam 1Bronx study jam 1
Bronx study jam 1
 

Plus de Carl Brown

Cocoa coders 141113-watch
Cocoa coders 141113-watchCocoa coders 141113-watch
Cocoa coders 141113-watch
Carl Brown
 

Plus de Carl Brown (20)

GDPR, User Data, Privacy, and Your Apps
GDPR, User Data, Privacy, and Your AppsGDPR, User Data, Privacy, and Your Apps
GDPR, User Data, Privacy, and Your Apps
 
New in iOS 11.3b4 and Xcode 9.3b4
New in iOS 11.3b4 and Xcode 9.3b4New in iOS 11.3b4 and Xcode 9.3b4
New in iOS 11.3b4 and Xcode 9.3b4
 
Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)
 
Better Swift from the Foundation up #tryswiftnyc17 09-06
Better Swift from the Foundation up #tryswiftnyc17 09-06Better Swift from the Foundation up #tryswiftnyc17 09-06
Better Swift from the Foundation up #tryswiftnyc17 09-06
 
Generics, the Swift ABI and you
Generics, the Swift ABI and youGenerics, the Swift ABI and you
Generics, the Swift ABI and you
 
Swift GUI Development without Xcode
Swift GUI Development without XcodeSwift GUI Development without Xcode
Swift GUI Development without Xcode
 
what's new in iOS10 2016-06-23
what's new in iOS10 2016-06-23what's new in iOS10 2016-06-23
what's new in iOS10 2016-06-23
 
Open Source Swift: Up and Running
Open Source Swift: Up and RunningOpen Source Swift: Up and Running
Open Source Swift: Up and Running
 
Parse migration CocoaCoders April 28th, 2016
Parse migration CocoaCoders April 28th, 2016Parse migration CocoaCoders April 28th, 2016
Parse migration CocoaCoders April 28th, 2016
 
Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016
 
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
 
Gcd cc-150205
Gcd cc-150205Gcd cc-150205
Gcd cc-150205
 
Cocoa coders 141113-watch
Cocoa coders 141113-watchCocoa coders 141113-watch
Cocoa coders 141113-watch
 
iOS8 and the new App Store
iOS8 and the new App Store   iOS8 and the new App Store
iOS8 and the new App Store
 
Dark Art of Software Estimation 360iDev2014
Dark Art of Software Estimation 360iDev2014Dark Art of Software Estimation 360iDev2014
Dark Art of Software Estimation 360iDev2014
 
Intro to cloud kit Cocoader.org 24 July 2014
Intro to cloud kit   Cocoader.org 24 July 2014Intro to cloud kit   Cocoader.org 24 July 2014
Intro to cloud kit Cocoader.org 24 July 2014
 
Welcome to Swift (CocoaCoder 6/12/14)
Welcome to Swift (CocoaCoder 6/12/14)Welcome to Swift (CocoaCoder 6/12/14)
Welcome to Swift (CocoaCoder 6/12/14)
 
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
Writing Apps that Can See: Getting Data from CoreImage to Computer  Vision - ...Writing Apps that Can See: Getting Data from CoreImage to Computer  Vision - ...
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
 
Introduction to Git Commands and Concepts
Introduction to Git Commands and ConceptsIntroduction to Git Commands and Concepts
Introduction to Git Commands and Concepts
 
REST/JSON/CoreData Example Code - A Tour
REST/JSON/CoreData Example Code - A TourREST/JSON/CoreData Example Code - A Tour
REST/JSON/CoreData Example Code - A Tour
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Dernier (20)

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 

Chipmunk physics presentation

  • 1. Chipmunk Physics Integrating A Physics Engine with UIKIT Carl Brown CocoaCoder.org 12 July 2012
  • 2. What is a Physics Engine?
  • 3. What Physics Engines are there? ‣Box2D ‣Chipmunk Physics
  • 4. Why might one choose Box2D? ‣Larger Community ‣More Examples ‣Continuous Collisions ("Bullets") ‣Good Enough for Angry Birds
  • 5. Why Chipmunk? ‣ C w/Objective-C* instead of C++ ‣ //I *HATE* C++ *Objective-C Wrapper requires $89 indie license, C is MIT licensed/Free
  • 6. Why Chipmunk? ‣ C w/Objective-C* instead of C++ ‣ //I *HATE* C++ ‣ Global (Space) Origin in the same place as UIView ‣(Seems different for Box2D) *Objective-C Wrapper requires $89 indie license, C is MIT licensed/Free
  • 7. Why Chipmunk? ‣ C w/Objective-C* instead of C++ ‣ //I *HATE* C++ ‣ Global (Space) Origin in the same place as UIView ‣(Seems different for Box2D) ‣ Units in pixels instead of meters *Objective-C Wrapper requires $89 indie license, C is MIT licensed/Free
  • 8. Why Chipmunk? ‣ C w/Objective-C* instead of C++ ‣ //I *HATE* C++ ‣ Global (Space) Origin in the same place as UIView ‣(Seems different for Box2D) ‣ Units in pixels instead of meters ‣ Box2D: body->DoThing(&shapeObject) ‣ //I find that annoying *Objective-C Wrapper requires $89 indie license, C is MIT licensed/Free
  • 9. Why Chipmunk? ‣ C w/Objective-C* instead of C++ ‣ //I *HATE* C++ ‣ Global (Space) Origin in the same place as UIView ‣(Seems different for Box2D) ‣ Units in pixels instead of meters ‣ Box2D: body->DoThing(&shapeObject) ‣ //I find that annoying *Objective-C Wrapper requires $89 indie license, C is MIT licensed/Free See http://www.cocoanetics.com/2010/05/physics-101-uikit-app-with-box2d-for-gravity/ for Box2D code
  • 10. What about Cocos2D? ‣ Cocos2D is a Gaming Framework ‣ Uses Sprites/Scenes (kinda like Views) ‣ Integrates Either Box2D or Chipmunk
  • 11. What about Cocos2D? ‣ Cocos2D is a Gaming Framework ‣ Uses Sprites/Scenes (kinda like Views) ‣ Integrates Either Box2D or Chipmunk ‣ Still need to deal with Physics Engine
  • 12. What about Cocos2D? ‣ Cocos2D is a Gaming Framework ‣ Uses Sprites/Scenes (kinda like Views) ‣ Integrates Either Box2D or Chipmunk ‣ Still need to deal with Physics Engine ‣ Might as well learn Physics w/UIKit
  • 13. What about Cocos2D? ‣ Cocos2D is a Gaming Framework ‣ Uses Sprites/Scenes (kinda like Views) ‣ Integrates Either Box2D or Chipmunk ‣ Still need to deal with Physics Engine ‣ Might as well learn Physics w/UIKit ‣ Cocos2D + Physics Way too long a talk
  • 14. Integrating Chipmunk with UIKit ‣Create Space
  • 15. Integrating Chipmunk with UIKit ‣Create Space ‣Create Body+shape for UIView
  • 16. Integrating Chipmunk with UIKit ‣Create Space ‣Create Body+shape for UIView ‣Iterate Space
  • 17. Integrating Chipmunk with UIKit ‣Create Space ‣Create Body+shape for UIView ‣Iterate Space ‣Reset Frame Coordinates ‣ Happens Third, but we'll talk about it Last
  • 18. Chipmunk Spaces !self.space = [[ChipmunkSpace alloc] init]; ! [self.space addBounds:self.view.bounds thickness:10.0f elasticity:1.0f friction:1.0f layers:CP_ALL_LAYERS group:CP_NO_GROUP collisionType:borderType]; !self.space.gravity = cpv(0.0f, 10.0f);
  • 19. Space Parameters ‣ Bounds ‣ Walls around the Space that things bounce off of
  • 20. Space Parameters ‣ Bounds ‣ Walls around the Space that things bounce off of ‣ Elasticity ‣ "Bounciness"
  • 21. Space Parameters ‣ Bounds ‣ Walls around the Space that things bounce off of ‣ Elasticity ‣ "Bounciness" ‣ Friction
  • 22. Space Parameters ‣ Bounds ‣ Walls around the Space that things bounce off of ‣ Elasticity ‣ "Bounciness" ‣ Friction ‣ Layers/Group/CollisionType ‣ Think "Collision Filters" ‣ Allow for "Friendly Fire" type scenarios
  • 23. Space Parameters ‣ Bounds ‣ Walls around the Space that things bounce off of ‣ Elasticity ‣ "Bounciness" ‣ Friction ‣ Layers/Group/CollisionType ‣ Think "Collision Filters" ‣ Allow for "Friendly Fire" type scenarios ‣ Gravity
  • 24. Chipmunk Body/ Shape ChipmunkBody *body = [self.space add:[ChipmunkBody bodyWithMass:mass andMoment:cpMomentForBox(mass, view.bounds.size.width, view.bounds.size.height)]]; [body setData:view]; //Make retrievable ChipmunkShape *shape = [self.space add: [ChipmunkPolyShape boxWithBody:body width:view.bounds.size.width height:view.bounds.size.height]]; shape.friction = 0.8f; shape.elasticity = 0.1f;
  • 25. Chipmunk Body/ Shape ‣ Body ‣ Used for motion ‣ Acceleration/Mass/Momentum/Energy/Rotation/Etc
  • 26. Chipmunk Body/ Shape ‣ Body ‣ Used for motion ‣ Acceleration/Mass/Momentum/Energy/Rotation/Etc ‣ Shape ‣ Used for Collisions and Intersections
  • 27. Iterate Space _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update:)]; ![self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; -(void) update:(id) sender { !cpFloat dt = self.displayLink.duration*self.displayLink.frameInterval; ![self.space step:dt]; for (ChipmunkBody *body in self.space.bodies) { UIView *view = (UIView *) [body data]; [view setTransform:body.affineTransform]; } } *DisplayLink requires the QuartzCore Framework
  • 28. Display Link ‣ Calls a method on every screen refresh ‣ Used to update Motion/Position ‣ Can be set to every N frames
  • 29. [Space Step] ‣ Tell the physics simulator to advance by a given timestep ‣ Simple multiplication here ‣ Using an accumulator is more accurate ‣ See: http://gafferongames.com/game-physics/fix- your-timestep/
  • 30. setTransform ‣ Chipmunk creates an AffineTransform for each body on each step ‣ Use that value to set the "transform" property of the UIView
  • 31. What the @#$% is a Transform? ‣ Remember Matrix Math? ‣ Yeah, I didn't think so ‣ It's a Mathematical representation (matrix) that's used to describe Position and Rotation of an object ‣ You don't have to understand them to use them ‣ Though it helps when debugging ‣ See http://iphonedevelopment.blogspot.com/2008/10/ demystifying-cgaffinetransform.html for more
  • 32. Reset Frame body.pos = cpv(view.center.x,view.center.y); //Reset the frame to the origin and set the Transform // to move the object back to where the frame had it view.center=CGPointMake(0.0f, 0.0f); view.transform=CGAffineTransformMakeTranslation(body.pos.x, body.pos.y);
  • 34. Wait, What? ‣Think of it like This: ‣ You can EITHER use a Frame OR a Transform to Position views ‣ NEVER BOTH ‣ IF you set both, they interact with each other in weird ways
  • 35. Wait, What? ‣Think of it like This: ‣ You can EITHER use a Frame OR a Transform to Position views ‣ NEVER BOTH ‣ IF you set both, they interact with each other in weird ways ‣ To Switch: set one to Origin and then set the other to the same place
  • 36. Wait, What? ‣Think of it like This: ‣ You can EITHER use a Frame OR a Transform to Position views ‣ NEVER BOTH ‣ IF you set both, they interact with each other in weird ways ‣ To Switch: set one to Origin and then set the other to the same place ‣ Happens fast enough, hard to notice ‣ You can Hide or remove subview first if it's noticeable
  • 37. Now we're set up Not much of a game so far. Now What?
  • 38. Collision Callbacks ‣ "Kill Pigs" ‣ Add Score ‣ Play Sounds ‣ [Space setDefaultCollisionHandler:...]
  • 39. Joints & Constraints ‣ "Build House to shoot Birds at" ‣ "Hook up Spring to Slingshot" ‣ [Space addConstraint:...] ‣ http://www.youtube.com/watch?v=ZgJJZTS0aMM
  • 40. Apply Impulses ‣ "Shoot something out of a cannon" ‣ [Body applyImpulse:offset:...]
  • 41. RayCasting (ChipmunkSegmentQueryInfo) ‣ Does "Bullet" handling (Avoids Tunneling) ‣ [Space segmentQueryFirstFrom:to:...]
  • 42. Where Do I get it? ‣ Downloads: ‣ http://chipmunk-physics.net/downloads.php ‣ Documentation: ‣ http://chipmunk-physics.net/documentation.php

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n