在物理世界中添加一个物理精灵,让其自由落下,但是出现了问题(下面有运行结果的图片),实在不知道哪里错了。
#include"HelloBox2D.h"
#define PTM_RATIO 32
#define ktagparent 9
Scene* HelloBox2D::createScene()
{
Scene* scene = Scene::create();
Layer* layer = HelloBox2D::create();
scene->addChild(layer);
return scene;
}
HelloBox2D::HelloBox2D()
:m_texture(nullptr),
world(nullptr)
{
}
HelloBox2D::~HelloBox2D()
{
CC_SAFE_DELETE(world);
}
bool HelloBox2D::init()
{
initPhysicsWorld();
Size s = Director::getInstance()->getVisibleSize();
addSpriteAtPosition(Point(s.width / 2, s.height / 2));
this->scheduleUpdate();
return true;
}
void HelloBox2D::initPhysicsWorld()
{
Size s = Director::getInstance()->getVisibleSize();
//定义重力矢量
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
//创建世界
world = new b2World(gravity);
//允许物体是否休眠,只有在碰撞时才会唤醒该对象,这将提高物理世界中的处理效率
world->SetAllowSleeping(true);
//开启连续物理测试,这是因为计算机只能把一段连续的时间分成许多离散的时间点,再对每个时间点之间的行为进行演算,
//如果时间点的分割不够细致,速度较快的两个物体碰撞时就可能会产生“穿透”现象,开启连续物理将启用特殊的算法来避免该现象
world->SetContinuousPhysics(true);
//地面物体定义
b2BodyDef groundBodyDef;
//位置左下角
groundBodyDef.position.Set(0.0, 0.0);
//创建地面物体
b2Body * groundBody = world->CreateBody(&groundBodyDef);
//定义一个有边的形状
b2EdgeShape groundBox;
//buttom,设置有边形状开始位置和结束位置
groundBox.Set(b2Vec2(0 / PTM_RATIO, 0 / PTM_RATIO), b2Vec2(s.width / PTM_RATIO, 0 / PTM_RATIO));
//使用夹具固定形状到物体上
groundBody->CreateFixture(&groundBox, 0);
//top
groundBox.Set(b2Vec2(0 / PTM_RATIO, s.height / PTM_RATIO), b2Vec2(s.width / PTM_RATIO, s.height / PTM_RATIO));
groundBody->CreateFixture(&groundBox, 0);
//left
groundBox.Set(b2Vec2(0 / PTM_RATIO, 0 / PTM_RATIO), b2Vec2(0 / PTM_RATIO, s.height / PTM_RATIO));
groundBody->CreateFixture(&groundBox, 0);
//right
groundBox.Set(b2Vec2(s.width / PTM_RATIO, 0 / PTM_RATIO), b2Vec2(s.width / PTM_RATIO, s.height / PTM_RATIO));
groundBody->CreateFixture(&groundBox, 0);
}
void HelloBox2D::addSpriteAtPosition(Point pos)
{
Sprite* sprite = Sprite::create("orange.png");
sprite->setPosition(pos);
this->addChild(sprite);
Size s = Director::getInstance()->getVisibleSize();
s = s / 2;
CCLOG("Add sprite %0.2f x %02.f", pos.x, pos.y);
//物体定义
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
//bodyDef.position.Set(pos.x / PTM_RATIO, pos.y / PTM_RATIO);
bodyDef.position.Set(pos.x / PTM_RATIO, pos.y / PTM_RATIO);
//创建物体
b2Body* body = world->CreateBody(&bodyDef);
body->SetUserData(sprite);
//定义1米长1米宽的方形
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(0.5f, 0.5f);
//夹具定义
b2FixtureDef fixtureDef;
//设置密度
fixtureDef.density = 1.0f;
//摩擦系数
fixtureDef.friction = 0.3f;
//设置夹具形状
fixtureDef.shape = &dynamicBox;
//创建夹具--此时物体便有了形状
body->CreateFixture(&fixtureDef);
}
void HelloBox2D::update(float dt)
{
int velocityIterations = 8;
int positionIterations = 1;
world->Step(0.03f, velocityIterations, positionIterations);
for (b2Body* b = world->GetBodyList(); b; b->GetNext())
{
if (b->GetUserData()!=nullptr)
{
Sprite* sprite = (Sprite*)b->GetUserData();
sprite->setPosition(Point(b->GetPosition().x*PTM_RATIO, b->GetPosition().y*PTM_RATIO));
sprite->setRotation(-1*(b->GetAngle() * 180 / M_PI));
}
}
}
```
豆子不是很多请多多包涵
精灵就在那一动不动。
窗口也拖不动。左下角的数字吓死人。
不知道什么原因