Need help to get started: Catching simple border collision

Official forum for the Chipmunk2D Physics Library.

Need help to get started: Catching simple border collision

Postby ohrobot » Thu May 02, 2013 1:51 pm

I need a bit help to get started.

I am trying to adapt the SimpleObjectiveChipmunk example to my use case which is a board with multiple cards on it. For the beginning I only want to get a collision message when the a card hits the bounds of the board (by being dragged on it).

For the dragging I currently don't use physics/Chipmunk but a simple UIPanGestureRecognizer in each card firing a handlePan function (see below).

Image

BoardViewController

with the ChipmunkSpace and the target beginCollision method

Code: Select all
- (id)init {
...
        space = [[ChipmunkSpace alloc] init];
        [space addBounds:self.view.bounds thickness:10.0f elasticity:1.0f friction:1.0f layers:CP_ALL_LAYERS group:CP_NO_GROUP collisionType:@"borderType"];
        [space addCollisionHandler:self
                             typeA:[CardViewController class] typeB:@"borderType"
                             begin:@selector(beginCollision:space:)
                          preSolve:nil
                         postSolve:nil
                          separate:nil
         ];

        CardViewController *testCard = [[CardViewController alloc]initOnPositionX:50.0 andY:50.0];
        [testCard setBoardReference:self];
        [self addChildViewController:testCard];
        [self.view addSubview:testCard.view];
       
        [space add:testCard];
...
}

- (bool)beginCollision:(cpArbiter*)arbiter space:(ChipmunkSpace*)space {
    LGRED(3,@"COLLISION!");
    return YES;
}


CardViewController
UIViewController that conforms to the ChipmunkObject protocol and added to the board as SubView, ChildViewController.

Code: Select all
-(id)init {
...
    cpFloat mass = 1.0f;
      cpFloat moment = cpMomentForBox(mass, 100, 100);
      body = [[ChipmunkBody alloc] initWithMass:mass andMoment:moment];
      body.pos = cpv(200.0f, 200.0f);
      ChipmunkShape *shape = [ChipmunkPolyShape boxWithBody:body width:100 height:100];
      shape.elasticity = 0.3f;
      shape.friction = 0.3f;
      shape.collisionType = [CardViewController class];
      shape.data = self;
      chipmunkObjects = [[NSArray alloc] initWithObjects:body, shape, nil];
}

-(void)handlePan:(UIPanGestureRecognizer*)thisPanRecognizer {   
    if (thisPanRecognizer.state == UIGestureRecognizerStateChanged) {
        CGPoint center = thisPanRecognizer.view.center;
        CGPoint translation = [thisPanRecognizer translationInView:thisPanRecognizer.view];
        center = CGPointMake(center.x + translation.x,
                             center.y + translation.y);
        thisPanRecognizer.view.center = center;
...
}


Problem: The "beginCollision" method does not get called. Did I miss something? I am trying to keep it really basic for the beginning.

I am using the Indie license with the ObjectiveChipmunk library. I'd appreciate any help on this.
ohrobot
 
Posts: 4
Joined: Tue Apr 30, 2013 4:47 pm

Re: Need help to get started: Catching simple border collisi

Postby slembcke » Thu May 02, 2013 2:50 pm

You are setting the position of the UIView and not the ChipmunkBody. Chipmunk has no way of knowing when you update the position of an object for a different library. The position from the physics is copied to the view every frame after stepping. You need to set the position of the body instead and let the DisplayLink callback copy the body's position to the view.

Now, while the above will let you know when the collision occurred, the physics won't work very well. The problem is that by setting the position of the body you are causing it to "teleport" to it's new position. If you teleport the card into the wall, Chipmunk will try and push it out a little each frame. This sort of looks okay. The big problem is when you repeatedly set the position of a body to be in the wall using the touch events. The body will jitter as the position you are assigning to it fights with what Chipmunk is trying to tell it to do.

What you really want to do is use the ChipmunkMultiGrab class. It automatically connects joints from the touches to the bodies and moves them around using real forces. Then when you try and drag a card into the immovable wall, Chipmunk can generate an even large force to prevent it from going through.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Need help to get started: Catching simple border collisi

Postby ohrobot » Thu May 02, 2013 3:26 pm

Thanks so much for the quick response. That makes sense to me.

Is there a good way tom display the body/shapes in the UIView (e.g. as coloured) areas? That would help me debugging a lot.
ohrobot
 
Posts: 4
Joined: Tue Apr 30, 2013 4:47 pm

Re: Need help to get started: Catching simple border collisi

Postby slembcke » Thu May 02, 2013 3:34 pm

ohrobot wrote:Is there a good way tom display the body/shapes in the UIView (e.g. as coloured) areas? That would help me debugging a lot.


Unfortunately not easily when using UIKit. That's one of the biggest downsides of using Chipmunk with UIKit in my opinion. I think the best you could do is to make a transparent view with a custom drawing method that sits over your regular one to draw the outlines. Quartz is great if you want quality, and bad if you want good performance. The compositing cost wouldn't be very friendly either.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Need help to get started: Catching simple border collisi

Postby slembcke » Fri May 03, 2013 10:56 am

I'm not sure what you are describing in the second option, but it sounds like you might be over-thinking things or trying to over-engineer a solution. The first option is roughly what I do.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm


Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 0 guests