#include "Ball.h"
const int tagSpriteA = 1;
const int tagSpriteB = 2;
#define PTM_PATIO 32
Ball::Ball()
{
isTouch = false;
m_pMouseJoint = NULL;
arraySprite = CCArray::create();
arraySprite->retain();
setTouchEnabled(true);
setAccelerometerEnabled(true);
size = CCDirector::sharedDirector()->getWinSize();
b2Vec2 gravity(0.0f, -10.0f);
world = new b2World(gravity);
bool doSleep = true;
world->SetAllowSleeping(doSleep);
world->SetContinuousPhysics(true);
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}
Ball::~Ball()
{
}
bool Ball::init()
{
if(!CCLayer::init())
{
return false;
}
b2BodyDef groundBodydef;
groundBody = world->CreateBody(&groundBodydef);
b2PolygonShape groundBox;
//墙底
groundBox.SetAsBox(size.width / PTM_PATIO, 0, b2Vec2(0,0) ,0);
groundBody->CreateFixture(&groundBox, 0);
groundBox.SetAsBox(size.width / PTM_PATIO, 0, b2Vec2(0, size.height / PTM_PATIO), 0);
groundBody->CreateFixture(&groundBox, 0);
groundBox.SetAsBox(0, size.height / PTM_PATIO, b2Vec2(0,0), 0);
groundBody->CreateFixture(&groundBox, 0);
groundBox.SetAsBox(0, size.height / PTM_PATIO, b2Vec2(size.width / PTM_PATIO, 0), 0);
groundBody->CreateFixture(&groundBox, 0);
CCSpriteBatchNode* batchA = CCSpriteBatchNode::create("a.png");//::initWithFile("a.png");
addChild(batchA, 2, tagSpriteA);
CCSpriteBatchNode* batchB = CCSpriteBatchNode::create("b.png");
addChild(batchB, 2, tagSpriteB);
addSprite(1);
schedule(schedule_selector(Ball::update));
//schedule(schedule_selector(Ball::refreshUpdate), 2);
return true;
}
bool Ball::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
CCPoint point = touch->getLocationInView();
point = CCDirector::sharedDirector()->convertToGL(point);
for(b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if(b->GetUserData() != NULL)
{
CCSprite* sprite = (CCSprite*)b->GetUserData();
if(abs(b->GetPosition().x*PTM_PATIO - point.x) <= 100 && abs(b->GetPosition().y*PTM_PATIO - point.y) <= 100)
{
isTouch = true;
curBody = b;
b2MouseJointDef mouseJointDef;
mouseJointDef.bodyA = groundBody;//随便一个世界中有的body就行,这个目前没有发现什么大影响
mouseJointDef.bodyB = b;
mouseJointDef.target = b2Vec2(point.x / PTM_PATIO, point.y / PTM_PATIO);
mouseJointDef.collideConnected = true;
mouseJointDef.maxForce = 1000.0f * b->GetMass();
m_pMouseJoint = ( b2MouseJoint* )world->CreateJoint( &mouseJointDef );
b->SetAwake(true);
break;
}
}
}
return true;
}
void Ball::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
if(isTouch)
{
CCPoint point = touch->getLocationInView();
point = CCDirector::sharedDirector()->convertToGL(point);
float32 x = point.x / PTM_PATIO;
float32 y = point.y / PTM_PATIO;
b2Vec2 locationWorld = b2Vec2(x, y);
m_pMouseJoint->SetTarget(locationWorld);
//curBody->SetTransform(b2Vec2(x, y), 0);
}
}
void Ball::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
isTouch = false;
if(m_pMouseJoint != NULL)
{
world->DestroyJoint(m_pMouseJoint);
m_pMouseJoint=NULL;
}
}
void Ball::update(float dt)
{
if(!isTouch)
{
int verlocityIterations = 8;
int positionIterations = 1;
world->Step(dt, verlocityIterations, positionIterations);
for(b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if(b->GetUserData() != NULL)
{
CCSprite* sprite = (CCSprite*)b->GetUserData();
sprite->setPosition(ccp(b->GetPosition().x* PTM_PATIO, b->GetPosition().y * PTM_PATIO));
}
}
}
}
void Ball::refreshUpdate(float dt)
{
//addSprite(rand() % 2 + 1);
}
void Ball::addSprite(int type)
{
CCSpriteBatchNode* batch;
if(type == 1)
{
batch = (CCSpriteBatchNode*)this->getChildByTag(tagSpriteA);
}else if(type == 2)
{
batch = (CCSpriteBatchNode*)this->getChildByTag(tagSpriteB);
}
CCSprite* sprite = CCSprite::createWithTexture(batch->getTexture(), CCRectMake(0, 0, 100, 100));
batch->addChild(sprite);
sprite->setPosition(ccp(size.width / 2, size.height - 100));
arraySprite->addObject(sprite);
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(sprite->getPositionX() / PTM_PATIO, sprite->getPositionY() / PTM_PATIO);
bodyDef.userData = sprite;
b2Body* body = world->CreateBody(&bodyDef);
b2PolygonShape shape;
shape.SetAsBox(0.5f, 0.5f);
b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
fixtureDef.density = 1.0;
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.5f;
body->CreateFixture(&fixtureDef);
}
CCScene* Ball::scene()
{
CCLayer* layer = Ball::create();
CCScene* scene = CCScene::create();
scene->addChild(layer);
return scene;
}
```
我创建了一个刚体, 想让这个刚体能够通过鼠标拖拽 并且能根据box2d的有重力加速度,然后上网搜了box2d里面用mouse joint鼠标节点来控制刚体的移动
然后写了以上的代码 虽然代码都正常走了 但是没有效果 刚体不动
我去 我解决了。。。
我刚开始本来以为是cctouchmove里面对sprite进行setposition就可以 然后在update的时候 加了!isTouch的时候 不想走world.step 这么做是有问题 后来改成鼠标节点的方法 然后!isTouch的判断忘了删掉 刚才发完贴又看了遍代码才发现这个问题 去掉就好了。。。
楼主能分享你修改后的代码么 我也遇到相似的问题