Skip to content

Added boundingBox and boundingBoxesArray methods to GHSkeleton #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions GameDevHelperAPI/GHBoneSkin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -84,4 +87,7 @@
Update sprite position and rotation based on the bone movement.
*/
-(void)transform;

//Get my bounding box
-(CGRect)boundingBox;
@end
9 changes: 9 additions & 0 deletions GameDevHelperAPI/GHBoneSkin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 11 additions & 2 deletions GameDevHelperAPI/GHSkeleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
79 changes: 71 additions & 8 deletions GameDevHelperAPI/GHSkeleton.mm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ @implementation GHSkeleton
-(void)dealloc{
delegate = nil;
#ifdef GH_DEBUG
shaderProgram_ = nil;
_shaderProgram = nil;
#endif
[self unscheduleAllSelectors];
[self unscheduleUpdate];
Expand Down Expand Up @@ -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])
Expand All @@ -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


Expand Down