Multigrab without Cocos2D

Official forum for the Chipmunk2D Physics Library.
Post Reply
emilebennett
Posts: 4
Joined: Wed Oct 12, 2011 6:26 am
Contact:

Multigrab without Cocos2D

Post by emilebennett »

Hi Guys,

I'm developing a simple app that needs to spawn and drag multiple UIImageViews with some simple physics so they can interact with each other. Objective-Chipmunk looks like the best bet however I'm having trouble implementing the MultiGrab functionality and there isn't much in the way of documentation.

The Multgrab demo uses Cocos2D - and I'm trying to implement it without Cocos2D as I don't really want the overhead of having to use it (I don't need it in this app at all).

Does anyone know if I must use Cocos2D or whether I can in fact use Multigrab on it's own?

I'm creating a space:

Code: Select all

space = [[ChipmunkSpace alloc] init]; ... setup
creating the multigrab class:

Code: Select all

multigrab = [[ChipmunkMultiGrab alloc] initForSpace:space withSmoothing:cpfpow(0.8, 60.0) withGrabForce:20000];
adding an object (shape/body/related image) to the space:

Code: Select all

FallingButton *fallingButton = [[FallingButton alloc] initWithPosition:cpv(200, 200) inSpace:space]; ... setup
and tracking touched in the main view:

Code: Select all

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touch began");
    for (UITouch *touch in touches) 
    {
        CGPoint location = [touch locationInView:[touch view]];
        [multigrab beginLocation:location];
    }
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touch moved");
    for (UITouch *touch in touches) 
    {
        CGPoint location = [touch locationInView:[touch view]];
        [multigrab updateLocation:location];
    }
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touch ended");
    for (UITouch *touch in touches) 
    {
        CGPoint location = [touch locationInView:[touch view]];
        [multigrab endLocation:location];
    }
}
the touches are being picked up by the main view, but the objects are not moving.

Any help would be appreciated, Cheers

Emile
emilebennett
Posts: 4
Joined: Wed Oct 12, 2011 6:26 am
Contact:

Re: Multigrab without Cocos2D

Post by emilebennett »

Quick update,

In the 'touchesMoved' method I've now logged the x position of the body I am trying to move. Although it doesn't look like it's moving, the log tells differently :
2011-10-12 13:43:00.827 ObjectiveChipmunkTest[43058:207] touch moved
2011-10-12 13:43:00.827 ObjectiveChipmunkTest[43058:207] pos = 200.000000
2011-10-12 13:43:00.843 ObjectiveChipmunkTest[43058:207] touch moved
2011-10-12 13:43:00.844 ObjectiveChipmunkTest[43058:207] pos = 199.994446
2011-10-12 13:43:00.860 ObjectiveChipmunkTest[43058:207] touch moved
2011-10-12 13:43:00.860 ObjectiveChipmunkTest[43058:207] pos = 199.988892
2011-10-12 13:43:00.877 ObjectiveChipmunkTest[43058:207] touch moved
2011-10-12 13:43:00.878 ObjectiveChipmunkTest[43058:207] pos = 199.983337
2011-10-12 13:43:00.894 ObjectiveChipmunkTest[43058:207] touch moved
2011-10-12 13:43:00.895 ObjectiveChipmunkTest[43058:207] pos = 199.977783
2011-10-12 13:43:00.911 ObjectiveChipmunkTest[43058:207] touch moved
2011-10-12 13:43:00.911 ObjectiveChipmunkTest[43058:207] pos = 199.972229
2011-10-12 13:43:00.927 ObjectiveChipmunkTest[43058:207] touch moved
2011-10-12 13:43:00.927 ObjectiveChipmunkTest[43058:207] pos = 199.966675
2011-10-12 13:43:00.944 ObjectiveChipmunkTest[43058:207] touch moved
2011-10-12 13:43:00.944 ObjectiveChipmunkTest[43058:207] pos = 199.961121
2011-10-12 13:43:00.960 ObjectiveChipmunkTest[43058:207] touch moved
2011-10-12 13:43:00.961 ObjectiveChipmunkTest[43058:207] pos = 199.955566
Is this something to do with needing to convert my UIView positions to openGL positions? I noticed this is implemented in the cocos2D/multigrab example

Code: Select all

TouchLocation(UITouch *touch)
{
	return [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
}
but figured as I'm not working in cocos2D it shouldn't be required...
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Multigrab without Cocos2D

Post by slembcke »

Hmm. If you are using UIKit, the coordinates should be relative to the parent view.

I don't see any begin touch events in the log, are they being swallowed by the UIImageViews maybe? Is the beginLocation: method returning true?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
emilebennett
Posts: 4
Joined: Wed Oct 12, 2011 6:26 am
Contact:

Re: Multigrab without Cocos2D

Post by emilebennett »

Hi thanks for the reply. Yep touchesBegan / moved / ended all get called...it's weird right?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Multigrab without Cocos2D

Post by slembcke »

Does the begin method return true though? It returns true when it grabs an object.

If it is returning true, then it's grabbing the object but doesn't have enough force to noticeably pull on the object. If it returns false, then you might have a problem with your coordinates needing an extra conversion step to get them in absolute coordinates of the space.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
emilebennett
Posts: 4
Joined: Wed Oct 12, 2011 6:26 am
Contact:

Re: Multigrab without Cocos2D

Post by emilebennett »

Hi mate,

I've be a major fool! Where you said "then it's grabbing the object but doesn't have enough force to noticeably pull on the object" it reminded me that I'd hardcoded 1000.0f for the mass of the button when I was experimenting before using the multigrab class.

So, now that I've stopped being an idiot it works great :D

Thanks for all your hard work on this project - we'll be purchasing the pro library once we get this initial R&D phase out the way.

Cheers!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Multigrab without Cocos2D

Post by slembcke »

Ah, yup. That would do it. Cheers. ;)
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 31 guests