<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>cocos2d for iPhone &#187; Topic: Collision detection in Chipmunk with dragable Sprite object</title>
		<link>http://www.cocos2d-iphone.org/forum/topic/158</link>
		<description>A fast, easy to use, free, and community supported 2D game engine</description>
		<language>en-US</language>
		<pubDate>Fri, 10 Feb 2012 01:22:40 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.1</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://www.cocos2d-iphone.org/forum/search.php</link>
		</textInput>
		<atom:link href="http://www.cocos2d-iphone.org/forum/rss/topic/158" rel="self" type="application/rss+xml" />

		<item>
			<title>shivaz on "Collision detection in Chipmunk with dragable Sprite object"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/158#post-830</link>
			<pubDate>Thu, 18 Jun 2009 04:45:48 +0000</pubDate>
			<dc:creator>shivaz</dc:creator>
			<guid isPermaLink="false">830@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>This question was asked in the google code forum - and the thread has lots of good information. The setPosition overide is golden.</p>
<p><a href="http://groups.google.com/group/cocos2d-iphone-discuss/browse_thread/thread/ae37e90eac059135/081da9bcefec76ba?#081da9bcefec76ba" rel="nofollow">http://groups.google.com/group/cocos2d-iphone-discuss/browse_thread/thread/ae37e90eac059135/081da9bcefec76ba?#081da9bcefec76ba</a></p>
<p>Justin
</p></description>
		</item>
		<item>
			<title>michaelSvendsen on "Collision detection in Chipmunk with dragable Sprite object"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/158#post-797</link>
			<pubDate>Wed, 17 Jun 2009 23:20:49 +0000</pubDate>
			<dc:creator>michaelSvendsen</dc:creator>
			<guid isPermaLink="false">797@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi Patrick, thanks for the information I'm going to head over there now and take a look.
</p></description>
		</item>
		<item>
			<title>patrickC on "Collision detection in Chipmunk with dragable Sprite object"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/158#post-795</link>
			<pubDate>Wed, 17 Jun 2009 22:26:26 +0000</pubDate>
			<dc:creator>patrickC</dc:creator>
			<guid isPermaLink="false">795@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi there, regarding the dragging of a chipmunk body, you best refer to the chipmunk forum: <a href="http://www.slembcke.net/forums/viewforum.php?f=1" rel="nofollow">http://www.slembcke.net/forums/viewforum.php?f=1</a><br />
There's a class called cpMouse that handles the dragging and dropping of interactive objects, a quick search on the forum just now reveals that the latest SVN trunk of chipmunk is abandoning that, and the author of chipmunk suggests looking at the demo examples of the latest svn trunk to see how dragging is handled there.<br />
so not much help from me, but you'll find lots of information on the chipmunk forum.<br />
Good luck!
</p></description>
		</item>
		<item>
			<title>michaelSvendsen on "Collision detection in Chipmunk with dragable Sprite object"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/158#post-794</link>
			<pubDate>Wed, 17 Jun 2009 21:57:28 +0000</pubDate>
			<dc:creator>michaelSvendsen</dc:creator>
			<guid isPermaLink="false">794@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi Guys,</p>
<p>This is my first time playing around with cocos2D iPhone and the Chipmunk physics engine, and I was wondering if I could get some help.  I have two objects on my screen, one is a marker that should follow the users touch, the other is an object that I would like to move with physics when the marker has collided with it.  I have been trying things for a few hours now, but being new too Obj-C and C, a lot of the information seems to be going over my head.  I have the all the basic stuff set up in my file, chipmunk should be running, and I have the dragging working on my marker Sprite, but if someone could please give me some insight into how I could get my marker to "bump" the second object that would be great.  Below please find the code that I have been using inside of my GameLayer( which extends the cocos2d Layer ).  Thank you for any help that can be provided.  It is greatly appreciated.</p>
<p><code><br />
#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> "GameLayer.h"<br />
#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> "chipmunk.h"<br />
#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> "Director.h"</p>
<p>@implementation GameLayer</p>
<p>@synthesize marker1;<br />
@synthesize sumo1;<br />
@synthesize background;</p>
<p>- (id) init{</p>
<p>	self = [super init];</p>
<p>	if(self != nil){</p>
<p>		isTouchEnabled = YES;</p>
<p>		//create the background<br />
		background = [Sprite spriteWithFile:@"BackGround.png"];<br />
		[background setTransformAnchor:CGPointMake(0, 0)];<br />
		[self addChild:background];</p>
<p>		//create the first marker<br />
		marker1 = [[Marker alloc] init];<br />
		marker1.position = CGPointMake(50, 50);<br />
		[self addChild:marker1];</p>
<p>		sumo1 = [[SumoWrestler alloc] init];<br />
		sumo1.position = CGPointMake(160, 240);<br />
		[self addChild:sumo1];</p>
<p>		[self setupChipmunk];</p>
<p>	}<br />
	return self;<br />
}</p>
<p>#pragma mark chipmuk setup<br />
- (void)setupChipmunk{</p>
<p>	//init chipmunk<br />
	cpInitChipmunk();</p>
<p>	//set up a space to work within<br />
	space = cpSpaceNew();</p>
<p>	//define a gravity vector<br />
	space-&#62;gravity = cpv(0, -50);</p>
<p>	//resize the spatial hash to increase collison detection speed<br />
	//the space to use, average object radius, approx amount of objects<br />
	cpSpaceResizeActiveHash(space, 50.0f, 10);</p>
<p>	//create the body for marker #<a href='http://www.cocos2d-iphone.org/forum/tags/1'>1</a><br />
	marker1Body = cpBodyNew(INFINITY, INFINITY);<br />
	marker1Body-&#62;p = cpv(50, 50);<br />
	cpSpaceAddBody(space, marker1Body);</p>
<p>	//create the shape for marker #<a href='http://www.cocos2d-iphone.org/forum/tags/1'>1</a><br />
	marker1Shape = cpCircleShapeNew(marker1Body, 12.5, cpvzero);<br />
	marker1Shape-&#62;u = 0.5; //friction<br />
	marker1Shape-&#62;data = marker1;<br />
	marker1Shape-&#62;collision_type = 1;</p>
<p>	//add the shape to the space<br />
	cpSpaceAddShape(space, marker1Shape);</p>
<p>	//setup the sumo body<br />
	sumo1Body = cpBodyNew(INFINITY, INFINITY);<br />
	sumo1Body-&#62;p = cpv(160, 240);<br />
	cpSpaceAddBody(space, sumo1Body);</p>
<p>	//set up the shape for sumo1<br />
	sumo1Shape = cpCircleShapeNew(sumo1Body, 50, cpvzero);<br />
	sumo1Shape-&#62;u = 0.8;<br />
	sumo1Shape-&#62;data = sumo1;<br />
	sumo1Shape-&#62;collision_type = 0;</p>
<p>	//set up collision data<br />
	cpSpaceAddCollisionPairFunc(space, 0, 1, &#38;collFunc, self);</p>
<p>	//set up the timer to loop over the tick method<br />
	[NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(tick:) userInfo:nil repeats:YES];</p>
<p>}</p>
<p>- (void)tick:(NSTimer *)timer{<br />
	//cpSpaceHashEach(space-&#62;activeShapes, &#38;collFunc, nil);<br />
	cpSpaceStep(space, (1.0f/30.0f) );</p>
<p>}</p>
<p>static int collFunc(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)<br />
{<br />
	NSLog(@"collision function");<br />
	if(a-&#62;collision_type != 1){<br />
		return 0;<br />
	}else{<br />
		return 1;<br />
	}</p>
<p>}</p>
<p>#pragma mark TOUCHES<br />
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{<br />
	UITouch *myTouch = [touches anyObject];<br />
	CGPoint location = [myTouch locationInView:[myTouch view]];<br />
	NSLog(@"touch has begain at :: x:%f, y:%f", location.x, location.y);<br />
	return YES;<br />
}</p>
<p>- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{</p>
<p>	UITouch *myTouch = [touches anyObject];<br />
	CGPoint location = [myTouch locationInView:[myTouch view]];</p>
<p>	cpShape *shape = marker1Shape;</p>
<p>	[shape-&#62;data setPosition: [[Director sharedDirector] convertCoordinate:location] ];</p>
<p>	return YES;<br />
}</p>
<p>- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{<br />
	UITouch *myTouch = [touches anyObject];<br />
	CGPoint location = [myTouch locationInView:[myTouch view]];<br />
	NSLog(@"touch has end at :: x:%f, y:%f", location.x, location.y);<br />
	return YES;<br />
}</p>
<p>#pragma mark DEALLOC<br />
- (void) dealloc{<br />
	[sumo1 dealloc];<br />
	[background dealloc];<br />
	[marker1 dealloc];<br />
	[super dealloc];<br />
}</p>
<p>@end<br />
</code></p>
<p>Thanks,</p>
<p>Michael
</p></description>
		</item>

	</channel>
</rss>

