Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-28-2009, 02:58 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 135
mediashock is on a distinguished road
Default cocos2d Collision detection between sprites

I am stuck on detecting collision on two sprites.. anyone has a function or anything that may help me detect collision between two sprites?
__________________
--- sup
mediashock is offline   Reply With Quote
Old 04-28-2009, 03:13 PM   #2 (permalink)
Developer
 
Join Date: Jan 2009
Location: Dallas
Posts: 27
SpaceHunter is on a distinguished road
Default

Quote:
Originally Posted by mediashock View Post
I am stuck on detecting collision on two sprites.. anyone has a function or anything that may help me detect collision between two sprites?
CGRectIntersectsRect is your friend. Past in two CGRects (each one represents a sprite). A Boolean is returned based on intersects/collection. Easy as pie.
SpaceHunter is offline   Reply With Quote
Old 04-28-2009, 03:41 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 135
mediashock is on a distinguished road
Default

so i have

Sprite *sprite = [Sprite spriteWithFile: @"game.png"];

how would i get CGRect of it...
__________________
--- sup
mediashock is offline   Reply With Quote
Old 04-28-2009, 03:50 PM   #4 (permalink)
Developer
 
Join Date: Jan 2009
Location: Dallas
Posts: 27
SpaceHunter is on a distinguished road
Default

Quote:
Originally Posted by mediashock View Post
so i have

Sprite *sprite = [Sprite spriteWithFile: @"game.png"];

how would i get CGRect of it...
Create a method in your class to return the size of your Sprite...something like this...
Code:
- (CGRect) getSize
{
    double sx = sprite.position.x;
    double sy = sprite.position.yl
    return CGRectMake(sx, sy, width, height);
}
SpaceHunter is offline   Reply With Quote
Old 07-08-2009, 07:33 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 12
pravin is on a distinguished road
Default Collision detection of two sprite objetcs

I made a special method for determining (atlas) sprite bounding rectangles (wrapped into a "SpriteUtilities" class):
Code:
+(CGRect) positionRect: (AtlasSprite*)sprite {
	CGSize contentSize = [sprite contentSize];
	CGPoint contentPosition = [sprite position];
	CGRect result = CGRectOffset(CGRectMake(0, 0, contentSize.width, contentSize.height), contentPosition.x-contentSize.width/2, contentPosition.y-contentSize.height/2);
	return result;
}

Then to check for collision of two atlas sprites:

CGRect rect1 = [SpriteUtilities positionRect:atlasSprite1];
CGRect rect2 = [SpriteUtilities positionRect:atlasSprite2];
if (!CGRectIsNull(CGRectIntersection(rect1, rect2))) {
    //handle collision
}
To get better result related to cocos-2d post your queries on cocos-2d forum
pravin is offline   Reply With Quote
Old 01-04-2012, 12:26 AM   #6 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 19
francis is on a distinguished road
Default

aren't there more clearer solution on this? ~.~
francis is offline   Reply With Quote
Old 01-04-2012, 02:59 AM   #7 (permalink)
Bringing fun to you
 
davidlxk's Avatar
 
Join Date: Dec 2011
Posts: 43
davidlxk is on a distinguished road
Default

you should try going through this tutorial How To Make A Simple iPhone Game with Cocos2D Tutorial | Ray Wenderlich
__________________
Drop Dem is out now! You should check it out at http://itunes.apple.com/us/app/drop-dem/id490101113

You should follow Drop Dem on Twitter http://twitter.com/dropdemgame
or find us on Facebook http://www.facebook.com/pages/Drop-Dem/314923981881703
davidlxk is offline   Reply With Quote
Old 01-04-2012, 04:18 AM   #8 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 19
francis is on a distinguished road
Default

can someone pls answer this

Thanks

francis is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 405
8 members and 397 guests
13dario13, ChrisYates, fredidf, iOS.Lover, Leslie80, Meoz, Wikiboo, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,670
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Yosh_K
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:38 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0