issue #4150: Add physics position/rotation test

This commit is contained in:
boyu0 2014-02-28 15:47:49 +08:00
parent a934d29c57
commit 015180bf62
3 changed files with 70 additions and 12 deletions

View File

@ -831,15 +831,10 @@ void PhysicsBody::setPositionOffset(const Point& position)
if (!_positionOffset.equals(position)) if (!_positionOffset.equals(position))
{ {
Point pos = getPosition(); Point pos = getPosition();
_positionOffset = position; _positionOffset = position;
if (_node!= nullptr)
{
setPosition(pos); setPosition(pos);
} }
} }
}
Point PhysicsBody::getPositionOffset() Point PhysicsBody::getPositionOffset()
{ {
@ -851,15 +846,10 @@ void PhysicsBody::setRotationOffset(float rotation)
if (std::abs(_rotationOffset - rotation) > 0.5f) if (std::abs(_rotationOffset - rotation) > 0.5f)
{ {
float rot = getRotation(); float rot = getRotation();
_rotationOffset = rotation; _rotationOffset = rotation;
if (_node != nullptr)
{
setRotation(rot); setRotation(rot);
} }
} }
}
float PhysicsBody::getRotationOffset() float PhysicsBody::getRotationOffset()
{ {

View File

@ -18,6 +18,7 @@ namespace
CL(PhysicsDemoSlice), CL(PhysicsDemoSlice),
CL(PhysicsDemoBug3988), CL(PhysicsDemoBug3988),
CL(PhysicsContactTest), CL(PhysicsContactTest),
CL(PhysicsPositionRotationTest),
#else #else
CL(PhysicsDemoDisabled), CL(PhysicsDemoDisabled),
#endif #endif
@ -1545,4 +1546,62 @@ std::string PhysicsContactTest::subtitle() const
return "should not crash"; return "should not crash";
} }
void PhysicsPositionRotationTest::onEnter()
{
PhysicsDemo::onEnter();
_scene->toggleDebug();
_scene->getPhysicsWorld()->setGravity(Point::ZERO);
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, this);
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemo::onTouchMoved, this);
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemo::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
auto wall = Node::create();
wall->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size));
wall->setPosition(VisibleRect::center());
addChild(wall);
// anchor test
auto anchorNode = Sprite::create("Images/YellowSquare.png");
anchorNode->setAnchorPoint(Point(0.1, 0.9));
anchorNode->setPosition(100, 100);
anchorNode->setScale(0.25);
anchorNode->setPhysicsBody(PhysicsBody::createBox(anchorNode->getContentSize()*anchorNode->getScale()));
anchorNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
addChild(anchorNode);
//parent test
auto parent = Sprite::create("Images/YellowSquare.png");
parent->setPosition(200, 100);
parent->setScale(0.25);
parent->setPhysicsBody(PhysicsBody::createBox(parent->getContentSize()*anchorNode->getScale()));
parent->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
addChild(parent);
auto leftBall = Sprite::create("Images/ball.png");
leftBall->setPosition(-30, 0);
leftBall->cocos2d::Node::setScale(2);
leftBall->setPhysicsBody(PhysicsBody::createCircle(leftBall->getContentSize().width/4));
leftBall->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
parent->addChild(leftBall);
// offset position rotation test
auto offsetPosNode = Sprite::create("Images/YellowSquare.png");
offsetPosNode->setPosition(100, 200);
offsetPosNode->setPhysicsBody(PhysicsBody::createBox(offsetPosNode->getContentSize()/2));
offsetPosNode->getPhysicsBody()->setPositionOffset(-Point(offsetPosNode->getContentSize()/2));
offsetPosNode->getPhysicsBody()->setRotationOffset(45);
offsetPosNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
addChild(offsetPosNode);
return;
}
std::string PhysicsPositionRotationTest::title() const
{
return "Position/Rotation Test";
}
#endif // ifndef CC_USE_PHYSICS #endif // ifndef CC_USE_PHYSICS

View File

@ -216,6 +216,15 @@ private:
int _blueTriangleNum; int _blueTriangleNum;
}; };
class PhysicsPositionRotationTest : public PhysicsDemo
{
public:
CREATE_FUNC(PhysicsPositionRotationTest);
void onEnter() override;
virtual std::string title() const override;
};
#endif #endif
#endif #endif