Page 1 of 1

Cocos2d V3.1 Vehicle touching ground?

PostPosted: Wed Aug 13, 2014 2:12 am
by polykiss
I know this is not the first time this question has been asked of a physics engine, but I have been unable to make this work in Cocos 3.1 (iphone / swift)

I am making a physics based driving game, and when my vehicle is in the air I want the gas and brake to add a little rotation to the vehicle so you can control its angle. I have the collisionType for the wheels set to "wheel" and I have a CCNode based class acting as a delegate listening for:

-(void)ccPhysicsCollisionPostSolve:(CCPhysicsCollisionPair )pair wheel:(CCNode )nodeA wildcard:(CCNode *)nodeB {

this gets called fine, and my plan was to set a variable to true in this method, but can not figure out where to reset the property for each cycle.

I tried adding an -(void)update:(CCTime)delta method, but it does not get called (which i find odd, I assumed this was all I would need to do in V3.1

Thanks in advance.

Re: Cocos2d V3.1 Vehicle touching ground?

PostPosted: Wed Aug 13, 2014 9:11 am
by slembcke
I came across your question on Stack Overflow first:
http://stackoverflow.com/questions/2527 ... 3#25287563

It is a bit weird that your -update method does not get called. That's best asked on the Cocos forums though.

Re: Cocos2d V3.1 Vehicle touching ground?

PostPosted: Wed Aug 13, 2014 2:23 pm
by polykiss
Thanks, you are very quick.

I resolved the update issue (I had not added the node to the parent, so update was not being called), but I am still having issues with the class. ccPhysicsCollisionSeparate is never called, so as a result if I leave out the update _wheelContacts just climbs by two at a time forever. When I add a reset into the update (_wheelContacts = 0) whenever I check for _wheelContacts I get a 0.



#import "PKTruckCollisionDelegate.h"

@implementation PKTruckCollisionDelegate {
int _wheelContacts;
}

@synthesize wheelContacts;


-(id)init {
if(self = [super init]) {
_wheelContacts = 0;
}

return self;
}

-(void)ccPhysicsCollisionPostSolve:(CCPhysicsCollisionPair *)pair truckWheel:(CCNode *)nodeA wildcard:(CCNode *)nodeB {
_wheelContacts ++;
}

-(void)ccPhysicsCollisionSeparate:(CCPhysicsCollisionPair *)pair truckWheel:(CCNode *)nodeA typeB:(CCNode *)nodeB {
CCLOG(@"seperate");
_wheelContacts --;
}


-(void)update:(CCTime)delta {
_wheelContacts = 0;
}


@end

Re: Cocos2d V3.1 Vehicle touching ground?

PostPosted: Wed Aug 13, 2014 3:37 pm
by polykiss
I just updated the separate method to use wildcard and now it is being called. The problem is separate is being called at the correct time (when the wheels leave the ground) but the collisionPostSolve method is being called what looks like every frame (maybe because the wheels are rolling?)


here is the updated, and working, ccPhysicsCollisionSeparate method:

-(void)ccPhysicsCollisionSeparate:(CCPhysicsCollisionPair *)pair truckWheel:(CCNode *)nodeA wildCard:(CCNode *)nodeB {
CCLOG(@"seperate");
_wheelContacts --;
}

Re: Cocos2d V3.1 Vehicle touching ground?

PostPosted: Thu Aug 14, 2014 12:26 pm
by polykiss
I just realized this post is totally in the wrong forum. Should I create a new version in the other forum?

Re: Cocos2d V3.1 Vehicle touching ground?

PostPosted: Thu Aug 21, 2014 3:12 pm
by slembcke
Hey sorry. Have been busy with work and haven't been checking the forums. (again...)

There are 4 collision events begin, pre-solve, post-solve and separate. You want to be using begin and separate. Post-solve is called every frame after running the solver so you can find out how much force was applied between two objects.