0

I'm currently trying out a few things. I already did this tutorial: https://www.makegameswith.us/tutorials/getting-started-with-spritebuilder/ However I'm a little stuck now. I created a CCNode in SpriteBuilder and added 3 images. carbody.png and 2 wheel.png. I made all objects physicsObjects.

In code I tried to connect them with joints and move them. However the carBody moves but the wheels keep staying on their place.

#import "Car.h"

@implementation Skater{
CCNode *_wheel1;
CCNode *_wheel2;
CCNode *_carBody;
CCPhysicsJoint *_bodyWheelJoint1;
CCPhysicsJoint *_bodyWheelJoint2;


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

if (self) {
    [_wheel1.physicsBody setCollisionGroup: _carBody];
    [_wheel2.physicsBody setCollisionGroup: _carBody;
    [_carBody.physicsBody setCollisionGroup: _carBody];

    _bodyWheelJoint1 = [CCPhysicsJoint connectedPivotJointWithBodyA:_wheel1.physicsBody bodyB: _carBody.physicsBody anchorA:_wheel1.anchorPointInPoints];
    _bodyWheelJoint2 = [CCPhysicsJoint connectedPivotJointWithBodyA:_wheel2.physicsBody bodyB: _carBody.physicsBody anchorA:_wheel2.anchorPointInPoints];


}
return self;
}

And in Gameplay.m I only did this:

-(void)didLoadFromCCB{
[_car runAction:
 [CCActionMoveTo actionWithDuration:10 position:CGPointMake(2000,_car.position.y)]];

}

The carBody moves, the wheels don't...

What am I missing?

Edit: I made a few changes now but my wheels still keep staying on their place...

#import "Car.h"

@implementation Car{
CCNode *_wheel1;
CCNode *_wheel2;
CCNode *_carBody;
CCPhysicsJoint *_bodyWheelJoint1;
CCPhysicsJoint *_bodyWheelJoint2;
}
- (id)init {

    self = [super init];
if (self) {
    CCLOG(@"Car created");
    [_wheel1.physicsBody setCollisionGroup:_carBody];
    [_wheel2.physicsBody setCollisionGroup:_carBody];
    [_carBody.physicsBody setCollisionGroup:_carBody];

    _bodyWheelJoint1 = [CCPhysicsJoint connectedPivotJointWithBodyA:_wheel1.physicsBody bodyB:_carBody.physicsBody anchorA:_wheel1.anchorPointInPoints];
    _bodyWheelJoint2 = [CCPhysicsJoint connectedPivotJointWithBodyA:_wheel2.physicsBody bodyB:_carBody.physicsBody anchorA:_wheel2.anchorPointInPoints];

}
return self;
}
-(void)moveCar:(int)distance{
CCLOG(@"Car should move");
CGPoint launchDirection = ccp(1, 0);
CGPoint force = ccpMult(launchDirection, distance);
[self.physicsBody applyForce:force];
}
@end
James Webster
  • 31,873
  • 11
  • 70
  • 114
Akaino
  • 1,025
  • 6
  • 23

1 Answers1

1

You're simply missing the point that move (and other) actions bypass physics. Once a node has a physics body, the use of CCAction* classes that change position and rotation is a no-no, and most others (ie scale) will not be applied to the physics body either but can still be used safely.

To move a physics object, apply an impulse or force to the physics body.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • That's a good point, thank you! It however doesn't solve my problem. The wheels keep standing on their place :\ – Akaino Mar 18 '14 at 10:50