Hi,
I'm having some issues with this spring, which does not seem to be created at all. The idea is to have two objects attached to each end of it, one object being static and the other a dynamic one that behaves according to the spring properties. However, at this moment, there is nothing happening, two bodies appear but there is no movement at all.
Does anyone have any ideas as to what might be the problem?
Thanks in advance.
Header
Implementation
I'm having some issues with this spring, which does not seem to be created at all. The idea is to have two objects attached to each end of it, one object being static and the other a dynamic one that behaves according to the spring properties. However, at this moment, there is nothing happening, two bodies appear but there is no movement at all.
Does anyone have any ideas as to what might be the problem?
Thanks in advance.
Header
Code: Select all
#import "CCLayer.h"
#import "Box2D.h"
#import "cocos2d.h"
@class CCScene;
@interface TestSpringLayer : CCLayer
{
b2World *_world;
b2Body *_ballBody;
b2Body *_anchorBody;
CCSprite *_ball;
CCSprite *_anchor;
b2PrismaticJoint *_springJoint;
}
// returns a CCScene that contains the sHelloWorldLayer as the only child
+(CCScene *) scene;
@endImplementation
Code: Select all
#import "TestSpringLayer.h"
#define PTM_RATIO 32.0
@implementation TestSpringLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
TestSpringLayer *layer = [TestSpringLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id)init
{
self = [super init];
if (self) {
[self schedule:@selector(update:)];
}
return self;
}
-(void)onEnter
{
CGSize winSize = [CCDirector sharedDirector].winSize;
// Create sprite and add it to the layer
_ball = [CCSprite spriteWithFile:@"rectangle.png" rect:CGRectMake(0, 0, 52, 52)];
_ball.position = ccp(500, 400);
[self addChild:_ball];
// Create sprite and add it to the layer
_anchor= [CCSprite spriteWithFile:@"rectangle.png" rect:CGRectMake(0, 0, 52, 52)];
_anchor.position = ccp(500, 500);
[self addChild:_anchor];
// Create a world
b2Vec2 gravity = b2Vec2(0.0f, -10.0f);
_world = new b2World(gravity);
_world->SetAllowSleeping(NO);
_world->SetContinuousPhysics(true);
// Create edges around the entire screen
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body *groundBody = _world->CreateBody(&groundBodyDef);
b2EdgeShape groundEdge;
b2FixtureDef boxShapeDef;
boxShapeDef.shape = &groundEdge;
groundEdge.Set(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
groundEdge.Set(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundEdge.Set(b2Vec2(0, winSize.height/PTM_RATIO),
b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundEdge.Set(b2Vec2(winSize.width/PTM_RATIO,
winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
// Create rect body and shape
b2BodyDef rectBodyDef;
rectBodyDef.type = b2_dynamicBody;
rectBodyDef.position.Set(_ball.position.x/PTM_RATIO, _ball.position.y/PTM_RATIO);
rectBodyDef.userData = (__bridge_retained void*) _ball;
_ballBody = _world->CreateBody(&rectBodyDef);
b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;
b2FixtureDef rectShapeDef;
rectShapeDef.shape = &circle;
rectShapeDef.density = 1.0f;
rectShapeDef.friction = 0.2f;
rectShapeDef.restitution = 0.8f;
_ballBody->CreateFixture(&rectShapeDef);
// Create anchor body and shape
b2BodyDef anchorBodyDef;
anchorBodyDef.type = b2_staticBody;
anchorBodyDef.position.Set(_anchor.position.x/PTM_RATIO, _anchor.position.y/PTM_RATIO);
anchorBodyDef.userData = (__bridge_retained void*) _anchor;
_anchorBody = _world->CreateBody(&anchorBodyDef);
b2FixtureDef anchorShapeDef;
anchorShapeDef.shape = &circle;
anchorShapeDef.density = 1.0f;
anchorShapeDef.friction = 0.2f;
anchorShapeDef.restitution = 0.8f;
_anchorBody->CreateFixture(&anchorShapeDef);
b2PrismaticJointDef jointDef;
b2Vec2 worldAxis(0.0f, 1.0f);
jointDef.Initialize(_anchorBody, _ballBody, _anchorBody->GetWorldCenter(), worldAxis);
jointDef.lowerTranslation = -50.0f;
jointDef.upperTranslation = 10.0f;
jointDef.enableLimit = true;
jointDef.maxMotorForce = 1.0f;
jointDef.motorSpeed = 1.0f;
jointDef.enableMotor = true;
// try to save a pointer to the joint
// but i think this does not work
// _springJoint = &jointDef;
_springJoint = (b2PrismaticJoint*)_world->CreateJoint(&jointDef);
}
- (void)update:(ccTime) dt {
_world->Step(dt, 10, 10);
// this should work.
// but i think '_springJoint' is a null pointer at this time
if (_anchor.position.y > _ball.position.y) {
_springJoint->SetMotorSpeed ((_anchor.position.y - _ball.position.y)/100);
}else {
_springJoint->SetMotorSpeed ( -(_anchor.position.y - _ball.position.y)/100);
}
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *ballData = (__bridge CCSprite *)b->GetUserData();
ballData.position = ccp(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
ballData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
@end