From 5c7fc9f4e5b23915aa861882db0868baf061bbb0 Mon Sep 17 00:00:00 2001 From: James Macanufo Date: Tue, 5 Nov 2013 10:44:07 -0600 Subject: [PATCH 1/4] Added boundingBox and boundingboxesarray methods for hit/touch testing --- GameDevHelperAPI/GHSkeleton.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/GameDevHelperAPI/GHSkeleton.h b/GameDevHelperAPI/GHSkeleton.h index b57c271..3099a9f 100644 --- a/GameDevHelperAPI/GHSkeleton.h +++ b/GameDevHelperAPI/GHSkeleton.h @@ -61,8 +61,7 @@ skeleton = [GHSkeleton skeletonWithFile:@"resourceFolderWhereItWasPublished/skeletons/DocName_SkeletonName.plist"]; @endcode */ -@interface GHSkeleton : CCNode -{ +@interface GHSkeleton : CCNode { GHBone* rootBone; CCSpriteBatchNode* batchNode_;//sprites are kept in this batchNode @@ -160,4 +159,14 @@ This will change or set an animation by transitioning each bone position */ -(void)stopAnimation; +/** + Returns a CGRect that is the sum of all the skins. + */ +-(CGRect) boundingBox; + +/** + Returns an array of NSValues that represent the individual boundingboxes for each skin. Could be used for more detailed hit testing. + */ +-(NSMutableArray*)boundingBoxesArray; + @end From a111c889b6d1599d7d3f2eb4402aef6e14381bb0 Mon Sep 17 00:00:00 2001 From: James Macanufo Date: Tue, 5 Nov 2013 10:47:22 -0600 Subject: [PATCH 2/4] Added boundingbox and boundingboxesarray methods. Additional -draw debug code. Tested on retina device on animated (but not moving or scaling) skeleton. --- GameDevHelperAPI/GHSkeleton.mm | 79 ++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 8 deletions(-) diff --git a/GameDevHelperAPI/GHSkeleton.mm b/GameDevHelperAPI/GHSkeleton.mm index ef2f311..37e1ea5 100644 --- a/GameDevHelperAPI/GHSkeleton.mm +++ b/GameDevHelperAPI/GHSkeleton.mm @@ -47,7 +47,7 @@ @implementation GHSkeleton -(void)dealloc{ delegate = nil; #ifdef GH_DEBUG - shaderProgram_ = nil; + _shaderProgram = nil; #endif [self unscheduleAllSelectors]; [self unscheduleUpdate]; @@ -892,22 +892,46 @@ -(void)transformSkins } + +-(CGRect)boundingBox{ + + CGRect bigBox = CGRectZero; + + for(GHBoneSkin* skin in skins) {bigBox = CGRectUnion(bigBox, [skin boundingBox]);} + + return bigBox; + +} + +-(NSMutableArray*)boundingBoxesArray{ + + NSMutableArray* allBoundingBoxes = [NSMutableArray array]; + for(GHBoneSkin* skin in skins) { + [allBoundingBoxes addObject:[NSValue valueWithCGRect:[skin boundingBox]]]; + } + + return allBoundingBoxes; + + +} + + #ifdef GH_DEBUG -(void)initShader { - shaderProgram_ = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_Position_uColor]; + _shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_Position_uColor]; - colorLocation_ = glGetUniformLocation( shaderProgram_->program_, "u_color"); + colorLocation_ = glGetUniformLocation( _shaderProgram->_program, "u_color"); } -(void)debugDrawBone:(GHBone*)bone { if([bone rigid]){ - [shaderProgram_ setUniformLocation:colorLocation_ withF1:0 f2:0 f3:1 f4:1]; + [_shaderProgram setUniformLocation:colorLocation_ withF1:0 f2:0 f3:1 f4:1]; } else{ - [shaderProgram_ setUniformLocation:colorLocation_ withF1:0 f2:1 f3:0 f4:1]; + [_shaderProgram setUniformLocation:colorLocation_ withF1:0 f2:1 f3:0 f4:1]; } for(GHBone* child in [bone children]) @@ -925,18 +949,57 @@ -(void)debugDrawBone:(GHBone*)bone } } + + -(void) draw{ - if(!shaderProgram_)return; + if(!_shaderProgram)return; - [shaderProgram_ use]; - [shaderProgram_ setUniformForModelViewProjectionMatrix]; + [_shaderProgram use]; + [_shaderProgram setUniformsForBuiltins]; [self debugDrawBone:rootBone]; CC_INCREMENT_GL_DRAWS(1); CHECK_GL_ERROR_DEBUG(); + + + //DRAW THE RECT FOR EACH SKIN + ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color ); + ccDrawColor4F(1, 1, 1, 1); + glLineWidth(2.0f); + for (NSValue* rectVal in [self boundingBoxesArray]){ + + [self drawBox:[rectVal CGRectValue]]; + + } + + //DRAW THE BIG RECT + ccDrawColor4F(1, 0, 0, 1); + glLineWidth(4.0f); + [self drawBox:[self boundingBox]]; + + [super draw]; + + + + + + +} + + +-(void) drawBox: (CGRect) rect +{ + CGPoint vertices[4]={ + ccp(rect.origin.x,rect.origin.y), + ccp(rect.origin.x+rect.size.width,rect.origin.y), + ccp(rect.origin.x+rect.size.width,rect.origin.y+rect.size.height), + ccp(rect.origin.x,rect.origin.y+rect.size.height), + }; + ccDrawPoly(vertices, 4, YES); } + #endif From d9936fdb49e368802b7f85ccba3e4287400e99b8 Mon Sep 17 00:00:00 2001 From: James Macanufo Date: Tue, 19 Nov 2013 12:07:25 -0600 Subject: [PATCH 3/4] added (CGRect)boundingBox method --- GameDevHelperAPI/GHBoneSkin.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/GameDevHelperAPI/GHBoneSkin.h b/GameDevHelperAPI/GHBoneSkin.h index fb0f404..b6b93fd 100644 --- a/GameDevHelperAPI/GHBoneSkin.h +++ b/GameDevHelperAPI/GHBoneSkin.h @@ -27,7 +27,10 @@ CGPoint positionOffset; float angleOffset; float connectionAngle;//initial angle when bone was connected with the sprite + } + + /** Get and set the position offset that is used when transforming a sprite based on bone movement. */ @@ -84,4 +87,7 @@ Update sprite position and rotation based on the bone movement. */ -(void)transform; + +//Get my bounding box +-(CGRect)boundingBox; @end From 967b5f297fb1791f8c847983cf4466404276e752 Mon Sep 17 00:00:00 2001 From: James Macanufo Date: Tue, 19 Nov 2013 12:08:41 -0600 Subject: [PATCH 4/4] added (CGRect)boundingBox method --- GameDevHelperAPI/GHBoneSkin.mm | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/GameDevHelperAPI/GHBoneSkin.mm b/GameDevHelperAPI/GHBoneSkin.mm index eb0801a..50baea9 100644 --- a/GameDevHelperAPI/GHBoneSkin.mm +++ b/GameDevHelperAPI/GHBoneSkin.mm @@ -125,5 +125,14 @@ -(void)transform [sprite setPosition:CGPointMake(origin.x + posOffset.x, origin.y - posOffset.y)]; [sprite setRotation:newAngle]; +} + +-(CGRect)boundingBox{ + + //get the rect of the GHSprite + return self.sprite.boundingBox; + + + } @end