Page 1 of 1

collision been ignored in an unpredictabble manner

Posted: Wed Dec 02, 2009 11:20 pm
by dilsonalkmim
Hi guys,

I have a problem with collisions, in the following scenario:

=> I have six segment shapes (lines, with ratio zero), for wich the body have infinity mass; These lines are been created like that

Code: Select all

   cpBody *bodyLine;
	cpShape *shapeLine;
	
	bodyLine = cpBodyNew(INFINITY, INFINITY);
	
	shapeLine = cpSegmentShapeNew(bodyLine, origem, destino, 0);
	shapeLine->collision_type = 0;
	shapeLine->e = 1.0;
	shapeLine->u = 0.2;
	cpSpaceAddStaticShape(space, shapeLine);
=> I have an object, created as a finity mass body, for which i'm attaching a circle shape, with a moment calculated by cpMomentForCircle passing the values related to the body. Both, the body and the shape are been added to the space, with cpSpaceAddBody and cpSpaceAddShape. For the line segments only the shapes are added to the space:

Code: Select all

cpBody *body = cpBodyNew(m, cpMomentForCircle(100, 50, 50, cpvzero)); 
	body->p = cpv(160, 240)
	cpSpaceAddBody(space, body);
		
	shape = cpCircleShapeNew(body, 50, cpvzero);
	shape->e = 1;
	shape->u = 0.8;
	shape->collision_type = 1;

        cpSpaceAddShape(space, shape);
=> In response to the user interaction, a velocity value is applied to the body, as

Code: Select all

 shape->body->v = velocity_vector;
Everything works nice, but in a few moments, the floating body traverse the static one as if it don't be there. The collision does not happens!!!
I placed a NSLog command to verify if this is due to a specific velocity value, but for a same value, sometimes the collision happens, sometime don't.

I have modified almost every parameter passed to the chipmunk, but unsuccessful.

I have no idea what is wrong.
Can you guys help me with this issue??

Thanks!
Dilson

Re: collision been ignored in an unpredictabble manner

Posted: Thu Dec 03, 2009 10:22 am
by slembcke
Are you setting the velocity every frame? That can be problematic.

How big is the velocity? If it's too big, it could be passing right through the line in a single step. How big are the steps you are using for each cpSpaceStep()?

Re: collision been ignored in an unpredictabble manner

Posted: Thu Dec 03, 2009 2:34 pm
by dilsonalkmim
No, i'm setting the velocity only once. After a while, when the velocity decrease to less than a fixed small value, the body is stoped resetting the forces and setting the velocity to zero ( (0,0) ).
The velocity has a length of about 9,25 * vector of length 215 units, and the step rate is 1/60. Its to high ??
I have tried step rate values ranging from 1/400 to 1/10, but with no success.

Tks a lot!
Dilson

Re: collision been ignored in an unpredictabble manner

Posted: Thu Dec 03, 2009 4:18 pm
by slembcke
Hmm. Sounds like that's not the issue then. Are you sure you aren't accidentally freeing the object somewhere?

Re: collision been ignored in an unpredictabble manner

Posted: Fri Dec 04, 2009 8:28 am
by dilsonalkmim
What did you mean with "Freeing the object"? cpBodyFree?? I don't do that.
I have noted the problem ocurr when the floating body is close to the static line.

My space have no gravity, but damping value of 0.01, so the body deaccelerate with time.

The code that control the movement look like this:

cpSpaceHashEach isn't called, because i have my own timer calling cpSpaceStep method and my updatePositioning method.

Code: Select all

-(void)stopBody {
	myBody->v = cpv(0,0);
	cpBodyResetForces(myBody);
}

-(void) updatePositionig {
	//set body representation position to myBody->p
	//set body representation rotation to myBody->a
}

-(void)run {
	[self disableUserInteraction];
	
	myBody->v = velocity_value;

	timer = [NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(nextStep) userInfo:nil repeats:YES];
}

-(void)nextStep {

	cpSpaceStep(space, 1.0f/60.0f);

	//cpSpaceHashEach(space->activeShapes, &updatePositioning, nil);
	[self updatePositioning];

	//stoped is calculated comparing actual velocity value against a fixed small value (cpvlength(velocity) < 15)
	if(stoped) {
		[self stopBody];

		[timer invalidate];
		timer = nil;

		[self enableUserInteraction];
	}
}

-(void)userEvent {
	[self run];
}
Tks!

Re: collision been ignored in an unpredictabble manner

Posted: Fri Dec 04, 2009 10:11 am
by slembcke
You are explicitly setting the position and angle in your updatePositioning method. That can be bad. What exactly are you doing in that method.

Re: collision been ignored in an unpredictabble manner

Posted: Fri Dec 04, 2009 11:01 am
by dilsonalkmim
I have an UIView *myBodyView, that is updated with position/angle taken from cpBody

Code: Select all

  -(void) updatePositioning {
      myBodyView.center = myBody->p;
      myBodyView.transform =  CGAffineTransformMakeRotation(myBody->a);
   }
 

Re: collision been ignored in an unpredictabble manner

Posted: Fri Dec 04, 2009 11:37 am
by slembcke
Ah, just updating graphics based on the body. I thought you meant you were doing body->p = something.

Can you make a video of the problem? I'm not sure I have any other ideas without seeing exactly what is happening.