Git Product home page Git Product logo

cpdebuglayer's Introduction

CPDebugLayer is a CCLayer subclass that helps visualize and debug your physics by drawing shape outlines and bounding boxes, constraints (joints) and collision points. It is based on 'drawSpace.c' by Scott Lembcke but targets Cocos2d 2.0 and OpenGL ES 2.0. As opposed to other Chipmunk debug helper CPDebugLayer is designed to use as little private API as possible (currently only 3 uses) - 'chipmunk_private.h' is not even required - which makes it a lot more stable. Cocos2d's 'CCDrawingPrimitives' are used for drawing in OpenGL ES 2.0.

##Features##

CPDebugLayer can be configured to draw the following Chipmunk debug information:

  • shape outlines
  • shape bounding boxes
  • constraints (joints)
  • collision points

###Supported shapes###

All kind of shapes are supported: circle, segment and poly shapes!

###Supported constraint/joint types:###

  • pin, slide, pivot, groove and damped springs joints
  • gear joint is in the work and will be added shortly

##Usage## Using the CPDebugLayer is as simple as that:

// create chipmunk debug layer
CCLayer *debugLayer = [[CPDebugLayer alloc] initWithSpace:_space options:nil];
[self addChild:debugLayer z:0]; // higher z than your other layers

It can be configured by passing a NSDictionary for the options parameter.

##Screenshots## Standard configuration: Shapes, constraints and collision points enabled shapes, constraints and collision points enabled

Bounding boxes enabled shapes, constraints, collision points and bounding boxes enabled

Groove joint and slide joint shapes, constraints and collision points enabled

##Configuration options##

Option Description Default value
CPDebugLayerDrawShapes Enables drawing of shape outlines. YES
CPDebugLayerDrawConstraints Enables drawing of constraints/joints. YES
CPDebugLayerDrawBBs Enables drawing of shape bounding boxes. NO
CPDebugLayerDrawCollisionPoints Enables drawing of collision points. YES
CPDebugLayerShapeColor Sets the color for shape outlines. green
CPDebugLayerConstraintColor Sets the color for constraints/joints. blue
CPDebugLayerBBColor Sets the color for shape bounding boxes. yellow
CPDebugLayerCollisionPointColor Sets the color for collision points. magenta
CPDebugLayerLineWidth Sets the line width for drawing. 2.0
CPDebugLayerPointSize Sets the point size for drawing. 5.0

###Example### The following dictionary represents the default options:

[NSDictionary dictionaryWithObjectsAndKeys:
 [NSNumber numberWithBool:YES], CPDebugLayerDrawShapes,
 [NSNumber numberWithBool:YES], CPDebugLayerDrawConstraints,
 [NSNumber numberWithBool:NO], CPDebugLayerDrawBBs,
 [NSNumber numberWithBool:YES], CPDebugLayerDrawCollisionPoints,
 [CPDebugLayerColor colorWithCCColor3B:ccGREEN], CPDebugLayerShapeColor,
 [CPDebugLayerColor colorWithCCColor3B:ccBLUE], CPDebugLayerConstraintColor,
 [CPDebugLayerColor colorWithCCColor3B:ccYELLOW], CPDebugLayerBBColor,
 [CPDebugLayerColor colorWithCCColor3B:ccMAGENTA], CPDebugLayerCollisionPointColor,
 [NSNumber numberWithFloat:2.0f], CPDebugLayerLineWidth,
 [NSNumber numberWithFloat:5.0f], CPDebugLayerPointSize,
 nil];

###Color values###

Color values are represented by the CPDebugLayerColor class as seen in the example above. A color object can be created by passing red, green, blue and alpha values, by passing a ccColor4F, a ccColor4B or a ccColor3B.

// create a red color
CPDebugLayerColor *red;

// using r,g, b, a values
red = [CPDebugLayerColor colorWithR:1.0f g:0.0f b:0.0f a:1.0f];

// using ccColor4F
red = [CPDebugLayerColor colorWithCCColor4F:(ccColor4F) {1.0f, 0.0f, 0.0f, 1.0f}];

// using ccColor4B
red = [CPDebugLayerColor colorWithCCColor4B:ccc4(255, 0, 0, 255)];

// using ccColor3B
red = [CPDebugLayerColor colorWithCCColor3B:ccc3(255, 0, 0)];

// using color constants
red = [CPDebugLayerColor colorWithCCColor3B:ccRED];

##Planned improvements##

I've noticed accessing a NSDictionary multiple times per frame drops the frame rate. In future I'll use a C struct holding the config data internally, but still offer the nice Objective-C API using a dictionary.

cpdebuglayer's People

Contributors

fadookie avatar nubbel avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.