box2d在使用鼠标关节拖动时忽略碰撞
box2d is ignoring collision when dragging with mouse joint
我不知道为什么,但当我拖动box2D时,它会忽略任何碰撞。我添加了项目符号,使用Stepping参数播放,但它仍然完全忽略它。
代码
class QueryCallback : public b2QueryCallback
{
public:
QueryCallback(const b2Vec2& point)
{
m_point = point;
m_fixture = NULL;
}
bool ReportFixture(b2Fixture* fixture)
{
b2Body* body = fixture->GetBody();
if (body->GetType() == b2_dynamicBody)
{
bool inside = fixture->TestPoint(m_point);
if (inside)
{
m_fixture = fixture;
// We are done, terminate the query.
return false;
}
}
// Continue the query.
return true;
}
b2Vec2 m_point;
b2Fixture* m_fixture;
};
Box2dModel::Box2dModel() {
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
_world = new b2World(gravity);
_world->SetAllowSleeping(true);
_mouseJoint = NULL;
_debugdraw = new GLESDebugDraw(PTM_RATIO);
_world->SetDebugDraw(_debugdraw);
// test
b2BodyDef bodyDef;
bodyDef.bullet = true;
bodyDef.fixedRotation = true;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(100/PTM_RATIO, 200/PTM_RATIO);
b2PolygonShape polygonShape;
polygonShape.SetAsBox(50/PTM_RATIO, 50/PTM_RATIO);
b2FixtureDef fixtureDef;
fixtureDef.shape = &polygonShape;
fixtureDef.density = 10.0f;
_body = _world->CreateBody(&bodyDef);
_body->CreateFixture(&fixtureDef);
//_body->SetGravityScale(0);
//ground
b2BodyDef bodyDef01;
bodyDef01.position.Set(160/PTM_RATIO, 50/PTM_RATIO);
b2PolygonShape polygonShape01;
polygonShape01.SetAsBox(100/PTM_RATIO, 50/PTM_RATIO);
b2FixtureDef fixtureDef01;
fixtureDef01.shape = &polygonShape01;
_ground = _world->CreateBody(&bodyDef01);
_ground->CreateFixture(&fixtureDef01);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
_debugdraw->SetFlags(flags);
}
Box2dModel::~Box2dModel() {
CC_SAFE_DELETE(_world);
}
bool Box2dModel::MouseDown(const b2Vec2& p) {
_worldPosition = p;
if (_mouseJoint != NULL)
{
return false;
}
b2AABB aabb;
b2Vec2 d;
d.Set(1.0f/PTM_RATIO, 1.0f/PTM_RATIO);
aabb.lowerBound = p - d;
aabb.upperBound = p + d;
QueryCallback callback(p);
_world->QueryAABB(&callback, aabb);
if (callback.m_fixture) {
b2Body* body = callback.m_fixture->GetBody();
b2MouseJointDef md;
md.bodyA = _ground;
md.bodyB = body;
md.target = p;
md.maxForce = 1000.0f * body->GetMass();
_mouseJoint = (b2MouseJoint*)_world->CreateJoint(&md);
body->SetAwake(true);
return true;
}
return false;
}
void Box2dModel::MouseUp(const b2Vec2& p) {
if (_mouseJoint)
{
_world->DestroyJoint(_mouseJoint);
_mouseJoint = NULL;
}
}
void Box2dModel::MouseMove(const b2Vec2& p) {
_worldPosition = p;
if (_mouseJoint)
{
_mouseJoint->SetTarget(p);
}
}
void Box2dModel::update(Settings* settings, float delta) {
if (settings->pause)
{
if (settings->singleStep)
{
settings->singleStep = 0;
}
else
{
delta = 0.0f;
}
}
float32 subStep = 3;
for (int i = 0; i < subStep; i++) {
_world->Step(delta/subStep, settings->velocityIterations, settings->positionIterations);
}
//_world->ClearForces();
}
测试视频
https://dl.dropboxusercontent.com/u/604317/test.mov
如果希望拖动的物体与属于地面主体的固定装置碰撞,则需要将鼠标关节的collapseConnected设置为true。
相关文章:
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 如何在鼠标挂钩过程中检测拖动
- 如何通过拖动鼠标QT检查按钮?鼠标移动事件
- 如何防止特定的QTreeView分支被拖动?
- 通过拖动边框调整大小时不显示SDL_WINDOWEVENT_SIZE_CHANGED?
- 在 qt 中的绘图中拖动误差线
- 非拖动功能模板参数包的合法使用
- QML在被Mousearea拖动时无法更改项目位置
- 在OpenGL中,如何将鼠标跟随更改为鼠标单击,拖动并移动释放?
- 如何在 Win32 应用中拖动纯色矩形,而不会产生白色闪烁,也不会干扰其他屏幕对象
- 图形项目的QgraphicsScene中的可拖动像素
- Qt - 防止在拖动标题栏时调整窗口大小
- 在内部使用ComboBox拖动ListWidget项目
- 使用C 绘制矩形拖动
- 在 QGraphicsScene 中拖动 QPixmap:如何避免 lambda 参数中不允许'auto'
- 如何在QT中拖动未装饰的窗口
- 有没有办法在Qt中以编程方式中断鼠标拖动
- Qt - 拖动移动 q图形项不能很好地工作
- 使用C 中的WXWIDGET使用可拖动矩形从图像中选择区域
- box2d在使用鼠标关节拖动时忽略碰撞