I'm having an issue which is strange. If I place my bullet shooting code into a touch event, the simulation is not correct (object jitters on contact), however if I just initialize the shot from init, everything is fine. I'm simulating a shot and collision with the following psuedo code.
Bullet.m
// Collision function
static int
myCollision(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
Bullet *b = (Bullet*) data;
[b stopRotation];
NSLog(@"Collision Detected");
return 1;
}
// Initialize sprite, chipmunk body/shape
-(void) init {...}
// Shoot
-(void) shoot:(cpVect)from to:(cpVect)to
{
// Make some calculations, velocity, etc.
[self schedule:@selector (setRotation)];
}
// Set the rotation
-(void) setRotation
{
// Calculate angle, etc...
cpBodySetAngle(myShape->body, angle);
}
There is a collision function there which just NSLogs the collision and also unschedules the rotation schedule, so the body will cease its rotating upon impact.
I have a feeling that my rotation schedule is the culprit here. What its doing is rotating the object as its moving. I want it to stop that behavior upon contact and just be effected by the normal collision. The thing is it works perfectly if I manually generate the shot:
Scene.m
init...
{
// Bullet initialization...
[bullet shoot:cpv(0, 0) to:cpv(100, 200)];
}
When I place the shooting code into the touch event, so I can click to shoot, upon contact the bullet sorta flops/jerks around and basically it seems to be repelled by a force from the contacting body, either a box or the ground.
The only thing that seems to cause this is placing the method in the touchbegin/ended methods. If I take out my rotation schedule, everything works fine as well. I've tried playing with the elasticity, mass, etc. There is some effect, but it isn't telling my why it works in one instance and not the other.
I'm not sure what I'm doing is considered best practice, or wonky, I'm still learning via examples mostly and trying to throw in a wrinkle here and there to challenge myself, I've been at this particular wrinkle for 2 days, so I figure its time to seek advice :)