Skip to content

Commit 4b5495c

Browse files
committed
Merge pull request #754 from jtwigg/develop
Support for new joints types in CCBReader
2 parents f46bd83 + eaddfa4 commit 4b5495c

File tree

3 files changed

+300
-8
lines changed

3 files changed

+300
-8
lines changed

cocos2d-ui/CCBReader/CCBReader.m

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -933,17 +933,14 @@ -(void)readJoints
933933
{
934934
int numJoints = readIntWithSign(self, NO);
935935

936-
NSMutableArray * joints = [NSMutableArray array];
937-
938936
for (int i =0; i < numJoints; i++)
939937
{
940-
id joint = [self readJoint];
941-
[joints addObject:joint];
938+
[self readJoint];
942939
}
943940
}
944941

945942

946-
-(CCPhysicsJoint*)readJoint
943+
-(void)readJoint
947944
{
948945

949946
CCPhysicsJoint * joint = nil;
@@ -964,12 +961,71 @@ -(CCPhysicsJoint*)readJoint
964961
float breakingForce = [properties[@"breakingForceEnabled"] boolValue] ? [properties[@"breakingForce"] floatValue] : INFINITY;
965962
float maxForce = [properties[@"maxForceEnabled"] boolValue] ? [properties[@"maxForce"] floatValue] : INFINITY;
966963
bool collideBodies = [properties[@"collideBodies"] boolValue];
964+
float referenceAngle = [properties[@"referenceAngle"] floatValue];
965+
referenceAngle = CC_DEGREES_TO_RADIANS(referenceAngle);
967966

968967
if([className isEqualToString:@"CCPhysicsPivotJoint"])
969968
{
970-
CGPoint anchorA = [properties[@"anchorA"] CGPointValue];
969+
if([properties[@"motorEnabled"] boolValue])
970+
{
971+
float motorRate = properties[@"motorRate"] ? [properties[@"motorRate"] floatValue] : 1.0f;
972+
CCPhysicsJoint * motorJoint = [CCPhysicsJoint connectedMotorJointWithBodyA:nodeBodyA.physicsBody bodyB:nodeBodyB.physicsBody rate:motorRate];
973+
974+
motorJoint.maxForce = maxForce;
975+
motorJoint.breakingForce = breakingForce;
976+
motorJoint.collideBodies = collideBodies;
977+
}
978+
979+
if([properties[@"dampedSpringEnabled"] boolValue])
980+
{
981+
float restAngle = properties[@"dampedSpringRestAngle"] ? [properties[@"dampedSpringRestAngle"] floatValue] : 0.0f;
982+
restAngle = CC_DEGREES_TO_RADIANS(restAngle);
983+
float stiffness = properties[@"dampedSpringStiffness"] ? [properties[@"dampedSpringStiffness"] floatValue] : 1.0f;
984+
stiffness *= 1000.0f;
985+
float damping = properties[@"dampedSpringDamping"] ? [properties[@"dampedSpringDamping"] floatValue] : 4.0f;
986+
damping *= 100.0f;
987+
988+
CCPhysicsJoint * rotarySpringJoint = [CCPhysicsJoint connectedRotarySpringJointWithBodyA:nodeBodyA.physicsBody bodyB:nodeBodyB.physicsBody restAngle:restAngle stifness:stiffness damping:damping];
989+
990+
rotarySpringJoint.maxForce = maxForce;
991+
rotarySpringJoint.breakingForce = breakingForce;
992+
rotarySpringJoint.collideBodies = collideBodies;
993+
}
994+
995+
996+
if([properties[@"limitEnabled"] boolValue])
997+
{
998+
float limitMax = properties[@"limitMax"] ? [properties[@"limitMax"] floatValue] : 90.0f;
999+
limitMax = CC_DEGREES_TO_RADIANS(limitMax);
1000+
1001+
float limitMin = properties[@"limitMin"] ? [properties[@"limitMin"] floatValue] : 0;
1002+
limitMin = CC_DEGREES_TO_RADIANS(limitMin);
1003+
1004+
CCPhysicsJoint * limitJoint = [CCPhysicsJoint connectedRotaryLimitJointWithBodyA:nodeBodyA.physicsBody bodyB:nodeBodyB.physicsBody min:limitMin max:limitMax];
1005+
1006+
limitJoint.maxForce = maxForce;
1007+
limitJoint.breakingForce = breakingForce;
1008+
limitJoint.collideBodies = collideBodies;
1009+
}
1010+
1011+
if([properties[@"ratchetEnabled"] boolValue])
1012+
{
1013+
float ratchetValue = properties[@"ratchetValue"] ? [properties[@"ratchetValue"] floatValue] : 30.0f;
1014+
ratchetValue = CC_DEGREES_TO_RADIANS(ratchetValue);
1015+
float ratchetPhase = properties[@"ratchetPhase"] ? [properties[@"ratchetPhase"] floatValue] : 0.0f;
1016+
ratchetPhase = CC_DEGREES_TO_RADIANS(ratchetPhase);
1017+
1018+
CCPhysicsJoint * ratchetJoint = [CCPhysicsJoint connectedRatchetJointWithBodyA:nodeBodyA.physicsBody bodyB:nodeBodyB.physicsBody phase:ratchetPhase ratchet:ratchetValue];
1019+
1020+
ratchetJoint.maxForce = maxForce;
1021+
ratchetJoint.breakingForce = breakingForce;
1022+
ratchetJoint.collideBodies = collideBodies;
1023+
1024+
}
9711025

1026+
CGPoint anchorA = [properties[@"anchorA"] CGPointValue];
9721027
joint = [CCPhysicsJoint connectedPivotJointWithBodyA:nodeBodyA.physicsBody bodyB:nodeBodyB.physicsBody anchorA:anchorA];
1028+
9731029
}
9741030
else if([className isEqualToString:@"CCPhysicsSpringJoint"])
9751031
{
@@ -982,6 +1038,7 @@ -(CCPhysicsJoint*)readJoint
9821038

9831039
BOOL restLengthEnabled = [properties[@"restLengthEnabled"] boolValue];
9841040
float restLength = restLengthEnabled? [properties[@"restLength"] floatValue] : distance;
1041+
9851042
float stiffness = [properties[@"stiffness"] floatValue];
9861043
float damping = [properties[@"damping"] floatValue];
9871044

@@ -1016,13 +1073,12 @@ -(CCPhysicsJoint*)readJoint
10161073
}
10171074
else
10181075
{
1019-
return nil;
1076+
return;
10201077
}
10211078
joint.maxForce = maxForce;
10221079
joint.breakingForce = breakingForce;
10231080
joint.collideBodies = collideBodies;
10241081
[joint resetScale:NodeToPhysicsScale(nodeBodyA).x];
1025-
return joint;
10261082

10271083
}
10281084

cocos2d/CCPhysicsJoint.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,65 @@ Joints cannot be reactivated once they are invalidated.
9696
anchorA:(CGPoint)anchorA anchorB:(CGPoint)anchorB
9797
restLength:(CGFloat)restLength stiffness:(CGFloat)stiffness damping:(CGFloat)damping;
9898

99+
100+
/**
101+
* Creates and returns a rotary spring joint between the two bodies. No anchor points are specified as this joint can be used in conjunction with a pivot joint to make a springing pivot joint.
102+
*
103+
* @param bodyA Body A.
104+
* @param bodyB Body B.
105+
* @param restAngle Rest angle.
106+
* @param stiffness Spring stiffness.
107+
* @param damping Sprin damping.
108+
*
109+
* @return The CCPhysicsJoint Object.
110+
*/
111+
+(CCPhysicsJoint *)connectedRotarySpringJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB restAngle:(CGFloat)restAngle stifness:(CGFloat)stiffness damping:(CGFloat)damping;
112+
113+
114+
115+
/**
116+
* Creates and returns a Motor joint between the two bodies. No anchor points are specified as this joint can be used in conjunction with a pivot joint to make a motor around a pivot point.
117+
*
118+
* @param bodyA Body A.
119+
* @param bodyB Body B.
120+
* @param rate Rate at which the rotate relative to each other. Negative values to reverse direction.
121+
*
122+
* @return The CCPhysicsJoint Object.
123+
*/
124+
+(CCPhysicsJoint *)connectedMotorJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB
125+
rate:(CGFloat)rate;
126+
127+
128+
/**
129+
* Creates and returns joint whereby the angle of rotation between too bodies is limited. No anchor points are specified as this joint can be used in conjunction with a pivot joint to make the pivots range of motion limited.
130+
*
131+
* @param bodyA Body A.
132+
* @param bodyB Body B.
133+
* @param min Minimum angle in radians.
134+
* @param max Maximum angle in radians.
135+
*
136+
* @return The CCPhysicsJoint Object.
137+
*/
138+
+(CCPhysicsJoint *)connectedRotaryLimitJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB
139+
min:(CGFloat)min
140+
max:(CGFloat)max;
141+
142+
143+
144+
/**
145+
* Creates and returns ratchet jointwhereby the angle of rotation between too bodies can go forwards smoothely, but the backwards motion is clipped at 'ratchet' intervals. No anchor points are specified as this joint can be used in conjunction with a pivot joint to ratchet its range of motion.
146+
* @param bodyA Body A.
147+
* @param bodyB Body B.
148+
* @param phase Phase angle in Radians [0, 2 PI] describing where within the rathet interval the joint is located.
149+
* @param ratchet Ratchet interval angle in radians.
150+
*
151+
* @return The CCPhysicsJoint Object.
152+
*/
153+
+(CCPhysicsJoint *)connectedRatchetJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB
154+
phase:(CGFloat)phase
155+
ratchet:(CGFloat)ratchet;
156+
157+
99158
/// -----------------------------------------------------------------------
100159
/// @name Accessing Physics Joint Attributes
101160
/// -----------------------------------------------------------------------

cocos2d/CCPhysicsJoint.m

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ @interface CCPhysicsSpringJoint : CCPhysicsJoint
4848
-(id)initWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB anchorA:(CGPoint)anchorA anchorB:(CGPoint)anchorB restLength:(CGFloat)restLength stiffness:(CGFloat)stiffness damping:(CGFloat)damping;
4949
@end
5050

51+
@interface CCPhysicsRotarySpring : CCPhysicsJoint
52+
-(id)initWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB restAngle:(cpFloat)restAngle stifness:(cpFloat)stiffness damping:(cpFloat)damping;
53+
@end
54+
55+
@interface CCPhysicsMotorJoint : CCPhysicsJoint
56+
-(id)initWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB rate:(cpFloat)rate;
57+
@end
58+
59+
@interface CCPhysicsRatchet : CCPhysicsJoint
60+
-(id)initWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB phase:(cpFloat)phase ratchet:(cpFloat)ratchet;
61+
@end
62+
63+
@interface CCPhysicsRotaryLimitJoint : CCPhysicsJoint
64+
-(id)initWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB min:(cpFloat)min max:(cpFloat)max;
65+
@end
5166

5267
@implementation CCPhysicsJoint
5368
{
@@ -124,6 +139,62 @@ +(CCPhysicsJoint *)connectedSpringJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(C
124139
return joint;
125140
}
126141

142+
143+
144+
+(CCPhysicsJoint *)connectedRotarySpringJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB
145+
restAngle:(CGFloat)restAngle
146+
stifness:(CGFloat)stiffness
147+
damping:(CGFloat)damping
148+
{
149+
CCPhysicsRotarySpring * joint = [[CCPhysicsRotarySpring alloc] initWithBodyA:bodyA bodyB:bodyB restAngle:restAngle stifness:stiffness damping:damping];
150+
151+
[bodyA addJoint:joint];
152+
[bodyB addJoint:joint];
153+
[joint addToPhysicsNode:bodyA.physicsNode];
154+
return joint;
155+
}
156+
157+
158+
+(CCPhysicsJoint *)connectedMotorJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB
159+
rate:(CGFloat)rate
160+
{
161+
CCPhysicsMotorJoint * joint = [[CCPhysicsMotorJoint alloc] initWithBodyA:bodyA bodyB:bodyB rate:rate];
162+
163+
[bodyA addJoint:joint];
164+
[bodyB addJoint:joint];
165+
[joint addToPhysicsNode:bodyA.physicsNode];
166+
return joint;
167+
}
168+
169+
170+
171+
+(CCPhysicsJoint *)connectedRotaryLimitJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB
172+
min:(cpFloat)min
173+
max:(cpFloat)max
174+
{
175+
CCPhysicsRotaryLimitJoint * joint = [[CCPhysicsRotaryLimitJoint alloc] initWithBodyA:bodyA bodyB:bodyB min:min max:max];
176+
177+
[bodyA addJoint:joint];
178+
[bodyB addJoint:joint];
179+
[joint addToPhysicsNode:bodyA.physicsNode];
180+
return joint;
181+
}
182+
183+
184+
+(CCPhysicsJoint *)connectedRatchetJointWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB
185+
phase:(cpFloat)phase
186+
ratchet:(cpFloat)ratchet
187+
{
188+
CCPhysicsRatchet * joint = [[CCPhysicsRatchet alloc] initWithBodyA:bodyA bodyB:bodyB phase:phase ratchet:ratchet];
189+
190+
[bodyA addJoint:joint];
191+
[bodyB addJoint:joint];
192+
[joint addToPhysicsNode:bodyA.physicsNode];
193+
return joint;
194+
}
195+
196+
197+
127198
-(CCPhysicsBody *)bodyA {return self.constraint.bodyA.userData;}
128199
//-(void)setBodyA:(CCPhysicsBody *)bodyA {NYI();}
129200

@@ -212,6 +283,32 @@ -(void)willAddToPhysicsNode:(CCPhysicsNode *)physics
212283

213284

214285

286+
@implementation CCPhysicsRotarySpring{
287+
ChipmunkDampedRotarySpring *_constraint;
288+
289+
}
290+
291+
-(id)initWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB restAngle:(cpFloat)restAngle stifness:(cpFloat)stiffness damping:(cpFloat)damping
292+
{
293+
if((self = [super init])){
294+
_constraint = [ChipmunkDampedRotarySpring dampedRotarySpringWithBodyA:bodyA.body bodyB:bodyB.body restAngle:restAngle stiffness:stiffness damping:damping];
295+
_constraint.userData = self;
296+
297+
}
298+
299+
return self;
300+
}
301+
302+
-(ChipmunkConstraint *)constraint {return _constraint;}
303+
304+
-(void)willAddToPhysicsNode:(CCPhysicsNode *)physics
305+
{
306+
307+
}
308+
309+
@end
310+
311+
215312
@implementation CCPhysicsPinJoint {
216313
ChipmunkPinJoint *_constraint;
217314
CGPoint _anchorA, _anchorB;
@@ -334,7 +431,87 @@ -(void)setScale:(float)_scale
334431

335432
@end
336433

434+
@implementation CCPhysicsMotorJoint
435+
{
436+
ChipmunkSimpleMotor *_constraint;
437+
}
438+
439+
-(id)initWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB rate:(cpFloat)rate
440+
{
441+
if((self = [super init])){
442+
_constraint = [ChipmunkSimpleMotor simpleMotorWithBodyA:bodyA.body bodyB:bodyB.body rate:rate];
443+
_constraint.userData = self;
444+
}
445+
446+
return self;
447+
}
448+
449+
-(ChipmunkConstraint *)constraint {return _constraint;}
450+
451+
-(void)willAddToPhysicsNode:(CCPhysicsNode *)physics
452+
{
453+
454+
}
455+
456+
@end
457+
458+
@implementation CCPhysicsRatchet
459+
{
460+
ChipmunkRatchetJoint * _constraint;
461+
}
462+
463+
-(id)initWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB phase:(cpFloat)phase ratchet:(cpFloat)ratchet
464+
{
465+
if((self = [super init])){
466+
_constraint = [ChipmunkRatchetJoint ratchetJointWithBodyA:bodyA.body bodyB:bodyB.body phase:phase ratchet:ratchet];
467+
_constraint.userData = self;
468+
}
469+
470+
return self;
471+
}
472+
473+
-(ChipmunkConstraint *)constraint {return _constraint;}
474+
475+
-(void)willAddToPhysicsNode:(CCPhysicsNode *)physics
476+
{
477+
478+
}
479+
480+
481+
@end
482+
483+
@implementation CCPhysicsRotaryLimitJoint
484+
{
485+
ChipmunkRotaryLimitJoint * _constraint;
486+
float min;
487+
float max;
488+
489+
}
337490

491+
-(id)initWithBodyA:(CCPhysicsBody *)bodyA bodyB:(CCPhysicsBody *)bodyB min:(cpFloat)_min max:(cpFloat)_max
492+
{
493+
if((self = [super init])){
494+
_constraint = [ChipmunkRotaryLimitJoint rotaryLimitJointWithBodyA:bodyA.body bodyB:bodyB.body min:_min max:_max];
495+
min = _min;
496+
max = _max;
497+
}
498+
499+
return self;
500+
}
501+
502+
-(ChipmunkConstraint *)constraint {return _constraint;}
503+
504+
-(void)willAddToPhysicsNode:(CCPhysicsNode *)physics
505+
{
506+
float currentAngle = (_constraint.bodyB.angle - _constraint.bodyA.angle);
507+
508+
_constraint.max = currentAngle - min;
509+
_constraint.min = currentAngle - max;
510+
}
511+
512+
513+
514+
@end
338515

339516

340517

0 commit comments

Comments
 (0)