mirror of https://github.com/axmolengine/axmol.git
Merge pull request #6288 from boyu0/bug4771_move_physicsworld_2_layer
closed #4771: Move PhysicsWorld to Layer
This commit is contained in:
commit
cd6fe294b8
|
@ -48,6 +48,10 @@ THE SOFTWARE.
|
||||||
#include "renderer/CCRenderer.h"
|
#include "renderer/CCRenderer.h"
|
||||||
#include "deprecated/CCString.h"
|
#include "deprecated/CCString.h"
|
||||||
|
|
||||||
|
#if CC_USE_PHYSICS
|
||||||
|
#include "physics/CCPhysicsBody.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
// Layer
|
// Layer
|
||||||
|
@ -60,6 +64,9 @@ Layer::Layer()
|
||||||
, _accelerationListener(nullptr)
|
, _accelerationListener(nullptr)
|
||||||
, _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
|
, _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
|
||||||
, _swallowsTouches(true)
|
, _swallowsTouches(true)
|
||||||
|
#if CC_USE_PHYSICS
|
||||||
|
, _physicsWorld(nullptr)
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
_ignoreAnchorPointForPosition = true;
|
_ignoreAnchorPointForPosition = true;
|
||||||
setAnchorPoint(Point(0.5f, 0.5f));
|
setAnchorPoint(Point(0.5f, 0.5f));
|
||||||
|
@ -67,7 +74,9 @@ Layer::Layer()
|
||||||
|
|
||||||
Layer::~Layer()
|
Layer::~Layer()
|
||||||
{
|
{
|
||||||
|
#if CC_USE_PHYSICS
|
||||||
|
CC_SAFE_DELETE(_physicsWorld);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Layer::init()
|
bool Layer::init()
|
||||||
|
@ -431,6 +440,93 @@ std::string Layer::getDescription() const
|
||||||
return StringUtils::format("<Layer | Tag = %d>", _tag);
|
return StringUtils::format("<Layer | Tag = %d>", _tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if CC_USE_PHYSICS
|
||||||
|
void Layer::onEnter()
|
||||||
|
{
|
||||||
|
Node::onEnter();
|
||||||
|
|
||||||
|
if (_physicsWorld != nullptr)
|
||||||
|
{
|
||||||
|
this->schedule(schedule_selector(Layer::updatePhysics));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Layer::onExit()
|
||||||
|
{
|
||||||
|
Node::onExit();
|
||||||
|
|
||||||
|
if (_physicsWorld != nullptr)
|
||||||
|
{
|
||||||
|
this->unschedule(schedule_selector(Layer::updatePhysics));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Layer::updatePhysics(float delta)
|
||||||
|
{
|
||||||
|
if (nullptr != _physicsWorld)
|
||||||
|
{
|
||||||
|
_physicsWorld->update(delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Layer* Layer::createWithPhysics()
|
||||||
|
{
|
||||||
|
Layer *ret = new Layer();
|
||||||
|
if (ret && ret->initWithPhysics())
|
||||||
|
{
|
||||||
|
ret->autorelease();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CC_SAFE_DELETE(ret);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Layer::initWithPhysics()
|
||||||
|
{
|
||||||
|
bool ret = false;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
Director * director;
|
||||||
|
CC_BREAK_IF( ! (director = Director::getInstance()) );
|
||||||
|
this->setContentSize(director->getWinSize());
|
||||||
|
CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::construct(*this)));
|
||||||
|
|
||||||
|
// success
|
||||||
|
ret = true;
|
||||||
|
} while (0);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Layer::addChildToPhysicsWorld(Node* child)
|
||||||
|
{
|
||||||
|
if (_physicsWorld)
|
||||||
|
{
|
||||||
|
std::function<void(Node*)> addToPhysicsWorldFunc = nullptr;
|
||||||
|
addToPhysicsWorldFunc = [this, &addToPhysicsWorldFunc](Node* node) -> void
|
||||||
|
{
|
||||||
|
if (node->getPhysicsBody())
|
||||||
|
{
|
||||||
|
_physicsWorld->addBody(node->getPhysicsBody());
|
||||||
|
|
||||||
|
node->updatePhysicsBodyPosition(this);
|
||||||
|
node->updatePhysicsBodyRotation(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& children = node->getChildren();
|
||||||
|
for( const auto &n : children) {
|
||||||
|
addToPhysicsWorldFunc(n);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
addToPhysicsWorldFunc(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
__LayerRGBA::__LayerRGBA()
|
__LayerRGBA::__LayerRGBA()
|
||||||
{
|
{
|
||||||
CCLOG("LayerRGBA deprecated.");
|
CCLOG("LayerRGBA deprecated.");
|
||||||
|
|
|
@ -38,6 +38,8 @@ THE SOFTWARE.
|
||||||
#include "CCEventKeyboard.h"
|
#include "CCEventKeyboard.h"
|
||||||
#include "renderer/CCCustomCommand.h"
|
#include "renderer/CCCustomCommand.h"
|
||||||
|
|
||||||
|
#include "CCPhysicsWorld.h"
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -169,6 +171,27 @@ CC_CONSTRUCTOR_ACCESS:
|
||||||
|
|
||||||
virtual bool init() override;
|
virtual bool init() override;
|
||||||
|
|
||||||
|
#if CC_USE_PHYSICS
|
||||||
|
public:
|
||||||
|
virtual void updatePhysics(float delta);
|
||||||
|
virtual void onEnter() override;
|
||||||
|
virtual void onExit() override;
|
||||||
|
|
||||||
|
inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; }
|
||||||
|
static Layer *createWithPhysics();
|
||||||
|
|
||||||
|
CC_CONSTRUCTOR_ACCESS:
|
||||||
|
bool initWithPhysics();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void addChildToPhysicsWorld(Node* child);
|
||||||
|
|
||||||
|
PhysicsWorld* _physicsWorld;
|
||||||
|
|
||||||
|
friend class Node;
|
||||||
|
friend class ProtectedNode;
|
||||||
|
#endif // CC_USE_PHYSICS
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//add the api for avoid use deprecated api
|
//add the api for avoid use deprecated api
|
||||||
void _addTouchListener();
|
void _addTouchListener();
|
||||||
|
|
|
@ -43,7 +43,7 @@ THE SOFTWARE.
|
||||||
#include "CCEventDispatcher.h"
|
#include "CCEventDispatcher.h"
|
||||||
#include "CCEvent.h"
|
#include "CCEvent.h"
|
||||||
#include "CCEventTouch.h"
|
#include "CCEventTouch.h"
|
||||||
#include "CCScene.h"
|
#include "CCLayer.h"
|
||||||
|
|
||||||
#if CC_USE_PHYSICS
|
#if CC_USE_PHYSICS
|
||||||
#include "CCPhysicsBody.h"
|
#include "CCPhysicsBody.h"
|
||||||
|
@ -269,9 +269,10 @@ void Node::setRotation(float rotation)
|
||||||
_transformUpdated = _transformDirty = _inverseDirty = true;
|
_transformUpdated = _transformDirty = _inverseDirty = true;
|
||||||
|
|
||||||
#if CC_USE_PHYSICS
|
#if CC_USE_PHYSICS
|
||||||
if (_physicsBody)
|
if (_physicsBody && !_physicsBody->_rotationResetTag)
|
||||||
{
|
{
|
||||||
_physicsBody->setRotation(rotation);
|
Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr;
|
||||||
|
updatePhysicsBodyRotation(layer);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -299,7 +300,8 @@ void Node::setRotation3D(const Vertex3F& rotation)
|
||||||
#if CC_USE_PHYSICS
|
#if CC_USE_PHYSICS
|
||||||
if (_physicsBody)
|
if (_physicsBody)
|
||||||
{
|
{
|
||||||
_physicsBody->setRotation(_rotationZ_X);
|
Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr;
|
||||||
|
updatePhysicsBodyRotation(layer);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -428,11 +430,10 @@ void Node::setPosition(const Point& position)
|
||||||
_transformUpdated = _transformDirty = _inverseDirty = true;
|
_transformUpdated = _transformDirty = _inverseDirty = true;
|
||||||
|
|
||||||
#if CC_USE_PHYSICS
|
#if CC_USE_PHYSICS
|
||||||
if (_physicsBody)
|
if (_physicsBody != nullptr && !_physicsBody->_positionResetTag)
|
||||||
{
|
{
|
||||||
Node* parent = getParent();
|
Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr;
|
||||||
Point pos = parent != nullptr ? parent->convertToWorldSpace(getPosition()) : getPosition();
|
updatePhysicsBodyPosition(layer);
|
||||||
_physicsBody->setPosition(pos);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -728,27 +729,24 @@ void Node::addChild(Node *child, int zOrder, int tag)
|
||||||
|
|
||||||
this->insertChild(child, zOrder);
|
this->insertChild(child, zOrder);
|
||||||
|
|
||||||
#if CC_USE_PHYSICS
|
|
||||||
if (child->getPhysicsBody() != nullptr)
|
|
||||||
{
|
|
||||||
child->getPhysicsBody()->setPosition(this->convertToWorldSpace(child->getPosition()));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Node* node = this->getParent(); node != nullptr; node = node->getParent())
|
|
||||||
{
|
|
||||||
if (dynamic_cast<Scene*>(node) != nullptr)
|
|
||||||
{
|
|
||||||
(dynamic_cast<Scene*>(node))->addChildToPhysicsWorld(child);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
child->_tag = tag;
|
child->_tag = tag;
|
||||||
|
|
||||||
child->setParent(this);
|
child->setParent(this);
|
||||||
child->setOrderOfArrival(s_globalOrderOfArrival++);
|
child->setOrderOfArrival(s_globalOrderOfArrival++);
|
||||||
|
|
||||||
|
#if CC_USE_PHYSICS
|
||||||
|
// Recursive add children with which have physics body.
|
||||||
|
for (Node* node = this; node != nullptr; node = node->getParent())
|
||||||
|
{
|
||||||
|
Layer* layer = dynamic_cast<Layer*>(node);
|
||||||
|
if (layer != nullptr && layer->getPhysicsWorld() != nullptr)
|
||||||
|
{
|
||||||
|
layer->addChildToPhysicsWorld(child);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if( _running )
|
if( _running )
|
||||||
{
|
{
|
||||||
child->onEnter();
|
child->onEnter();
|
||||||
|
@ -1587,6 +1585,43 @@ void Node::removeAllComponents()
|
||||||
}
|
}
|
||||||
|
|
||||||
#if CC_USE_PHYSICS
|
#if CC_USE_PHYSICS
|
||||||
|
|
||||||
|
void Node::updatePhysicsBodyPosition(Layer* layer)
|
||||||
|
{
|
||||||
|
if (_physicsBody != nullptr)
|
||||||
|
{
|
||||||
|
if (layer != nullptr && layer->getPhysicsWorld() != nullptr)
|
||||||
|
{
|
||||||
|
Point pos = getParent() == layer ? getPosition() : layer->convertToNodeSpace(_parent->convertToWorldSpace(getPosition()));
|
||||||
|
_physicsBody->setPosition(pos);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_physicsBody->setPosition(getPosition());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Node::updatePhysicsBodyRotation(Layer* layer)
|
||||||
|
{
|
||||||
|
if (_physicsBody != nullptr)
|
||||||
|
{
|
||||||
|
if (layer != nullptr && layer->getPhysicsWorld() != nullptr)
|
||||||
|
{
|
||||||
|
float rotation = _rotationZ_X;
|
||||||
|
for (Node* parent = _parent; parent != layer; parent = parent->getParent())
|
||||||
|
{
|
||||||
|
rotation += parent->getRotation();
|
||||||
|
}
|
||||||
|
_physicsBody->setRotation(rotation);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_physicsBody->setRotation(_rotationZ_X);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Node::setPhysicsBody(PhysicsBody* body)
|
void Node::setPhysicsBody(PhysicsBody* body)
|
||||||
{
|
{
|
||||||
if (body != nullptr)
|
if (body != nullptr)
|
||||||
|
@ -1617,12 +1652,23 @@ void Node::setPhysicsBody(PhysicsBody* body)
|
||||||
}
|
}
|
||||||
|
|
||||||
_physicsBody = body;
|
_physicsBody = body;
|
||||||
|
|
||||||
if (body != nullptr)
|
if (body != nullptr)
|
||||||
{
|
{
|
||||||
Node* parent = getParent();
|
Node* node;
|
||||||
Point pos = parent != nullptr ? parent->convertToWorldSpace(getPosition()) : getPosition();
|
Layer* layer = nullptr;
|
||||||
_physicsBody->setPosition(pos);
|
for (node = this->getParent(); node != nullptr; node = node->getParent())
|
||||||
_physicsBody->setRotation(getRotation());
|
{
|
||||||
|
Layer* tmpLayer = dynamic_cast<Layer*>(node);
|
||||||
|
if (tmpLayer != nullptr && tmpLayer->getPhysicsWorld() != nullptr)
|
||||||
|
{
|
||||||
|
layer = tmpLayer;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
updatePhysicsBodyPosition(layer);
|
||||||
|
updatePhysicsBodyRotation(layer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1365,6 +1365,11 @@ protected:
|
||||||
virtual void disableCascadeColor();
|
virtual void disableCascadeColor();
|
||||||
virtual void updateColor() {}
|
virtual void updateColor() {}
|
||||||
|
|
||||||
|
#if CC_USE_PHYSICS
|
||||||
|
virtual void updatePhysicsBodyPosition(Layer* layer);
|
||||||
|
virtual void updatePhysicsBodyRotation(Layer* layer);
|
||||||
|
#endif // CC_USE_PHYSICS
|
||||||
|
|
||||||
float _rotationX; ///< rotation on the X-axis
|
float _rotationX; ///< rotation on the X-axis
|
||||||
float _rotationY; ///< rotation on the Y-axis
|
float _rotationY; ///< rotation on the Y-axis
|
||||||
|
|
||||||
|
@ -1455,6 +1460,10 @@ protected:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CC_DISALLOW_COPY_AND_ASSIGN(Node);
|
CC_DISALLOW_COPY_AND_ASSIGN(Node);
|
||||||
|
|
||||||
|
#if CC_USE_PHYSICS
|
||||||
|
friend class Layer;
|
||||||
|
#endif //CC_USTPS
|
||||||
};
|
};
|
||||||
|
|
||||||
// NodeRGBA
|
// NodeRGBA
|
||||||
|
|
|
@ -36,9 +36,6 @@ THE SOFTWARE.
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
Scene::Scene()
|
Scene::Scene()
|
||||||
#if CC_USE_PHYSICS
|
|
||||||
: _physicsWorld(nullptr)
|
|
||||||
#endif
|
|
||||||
{
|
{
|
||||||
_ignoreAnchorPointForPosition = true;
|
_ignoreAnchorPointForPosition = true;
|
||||||
setAnchorPoint(Point(0.5f, 0.5f));
|
setAnchorPoint(Point(0.5f, 0.5f));
|
||||||
|
@ -46,9 +43,6 @@ Scene::Scene()
|
||||||
|
|
||||||
Scene::~Scene()
|
Scene::~Scene()
|
||||||
{
|
{
|
||||||
#if CC_USE_PHYSICS
|
|
||||||
CC_SAFE_DELETE(_physicsWorld);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Scene::init()
|
bool Scene::init()
|
||||||
|
@ -90,75 +84,4 @@ Scene* Scene::getScene()
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if CC_USE_PHYSICS
|
|
||||||
void Scene::addChild(Node* child, int zOrder, int tag)
|
|
||||||
{
|
|
||||||
Node::addChild(child, zOrder, tag);
|
|
||||||
addChildToPhysicsWorld(child);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Scene::update(float delta)
|
|
||||||
{
|
|
||||||
Node::update(delta);
|
|
||||||
if (nullptr != _physicsWorld)
|
|
||||||
{
|
|
||||||
_physicsWorld->update(delta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Scene *Scene::createWithPhysics()
|
|
||||||
{
|
|
||||||
Scene *ret = new Scene();
|
|
||||||
if (ret && ret->initWithPhysics())
|
|
||||||
{
|
|
||||||
ret->autorelease();
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CC_SAFE_DELETE(ret);
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Scene::initWithPhysics()
|
|
||||||
{
|
|
||||||
bool ret = false;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
Director * director;
|
|
||||||
CC_BREAK_IF( ! (director = Director::getInstance()) );
|
|
||||||
this->setContentSize(director->getWinSize());
|
|
||||||
CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::construct(*this)));
|
|
||||||
|
|
||||||
this->scheduleUpdate();
|
|
||||||
// success
|
|
||||||
ret = true;
|
|
||||||
} while (0);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Scene::addChildToPhysicsWorld(Node* child)
|
|
||||||
{
|
|
||||||
if (_physicsWorld)
|
|
||||||
{
|
|
||||||
std::function<void(Node*)> addToPhysicsWorldFunc = nullptr;
|
|
||||||
addToPhysicsWorldFunc = [this, &addToPhysicsWorldFunc](Node* node) -> void
|
|
||||||
{
|
|
||||||
if (node->getPhysicsBody())
|
|
||||||
{
|
|
||||||
_physicsWorld->addBody(node->getPhysicsBody());
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& children = node->getChildren();
|
|
||||||
for( const auto &n : children) {
|
|
||||||
addToPhysicsWorldFunc(n);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
addToPhysicsWorldFunc(child);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
|
@ -67,28 +67,10 @@ CC_CONSTRUCTOR_ACCESS:
|
||||||
virtual bool init() override;
|
virtual bool init() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
friend class Node;
|
|
||||||
friend class ProtectedNode;
|
|
||||||
friend class SpriteBatchNode;
|
friend class SpriteBatchNode;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CC_DISALLOW_COPY_AND_ASSIGN(Scene);
|
CC_DISALLOW_COPY_AND_ASSIGN(Scene);
|
||||||
|
|
||||||
#if CC_USE_PHYSICS
|
|
||||||
public:
|
|
||||||
virtual void addChild(Node* child, int zOrder, int tag) override;
|
|
||||||
virtual void update(float delta) override;
|
|
||||||
inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; }
|
|
||||||
static Scene *createWithPhysics();
|
|
||||||
|
|
||||||
CC_CONSTRUCTOR_ACCESS:
|
|
||||||
bool initWithPhysics();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void addChildToPhysicsWorld(Node* child);
|
|
||||||
|
|
||||||
PhysicsWorld* _physicsWorld;
|
|
||||||
#endif // CC_USE_PHYSICS
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// end of scene group
|
// end of scene group
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
|
|
||||||
#include "chipmunk.h"
|
#include "chipmunk.h"
|
||||||
|
|
||||||
#include "CCNode.h"
|
#include "CCLayer.h"
|
||||||
|
|
||||||
#include "CCPhysicsShape.h"
|
#include "CCPhysicsShape.h"
|
||||||
#include "CCPhysicsJoint.h"
|
#include "CCPhysicsJoint.h"
|
||||||
|
@ -347,18 +347,12 @@ void PhysicsBody::setGravityEnable(bool enable)
|
||||||
|
|
||||||
void PhysicsBody::setPosition(Point position)
|
void PhysicsBody::setPosition(Point position)
|
||||||
{
|
{
|
||||||
if (!_positionResetTag)
|
|
||||||
{
|
|
||||||
cpBodySetPos(_info->getBody(), PhysicsHelper::point2cpv(position + _positionOffset));
|
cpBodySetPos(_info->getBody(), PhysicsHelper::point2cpv(position + _positionOffset));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsBody::setRotation(float rotation)
|
void PhysicsBody::setRotation(float rotation)
|
||||||
{
|
{
|
||||||
if (!_rotationResetTag)
|
|
||||||
{
|
|
||||||
cpBodySetAngle(_info->getBody(), -PhysicsHelper::float2cpfloat((rotation + _rotationOffset) * (M_PI / 180.0f)));
|
cpBodySetAngle(_info->getBody(), -PhysicsHelper::float2cpfloat((rotation + _rotationOffset) * (M_PI / 180.0f)));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Point PhysicsBody::getPosition() const
|
Point PhysicsBody::getPosition() const
|
||||||
|
@ -770,12 +764,19 @@ void PhysicsBody::update(float delta)
|
||||||
if (_node != nullptr)
|
if (_node != nullptr)
|
||||||
{
|
{
|
||||||
Node* parent = _node->getParent();
|
Node* parent = _node->getParent();
|
||||||
|
Layer* layer = &_world->getLayer();
|
||||||
|
|
||||||
|
Point position = parent != layer ? parent->convertToNodeSpace(layer->convertToWorldSpace(getPosition())) : getPosition();
|
||||||
|
float rotation = getRotation();
|
||||||
|
for (; parent != layer; parent = parent->getParent())
|
||||||
|
{
|
||||||
|
rotation -= parent->getRotation();
|
||||||
|
}
|
||||||
|
|
||||||
Point position = parent != nullptr ? parent->convertToNodeSpace(getPosition()) : getPosition();
|
|
||||||
_positionResetTag = true;
|
_positionResetTag = true;
|
||||||
_rotationResetTag = true;
|
_rotationResetTag = true;
|
||||||
_node->setPosition(position);
|
_node->setPosition(position);
|
||||||
_node->setRotation(getRotation());
|
_node->setRotation(rotation);
|
||||||
_positionResetTag = false;
|
_positionResetTag = false;
|
||||||
_rotationResetTag = false;
|
_rotationResetTag = false;
|
||||||
|
|
||||||
|
|
|
@ -349,6 +349,7 @@ protected:
|
||||||
friend class PhysicsShape;
|
friend class PhysicsShape;
|
||||||
friend class PhysicsJoint;
|
friend class PhysicsJoint;
|
||||||
friend class Node;
|
friend class Node;
|
||||||
|
friend class Layer;
|
||||||
friend class ProtectedNode;
|
friend class ProtectedNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@
|
||||||
#include "chipmunk/CCPhysicsHelper_chipmunk.h"
|
#include "chipmunk/CCPhysicsHelper_chipmunk.h"
|
||||||
|
|
||||||
#include "CCDrawNode.h"
|
#include "CCDrawNode.h"
|
||||||
#include "CCScene.h"
|
#include "CCLayer.h"
|
||||||
#include "CCDirector.h"
|
#include "CCDirector.h"
|
||||||
#include "CCEventDispatcher.h"
|
#include "CCEventDispatcher.h"
|
||||||
#include "CCEventCustom.h"
|
#include "CCEventCustom.h"
|
||||||
|
@ -289,7 +289,7 @@ int PhysicsWorld::collisionBeginCallback(PhysicsContact& contact)
|
||||||
{
|
{
|
||||||
contact.setEventCode(PhysicsContact::EventCode::BEGIN);
|
contact.setEventCode(PhysicsContact::EventCode::BEGIN);
|
||||||
contact.setWorld(this);
|
contact.setWorld(this);
|
||||||
_scene->getEventDispatcher()->dispatchEvent(&contact);
|
_layer->getEventDispatcher()->dispatchEvent(&contact);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret ? contact.resetResult() : false;
|
return ret ? contact.resetResult() : false;
|
||||||
|
@ -305,7 +305,7 @@ int PhysicsWorld::collisionPreSolveCallback(PhysicsContact& contact)
|
||||||
|
|
||||||
contact.setEventCode(PhysicsContact::EventCode::PRESOLVE);
|
contact.setEventCode(PhysicsContact::EventCode::PRESOLVE);
|
||||||
contact.setWorld(this);
|
contact.setWorld(this);
|
||||||
_scene->getEventDispatcher()->dispatchEvent(&contact);
|
_layer->getEventDispatcher()->dispatchEvent(&contact);
|
||||||
|
|
||||||
return contact.resetResult();
|
return contact.resetResult();
|
||||||
}
|
}
|
||||||
|
@ -319,7 +319,7 @@ void PhysicsWorld::collisionPostSolveCallback(PhysicsContact& contact)
|
||||||
|
|
||||||
contact.setEventCode(PhysicsContact::EventCode::POSTSOLVE);
|
contact.setEventCode(PhysicsContact::EventCode::POSTSOLVE);
|
||||||
contact.setWorld(this);
|
contact.setWorld(this);
|
||||||
_scene->getEventDispatcher()->dispatchEvent(&contact);
|
_layer->getEventDispatcher()->dispatchEvent(&contact);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsWorld::collisionSeparateCallback(PhysicsContact& contact)
|
void PhysicsWorld::collisionSeparateCallback(PhysicsContact& contact)
|
||||||
|
@ -331,7 +331,7 @@ void PhysicsWorld::collisionSeparateCallback(PhysicsContact& contact)
|
||||||
|
|
||||||
contact.setEventCode(PhysicsContact::EventCode::SEPERATE);
|
contact.setEventCode(PhysicsContact::EventCode::SEPERATE);
|
||||||
contact.setWorld(this);
|
contact.setWorld(this);
|
||||||
_scene->getEventDispatcher()->dispatchEvent(&contact);
|
_layer->getEventDispatcher()->dispatchEvent(&contact);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsWorld::rayCast(PhysicsRayCastCallbackFunc func, const Point& point1, const Point& point2, void* data)
|
void PhysicsWorld::rayCast(PhysicsRayCastCallbackFunc func, const Point& point1, const Point& point2, void* data)
|
||||||
|
@ -416,10 +416,10 @@ PhysicsShape* PhysicsWorld::getShape(const Point& point) const
|
||||||
return shape == nullptr ? nullptr : PhysicsShapeInfo::getMap().find(shape)->second->getShape();
|
return shape == nullptr ? nullptr : PhysicsShapeInfo::getMap().find(shape)->second->getShape();
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsWorld* PhysicsWorld::construct(Scene& scene)
|
PhysicsWorld* PhysicsWorld::construct(Layer& layer)
|
||||||
{
|
{
|
||||||
PhysicsWorld * world = new PhysicsWorld();
|
PhysicsWorld * world = new PhysicsWorld();
|
||||||
if(world && world->init(scene))
|
if(world && world->init(layer))
|
||||||
{
|
{
|
||||||
return world;
|
return world;
|
||||||
}
|
}
|
||||||
|
@ -428,14 +428,14 @@ PhysicsWorld* PhysicsWorld::construct(Scene& scene)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PhysicsWorld::init(Scene& scene)
|
bool PhysicsWorld::init(Layer& layer)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
_info = new PhysicsWorldInfo();
|
_info = new PhysicsWorldInfo();
|
||||||
CC_BREAK_IF(_info == nullptr);
|
CC_BREAK_IF(_info == nullptr);
|
||||||
|
|
||||||
_scene = &scene;
|
_layer = &layer;
|
||||||
|
|
||||||
_info->setGravity(_gravity);
|
_info->setGravity(_gravity);
|
||||||
|
|
||||||
|
@ -867,8 +867,7 @@ void PhysicsWorld::setGravity(const Vect& gravity)
|
||||||
// reset gravity for body
|
// reset gravity for body
|
||||||
if (!body->isGravityEnabled())
|
if (!body->isGravityEnabled())
|
||||||
{
|
{
|
||||||
body->applyForce(_gravity * body->getMass());
|
body->applyForce((_gravity - gravity) * body->getMass());
|
||||||
body->applyForce(- gravity * body->getMass());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -912,7 +911,7 @@ PhysicsWorld::PhysicsWorld()
|
||||||
, _updateRateCount(0)
|
, _updateRateCount(0)
|
||||||
, _updateTime(0.0f)
|
, _updateTime(0.0f)
|
||||||
, _info(nullptr)
|
, _info(nullptr)
|
||||||
, _scene(nullptr)
|
, _layer(nullptr)
|
||||||
, _delayDirty(false)
|
, _delayDirty(false)
|
||||||
, _debugDraw(nullptr)
|
, _debugDraw(nullptr)
|
||||||
, _debugDrawMask(DEBUGDRAW_NONE)
|
, _debugDrawMask(DEBUGDRAW_NONE)
|
||||||
|
@ -933,7 +932,7 @@ PhysicsDebugDraw::PhysicsDebugDraw(PhysicsWorld& world)
|
||||||
, _world(world)
|
, _world(world)
|
||||||
{
|
{
|
||||||
_drawNode = DrawNode::create();
|
_drawNode = DrawNode::create();
|
||||||
_world.getScene().addChild(_drawNode);
|
_world.getLayer().addChild(_drawNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsDebugDraw::~PhysicsDebugDraw()
|
PhysicsDebugDraw::~PhysicsDebugDraw()
|
||||||
|
|
|
@ -46,7 +46,7 @@ typedef Point Vect;
|
||||||
|
|
||||||
class Node;
|
class Node;
|
||||||
class Sprite;
|
class Sprite;
|
||||||
class Scene;
|
class Layer;
|
||||||
class DrawNode;
|
class DrawNode;
|
||||||
class PhysicsDebugDraw;
|
class PhysicsDebugDraw;
|
||||||
|
|
||||||
|
@ -119,8 +119,8 @@ public:
|
||||||
/** Get body by tag */
|
/** Get body by tag */
|
||||||
PhysicsBody* getBody(int tag) const;
|
PhysicsBody* getBody(int tag) const;
|
||||||
|
|
||||||
/** Get scene contain this physics world */
|
/** Get layer contain this physics world */
|
||||||
inline Scene& getScene() const { return *_scene; }
|
inline Layer& getLayer() const { return *_layer; }
|
||||||
/** get the gravity value */
|
/** get the gravity value */
|
||||||
inline Vect getGravity() const { return _gravity; }
|
inline Vect getGravity() const { return _gravity; }
|
||||||
/** set the gravity value */
|
/** set the gravity value */
|
||||||
|
@ -144,8 +144,8 @@ public:
|
||||||
inline int getDebugDrawMask() { return _debugDrawMask; }
|
inline int getDebugDrawMask() { return _debugDrawMask; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static PhysicsWorld* construct(Scene& scene);
|
static PhysicsWorld* construct(Layer& layer);
|
||||||
bool init(Scene& scene);
|
bool init(Layer& layer);
|
||||||
|
|
||||||
virtual void addBody(PhysicsBody* body);
|
virtual void addBody(PhysicsBody* body);
|
||||||
virtual void addShape(PhysicsShape* shape);
|
virtual void addShape(PhysicsShape* shape);
|
||||||
|
@ -180,7 +180,7 @@ protected:
|
||||||
|
|
||||||
Vector<PhysicsBody*> _bodies;
|
Vector<PhysicsBody*> _bodies;
|
||||||
std::list<PhysicsJoint*> _joints;
|
std::list<PhysicsJoint*> _joints;
|
||||||
Scene* _scene;
|
Layer* _layer;
|
||||||
|
|
||||||
bool _delayDirty;
|
bool _delayDirty;
|
||||||
PhysicsDebugDraw* _debugDraw;
|
PhysicsDebugDraw* _debugDraw;
|
||||||
|
@ -198,7 +198,7 @@ protected:
|
||||||
|
|
||||||
friend class Node;
|
friend class Node;
|
||||||
friend class Sprite;
|
friend class Sprite;
|
||||||
friend class Scene;
|
friend class Layer;
|
||||||
friend class PhysicsBody;
|
friend class PhysicsBody;
|
||||||
friend class PhysicsShape;
|
friend class PhysicsShape;
|
||||||
friend class PhysicsJoint;
|
friend class PhysicsJoint;
|
||||||
|
|
|
@ -74,7 +74,7 @@ tolua_lerror:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int lua_cocos2dx_physics_PhysicsWorld_getScene(lua_State* tolua_S)
|
int lua_cocos2dx_physics_PhysicsWorld_getLayer(lua_State* tolua_S)
|
||||||
{
|
{
|
||||||
int argc = 0;
|
int argc = 0;
|
||||||
cocos2d::PhysicsWorld* cobj = nullptr;
|
cocos2d::PhysicsWorld* cobj = nullptr;
|
||||||
|
@ -93,7 +93,7 @@ int lua_cocos2dx_physics_PhysicsWorld_getScene(lua_State* tolua_S)
|
||||||
#if COCOS2D_DEBUG >= 1
|
#if COCOS2D_DEBUG >= 1
|
||||||
if (!cobj)
|
if (!cobj)
|
||||||
{
|
{
|
||||||
tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsWorld_getScene'", NULL);
|
tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsWorld_getLayer'", NULL);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -103,7 +103,7 @@ int lua_cocos2dx_physics_PhysicsWorld_getScene(lua_State* tolua_S)
|
||||||
{
|
{
|
||||||
if(!ok)
|
if(!ok)
|
||||||
return 0;
|
return 0;
|
||||||
cocos2d::Scene& ret = cobj->getScene();
|
cocos2d::Layer& ret = cobj->getLayer();
|
||||||
do {
|
do {
|
||||||
|
|
||||||
std::string hashName = typeid(ret).name();
|
std::string hashName = typeid(ret).name();
|
||||||
|
@ -112,7 +112,7 @@ int lua_cocos2dx_physics_PhysicsWorld_getScene(lua_State* tolua_S)
|
||||||
if(iter != g_luaType.end()){
|
if(iter != g_luaType.end()){
|
||||||
className = iter->second.c_str();
|
className = iter->second.c_str();
|
||||||
} else {
|
} else {
|
||||||
className = "cc.Scene";
|
className = "cc.Layer";
|
||||||
}
|
}
|
||||||
|
|
||||||
int ID = (int)(ret._ID);
|
int ID = (int)(ret._ID);
|
||||||
|
@ -122,12 +122,12 @@ int lua_cocos2dx_physics_PhysicsWorld_getScene(lua_State* tolua_S)
|
||||||
}while (0);
|
}while (0);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getScene",argc, 0);
|
CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getLayer",argc, 0);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
#if COCOS2D_DEBUG >= 1
|
#if COCOS2D_DEBUG >= 1
|
||||||
tolua_lerror:
|
tolua_lerror:
|
||||||
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsWorld_getScene'.",&tolua_err);
|
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsWorld_getLayer'.",&tolua_err);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1573,8 +1573,8 @@ int register_all_cocos2dx_physics_manual(lua_State* tolua_S)
|
||||||
lua_rawget(tolua_S, LUA_REGISTRYINDEX);
|
lua_rawget(tolua_S, LUA_REGISTRYINDEX);
|
||||||
if (lua_istable(tolua_S,-1))
|
if (lua_istable(tolua_S,-1))
|
||||||
{
|
{
|
||||||
lua_pushstring(tolua_S,"getScene");
|
lua_pushstring(tolua_S,"getLayer");
|
||||||
lua_pushcfunction(tolua_S, lua_cocos2dx_physics_PhysicsWorld_getScene );
|
lua_pushcfunction(tolua_S, lua_cocos2dx_physics_PhysicsWorld_getLayer );
|
||||||
lua_rawset(tolua_S,-3);
|
lua_rawset(tolua_S,-3);
|
||||||
lua_pushstring(tolua_S,"queryPoint");
|
lua_pushstring(tolua_S,"queryPoint");
|
||||||
lua_pushcfunction(tolua_S, lua_cocos2dx_physics_PhysicsWorld_queryPoint );
|
lua_pushcfunction(tolua_S, lua_cocos2dx_physics_PhysicsWorld_queryPoint );
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
#if CC_USE_PHYSICS
|
#if CC_USE_PHYSICS
|
||||||
#include "CCPhysicsBody.h"
|
#include "CCPhysicsBody.h"
|
||||||
#endif
|
#endif
|
||||||
#include "CCScene.h"
|
#include "CCLayer.h"
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
|
@ -95,27 +95,24 @@ void ProtectedNode::addProtectedChild(Node *child, int zOrder, int tag)
|
||||||
|
|
||||||
this->insertProtectedChild(child, zOrder);
|
this->insertProtectedChild(child, zOrder);
|
||||||
|
|
||||||
#if CC_USE_PHYSICS
|
|
||||||
if (child->getPhysicsBody() != nullptr)
|
|
||||||
{
|
|
||||||
child->getPhysicsBody()->setPosition(this->convertToWorldSpace(child->getPosition()));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Node* node = this->getParent(); node != nullptr; node = node->getParent())
|
|
||||||
{
|
|
||||||
if (dynamic_cast<Scene*>(node) != nullptr)
|
|
||||||
{
|
|
||||||
(dynamic_cast<Scene*>(node))->addChildToPhysicsWorld(child);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
child->setTag(tag);
|
child->setTag(tag);
|
||||||
|
|
||||||
child->setParent(this);
|
child->setParent(this);
|
||||||
child->setOrderOfArrival(s_globalOrderOfArrival++);
|
child->setOrderOfArrival(s_globalOrderOfArrival++);
|
||||||
|
|
||||||
|
#if CC_USE_PHYSICS
|
||||||
|
// Recursive add children with which have physics body.
|
||||||
|
for (Node* node = this; node != nullptr; node = node->getParent())
|
||||||
|
{
|
||||||
|
Layer* layer = dynamic_cast<Layer*>(node);
|
||||||
|
if (layer != nullptr && layer->getPhysicsWorld() != nullptr)
|
||||||
|
{
|
||||||
|
layer->addChildToPhysicsWorld(child);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if( _running )
|
if( _running )
|
||||||
{
|
{
|
||||||
child->onEnter();
|
child->onEnter();
|
||||||
|
|
|
@ -3,23 +3,27 @@
|
||||||
#include "../testResource.h"
|
#include "../testResource.h"
|
||||||
USING_NS_CC;
|
USING_NS_CC;
|
||||||
|
|
||||||
|
#if CC_USE_PHYSICS
|
||||||
|
#define CLPHYSICS(__className__) [](){ return __className__::createWithPhysics();}
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
static std::function<Layer*()> createFunctions[] = {
|
static std::function<Layer*()> createFunctions[] = {
|
||||||
#if CC_USE_PHYSICS
|
#if CC_USE_PHYSICS
|
||||||
CL(PhysicsDemoLogoSmash),
|
CLPHYSICS(PhysicsDemoLogoSmash),
|
||||||
CL(PhysicsDemoPyramidStack),
|
CLPHYSICS(PhysicsDemoPyramidStack),
|
||||||
CL(PhysicsDemoClickAdd),
|
CLPHYSICS(PhysicsDemoClickAdd),
|
||||||
CL(PhysicsDemoRayCast),
|
CLPHYSICS(PhysicsDemoRayCast),
|
||||||
CL(PhysicsDemoJoints),
|
CLPHYSICS(PhysicsDemoJoints),
|
||||||
CL(PhysicsDemoActions),
|
CLPHYSICS(PhysicsDemoActions),
|
||||||
CL(PhysicsDemoPump),
|
CLPHYSICS(PhysicsDemoPump),
|
||||||
CL(PhysicsDemoOneWayPlatform),
|
CLPHYSICS(PhysicsDemoOneWayPlatform),
|
||||||
CL(PhysicsDemoSlice),
|
CLPHYSICS(PhysicsDemoSlice),
|
||||||
CL(PhysicsDemoBug3988),
|
CLPHYSICS(PhysicsDemoBug3988),
|
||||||
CL(PhysicsContactTest),
|
CLPHYSICS(PhysicsContactTest),
|
||||||
CL(PhysicsPositionRotationTest),
|
CL(PhysicsPositionRotationTest),
|
||||||
CL(PhysicsSetGravityEnableTest),
|
CLPHYSICS(PhysicsSetGravityEnableTest),
|
||||||
#else
|
#else
|
||||||
CL(PhysicsDemoDisabled),
|
CL(PhysicsDemoDisabled),
|
||||||
#endif
|
#endif
|
||||||
|
@ -59,12 +63,7 @@ namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsTestScene::PhysicsTestScene()
|
PhysicsTestScene::PhysicsTestScene()
|
||||||
#if CC_USE_PHYSICS
|
|
||||||
: TestScene(false, true)
|
|
||||||
#else
|
|
||||||
: TestScene()
|
: TestScene()
|
||||||
#endif
|
|
||||||
, _debugDraw(false)
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void PhysicsTestScene::runThisTest()
|
void PhysicsTestScene::runThisTest()
|
||||||
|
@ -75,14 +74,6 @@ void PhysicsTestScene::runThisTest()
|
||||||
Director::getInstance()->replaceScene(this);
|
Director::getInstance()->replaceScene(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsTestScene::toggleDebug()
|
|
||||||
{
|
|
||||||
#if CC_USE_PHYSICS
|
|
||||||
_debugDraw = !_debugDraw;
|
|
||||||
getPhysicsWorld()->setDebugDrawMask(_debugDraw ? PhysicsWorld::DEBUGDRAW_ALL : PhysicsWorld::DEBUGDRAW_NONE);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#if CC_USE_PHYSICS == 0
|
#if CC_USE_PHYSICS == 0
|
||||||
void PhysicsDemoDisabled::onEnter()
|
void PhysicsDemoDisabled::onEnter()
|
||||||
{
|
{
|
||||||
|
@ -97,9 +88,10 @@ void PhysicsDemoDisabled::onEnter()
|
||||||
#else
|
#else
|
||||||
|
|
||||||
PhysicsDemo::PhysicsDemo()
|
PhysicsDemo::PhysicsDemo()
|
||||||
: _scene(nullptr)
|
: _spriteTexture(nullptr)
|
||||||
, _spriteTexture(nullptr)
|
|
||||||
, _ball(nullptr)
|
, _ball(nullptr)
|
||||||
|
, _debugDraw(false)
|
||||||
|
, _isRoot(true)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,6 +100,12 @@ PhysicsDemo::~PhysicsDemo()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PhysicsDemo::toggleDebug()
|
||||||
|
{
|
||||||
|
_debugDraw = !_debugDraw;
|
||||||
|
getPhysicsWorld()->setDebugDrawMask(_debugDraw ? PhysicsWorld::DEBUGDRAW_ALL : PhysicsWorld::DEBUGDRAW_NONE);
|
||||||
|
}
|
||||||
|
|
||||||
std::string PhysicsDemo::title() const
|
std::string PhysicsDemo::title() const
|
||||||
{
|
{
|
||||||
return "PhysicsTest";
|
return "PhysicsTest";
|
||||||
|
@ -144,19 +142,26 @@ void PhysicsDemo::backCallback(Ref* sender)
|
||||||
|
|
||||||
void PhysicsDemo::onEnter()
|
void PhysicsDemo::onEnter()
|
||||||
{
|
{
|
||||||
|
if (_isRoot)
|
||||||
|
{
|
||||||
BaseTest::onEnter();
|
BaseTest::onEnter();
|
||||||
|
}
|
||||||
_scene = dynamic_cast<PhysicsTestScene*>(this->getParent());
|
else
|
||||||
|
{
|
||||||
|
Layer::onEnter();
|
||||||
|
}
|
||||||
|
|
||||||
_spriteTexture = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 100)->getTexture();
|
_spriteTexture = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 100)->getTexture();
|
||||||
|
|
||||||
|
if (this->_physicsWorld != nullptr)
|
||||||
|
{
|
||||||
// menu for debug layer
|
// menu for debug layer
|
||||||
MenuItemFont::setFontSize(18);
|
MenuItemFont::setFontSize(18);
|
||||||
auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(PhysicsDemo::toggleDebugCallback, this));
|
auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(PhysicsDemo::toggleDebugCallback, this));
|
||||||
|
auto menu = Menu::create(item, nullptr);
|
||||||
auto menu = Menu::create(item, NULL);
|
|
||||||
this->addChild(menu);
|
this->addChild(menu);
|
||||||
menu->setPosition(Point(VisibleRect::right().x-50, VisibleRect::top().y-10));
|
menu->setPosition(Point(getContentSize().width-50, getContentSize().height/2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Sprite* PhysicsDemo::addGrossiniAtPosition(Point p, float scale/* = 1.0*/)
|
Sprite* PhysicsDemo::addGrossiniAtPosition(Point p, float scale/* = 1.0*/)
|
||||||
|
@ -183,10 +188,7 @@ Sprite* PhysicsDemo::addGrossiniAtPosition(Point p, float scale/* = 1.0*/)
|
||||||
|
|
||||||
void PhysicsDemo::toggleDebugCallback(Ref* sender)
|
void PhysicsDemo::toggleDebugCallback(Ref* sender)
|
||||||
{
|
{
|
||||||
if (_scene != nullptr)
|
toggleDebug();
|
||||||
{
|
|
||||||
_scene->toggleDebug();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsDemoClickAdd::~PhysicsDemoClickAdd()
|
PhysicsDemoClickAdd::~PhysicsDemoClickAdd()
|
||||||
|
@ -246,10 +248,7 @@ void PhysicsDemoClickAdd::onAcceleration(Acceleration* acc, Event* event)
|
||||||
auto v = Point( accelX, accelY);
|
auto v = Point( accelX, accelY);
|
||||||
v = v * 200;
|
v = v * 200;
|
||||||
|
|
||||||
if(_scene != nullptr)
|
getPhysicsWorld()->setGravity(v);
|
||||||
{
|
|
||||||
_scene->getPhysicsWorld()->setGravity(v);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
|
@ -381,8 +380,8 @@ Sprite* PhysicsDemo::makeTriangle(Point point, Size size, int color, PhysicsMate
|
||||||
|
|
||||||
bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event)
|
bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event)
|
||||||
{
|
{
|
||||||
auto location = touch->getLocation();
|
auto location = this->convertTouchToNodeSpace(touch);
|
||||||
auto arr = _scene->getPhysicsWorld()->getShapes(location);
|
auto arr = getPhysicsWorld()->getShapes(location);
|
||||||
|
|
||||||
PhysicsBody* body = nullptr;
|
PhysicsBody* body = nullptr;
|
||||||
for (auto& obj : arr)
|
for (auto& obj : arr)
|
||||||
|
@ -403,7 +402,7 @@ bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event)
|
||||||
this->addChild(mouse);
|
this->addChild(mouse);
|
||||||
PhysicsJointPin* joint = PhysicsJointPin::construct(mouse->getPhysicsBody(), body, location);
|
PhysicsJointPin* joint = PhysicsJointPin::construct(mouse->getPhysicsBody(), body, location);
|
||||||
joint->setMaxForce(5000.0f * body->getMass());
|
joint->setMaxForce(5000.0f * body->getMass());
|
||||||
_scene->getPhysicsWorld()->addJoint(joint);
|
getPhysicsWorld()->addJoint(joint);
|
||||||
_mouses.insert(std::make_pair(touch->getID(), mouse));
|
_mouses.insert(std::make_pair(touch->getID(), mouse));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -418,7 +417,7 @@ void PhysicsDemo::onTouchMoved(Touch* touch, Event* event)
|
||||||
|
|
||||||
if (it != _mouses.end())
|
if (it != _mouses.end())
|
||||||
{
|
{
|
||||||
it->second->setPosition(touch->getLocation());
|
it->second->setPosition(this->convertTouchToNodeSpace(touch));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -438,8 +437,8 @@ void PhysicsDemoLogoSmash::onEnter()
|
||||||
{
|
{
|
||||||
PhysicsDemo::onEnter();
|
PhysicsDemo::onEnter();
|
||||||
|
|
||||||
_scene->getPhysicsWorld()->setGravity(Point(0, 0));
|
getPhysicsWorld()->setGravity(Point(0, 0));
|
||||||
_scene->getPhysicsWorld()->setUpdateRate(5.0f);
|
getPhysicsWorld()->setUpdateRate(5.0f);
|
||||||
|
|
||||||
_ball = SpriteBatchNode::create("Images/ball.png", sizeof(logo_image)/sizeof(logo_image[0]));
|
_ball = SpriteBatchNode::create("Images/ball.png", sizeof(logo_image)/sizeof(logo_image[0]));
|
||||||
addChild(_ball);
|
addChild(_ball);
|
||||||
|
@ -465,7 +464,6 @@ void PhysicsDemoLogoSmash::onEnter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
auto bullet = makeBall(Point(400, 0), 10, PhysicsMaterial(PHYSICS_INFINITY, 0, 0));
|
auto bullet = makeBall(Point(400, 0), 10, PhysicsMaterial(PHYSICS_INFINITY, 0, 0));
|
||||||
bullet->getPhysicsBody()->setVelocity(Point(200, 0));
|
bullet->getPhysicsBody()->setVelocity(Point(200, 0));
|
||||||
|
|
||||||
|
@ -539,8 +537,9 @@ void PhysicsDemoRayCast::onEnter()
|
||||||
auto listener = EventListenerTouchAllAtOnce::create();
|
auto listener = EventListenerTouchAllAtOnce::create();
|
||||||
listener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoRayCast::onTouchesEnded, this);
|
listener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoRayCast::onTouchesEnded, this);
|
||||||
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
||||||
|
scheduleUpdate();
|
||||||
|
|
||||||
_scene->getPhysicsWorld()->setGravity(Point::ZERO);
|
getPhysicsWorld()->setGravity(Point::ZERO);
|
||||||
|
|
||||||
auto node = DrawNode::create();
|
auto node = DrawNode::create();
|
||||||
node->setPhysicsBody(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Point(0, 50), VisibleRect::rightBottom() + Point(0, 50)));
|
node->setPhysicsBody(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Point(0, 50), VisibleRect::rightBottom() + Point(0, 50)));
|
||||||
|
@ -553,8 +552,6 @@ void PhysicsDemoRayCast::onEnter()
|
||||||
auto menu = Menu::create(item, NULL);
|
auto menu = Menu::create(item, NULL);
|
||||||
this->addChild(menu);
|
this->addChild(menu);
|
||||||
menu->setPosition(Point(VisibleRect::left().x+100, VisibleRect::top().y-10));
|
menu->setPosition(Point(VisibleRect::left().x+100, VisibleRect::top().y-10));
|
||||||
|
|
||||||
scheduleUpdate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsDemoRayCast::changeModeCallback(Ref* sender)
|
void PhysicsDemoRayCast::changeModeCallback(Ref* sender)
|
||||||
|
@ -600,7 +597,7 @@ void PhysicsDemoRayCast::update(float delta)
|
||||||
Point point3 = point2;
|
Point point3 = point2;
|
||||||
auto func = CC_CALLBACK_3(PhysicsDemoRayCast::anyRay, this);
|
auto func = CC_CALLBACK_3(PhysicsDemoRayCast::anyRay, this);
|
||||||
|
|
||||||
_scene->getPhysicsWorld()->rayCast(func, point1, point2, &point3);
|
getPhysicsWorld()->rayCast(func, point1, point2, &point3);
|
||||||
_node->drawSegment(point1, point3, 1, STATIC_COLOR);
|
_node->drawSegment(point1, point3, 1, STATIC_COLOR);
|
||||||
|
|
||||||
if (point2 != point3)
|
if (point2 != point3)
|
||||||
|
@ -626,7 +623,7 @@ void PhysicsDemoRayCast::update(float delta)
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
_scene->getPhysicsWorld()->rayCast(func, point1, point2, nullptr);
|
getPhysicsWorld()->rayCast(func, point1, point2, nullptr);
|
||||||
_node->drawSegment(point1, point3, 1, STATIC_COLOR);
|
_node->drawSegment(point1, point3, 1, STATIC_COLOR);
|
||||||
|
|
||||||
if (point2 != point3)
|
if (point2 != point3)
|
||||||
|
@ -653,7 +650,7 @@ void PhysicsDemoRayCast::update(float delta)
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
_scene->getPhysicsWorld()->rayCast(func, point1, point2, nullptr);
|
getPhysicsWorld()->rayCast(func, point1, point2, nullptr);
|
||||||
|
|
||||||
_node->drawSegment(point1, point2, 1, STATIC_COLOR);
|
_node->drawSegment(point1, point2, 1, STATIC_COLOR);
|
||||||
|
|
||||||
|
@ -705,7 +702,7 @@ std::string PhysicsDemoRayCast::title() const
|
||||||
void PhysicsDemoJoints::onEnter()
|
void PhysicsDemoJoints::onEnter()
|
||||||
{
|
{
|
||||||
PhysicsDemo::onEnter();
|
PhysicsDemo::onEnter();
|
||||||
_scene->toggleDebug();
|
toggleDebug();
|
||||||
|
|
||||||
auto listener = EventListenerTouchOneByOne::create();
|
auto listener = EventListenerTouchOneByOne::create();
|
||||||
listener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoJoints::onTouchBegan, this);
|
listener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoJoints::onTouchBegan, this);
|
||||||
|
@ -713,8 +710,6 @@ void PhysicsDemoJoints::onEnter()
|
||||||
listener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoJoints::onTouchEnded, this);
|
listener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoJoints::onTouchEnded, this);
|
||||||
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
||||||
|
|
||||||
//_scene->getPhysicsWorld()->setGravity(Point::ZERO);
|
|
||||||
|
|
||||||
float width = (VisibleRect::getVisibleRect().size.width - 10) / 4;
|
float width = (VisibleRect::getVisibleRect().size.width - 10) / 4;
|
||||||
float height = (VisibleRect::getVisibleRect().size.height - 50) / 4;
|
float height = (VisibleRect::getVisibleRect().size.height - 50) / 4;
|
||||||
|
|
||||||
|
@ -742,7 +737,7 @@ void PhysicsDemoJoints::onEnter()
|
||||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
PhysicsJointPin* joint = PhysicsJointPin::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), offset);
|
PhysicsJointPin* joint = PhysicsJointPin::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), offset);
|
||||||
_scene->getPhysicsWorld()->addJoint(joint);
|
getPhysicsWorld()->addJoint(joint);
|
||||||
|
|
||||||
this->addChild(sp1);
|
this->addChild(sp1);
|
||||||
this->addChild(sp2);
|
this->addChild(sp2);
|
||||||
|
@ -757,7 +752,7 @@ void PhysicsDemoJoints::onEnter()
|
||||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
PhysicsJointFixed* joint = PhysicsJointFixed::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), offset);
|
PhysicsJointFixed* joint = PhysicsJointFixed::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), offset);
|
||||||
_scene->getPhysicsWorld()->addJoint(joint);
|
getPhysicsWorld()->addJoint(joint);
|
||||||
|
|
||||||
this->addChild(sp1);
|
this->addChild(sp1);
|
||||||
this->addChild(sp2);
|
this->addChild(sp2);
|
||||||
|
@ -772,7 +767,7 @@ void PhysicsDemoJoints::onEnter()
|
||||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
PhysicsJointDistance* joint = PhysicsJointDistance::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO);
|
PhysicsJointDistance* joint = PhysicsJointDistance::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO);
|
||||||
_scene->getPhysicsWorld()->addJoint(joint);
|
getPhysicsWorld()->addJoint(joint);
|
||||||
|
|
||||||
this->addChild(sp1);
|
this->addChild(sp1);
|
||||||
this->addChild(sp2);
|
this->addChild(sp2);
|
||||||
|
@ -786,7 +781,7 @@ void PhysicsDemoJoints::onEnter()
|
||||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
PhysicsJointLimit* joint = PhysicsJointLimit::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO, 30.0f, 60.0f);
|
PhysicsJointLimit* joint = PhysicsJointLimit::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO, 30.0f, 60.0f);
|
||||||
_scene->getPhysicsWorld()->addJoint(joint);
|
getPhysicsWorld()->addJoint(joint);
|
||||||
|
|
||||||
this->addChild(sp1);
|
this->addChild(sp1);
|
||||||
this->addChild(sp2);
|
this->addChild(sp2);
|
||||||
|
@ -800,7 +795,7 @@ void PhysicsDemoJoints::onEnter()
|
||||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
PhysicsJointSpring* joint = PhysicsJointSpring::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO, 500.0f, 0.3f);
|
PhysicsJointSpring* joint = PhysicsJointSpring::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO, 500.0f, 0.3f);
|
||||||
_scene->getPhysicsWorld()->addJoint(joint);
|
getPhysicsWorld()->addJoint(joint);
|
||||||
|
|
||||||
this->addChild(sp1);
|
this->addChild(sp1);
|
||||||
this->addChild(sp2);
|
this->addChild(sp2);
|
||||||
|
@ -814,7 +809,7 @@ void PhysicsDemoJoints::onEnter()
|
||||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
PhysicsJointGroove* joint = PhysicsJointGroove::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point(30, 15), Point(30, -15), Point(-30, 0));
|
PhysicsJointGroove* joint = PhysicsJointGroove::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point(30, 15), Point(30, -15), Point(-30, 0));
|
||||||
_scene->getPhysicsWorld()->addJoint(joint);
|
getPhysicsWorld()->addJoint(joint);
|
||||||
|
|
||||||
this->addChild(sp1);
|
this->addChild(sp1);
|
||||||
this->addChild(sp2);
|
this->addChild(sp2);
|
||||||
|
@ -827,10 +822,10 @@ void PhysicsDemoJoints::onEnter()
|
||||||
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
|
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
|
||||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||||
PhysicsJointRotarySpring* joint = PhysicsJointRotarySpring::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 3000.0f, 60.0f);
|
PhysicsJointRotarySpring* joint = PhysicsJointRotarySpring::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 3000.0f, 60.0f);
|
||||||
_scene->getPhysicsWorld()->addJoint(joint);
|
getPhysicsWorld()->addJoint(joint);
|
||||||
|
|
||||||
this->addChild(sp1);
|
this->addChild(sp1);
|
||||||
this->addChild(sp2);
|
this->addChild(sp2);
|
||||||
|
@ -843,10 +838,10 @@ void PhysicsDemoJoints::onEnter()
|
||||||
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
|
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
|
||||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||||
PhysicsJointRotaryLimit* joint = PhysicsJointRotaryLimit::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f,(float) M_PI_2);
|
PhysicsJointRotaryLimit* joint = PhysicsJointRotaryLimit::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f,(float) M_PI_2);
|
||||||
_scene->getPhysicsWorld()->addJoint(joint);
|
getPhysicsWorld()->addJoint(joint);
|
||||||
|
|
||||||
this->addChild(sp1);
|
this->addChild(sp1);
|
||||||
this->addChild(sp2);
|
this->addChild(sp2);
|
||||||
|
@ -859,10 +854,10 @@ void PhysicsDemoJoints::onEnter()
|
||||||
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
|
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
|
||||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||||
PhysicsJointRatchet* joint = PhysicsJointRatchet::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f, (float)M_PI_2);
|
PhysicsJointRatchet* joint = PhysicsJointRatchet::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f, (float)M_PI_2);
|
||||||
_scene->getPhysicsWorld()->addJoint(joint);
|
getPhysicsWorld()->addJoint(joint);
|
||||||
|
|
||||||
this->addChild(sp1);
|
this->addChild(sp1);
|
||||||
this->addChild(sp2);
|
this->addChild(sp2);
|
||||||
|
@ -875,10 +870,10 @@ void PhysicsDemoJoints::onEnter()
|
||||||
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
|
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
|
||||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||||
PhysicsJointGear* joint = PhysicsJointGear::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f, 2.0f);
|
PhysicsJointGear* joint = PhysicsJointGear::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f, 2.0f);
|
||||||
_scene->getPhysicsWorld()->addJoint(joint);
|
getPhysicsWorld()->addJoint(joint);
|
||||||
|
|
||||||
this->addChild(sp1);
|
this->addChild(sp1);
|
||||||
this->addChild(sp2);
|
this->addChild(sp2);
|
||||||
|
@ -891,10 +886,10 @@ void PhysicsDemoJoints::onEnter()
|
||||||
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
|
auto sp2 = makeBox(offset + Point(30, 0), Size(30, 10));
|
||||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||||
PhysicsJointMotor* joint = PhysicsJointMotor::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), (float)M_PI_2);
|
PhysicsJointMotor* joint = PhysicsJointMotor::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), (float)M_PI_2);
|
||||||
_scene->getPhysicsWorld()->addJoint(joint);
|
getPhysicsWorld()->addJoint(joint);
|
||||||
|
|
||||||
this->addChild(sp1);
|
this->addChild(sp1);
|
||||||
this->addChild(sp2);
|
this->addChild(sp2);
|
||||||
|
@ -953,7 +948,7 @@ std::string PhysicsDemoActions::title() const
|
||||||
void PhysicsDemoPump::onEnter()
|
void PhysicsDemoPump::onEnter()
|
||||||
{
|
{
|
||||||
PhysicsDemo::onEnter();
|
PhysicsDemo::onEnter();
|
||||||
_scene->toggleDebug();
|
toggleDebug();
|
||||||
|
|
||||||
_distance = 0.0f;
|
_distance = 0.0f;
|
||||||
_rotationV = 0.0f;
|
_rotationV = 0.0f;
|
||||||
|
@ -998,7 +993,7 @@ void PhysicsDemoPump::onEnter()
|
||||||
VisibleRect::leftBottom() + Point(102, 20)
|
VisibleRect::leftBottom() + Point(102, 20)
|
||||||
};
|
};
|
||||||
|
|
||||||
auto _world = _scene->getPhysicsWorld();
|
auto _world = getPhysicsWorld();
|
||||||
|
|
||||||
// small gear
|
// small gear
|
||||||
auto sgear = Node::create();
|
auto sgear = Node::create();
|
||||||
|
@ -1054,7 +1049,7 @@ void PhysicsDemoPump::onEnter()
|
||||||
|
|
||||||
void PhysicsDemoPump::update(float delta)
|
void PhysicsDemoPump::update(float delta)
|
||||||
{
|
{
|
||||||
for (const auto& body : _scene->getPhysicsWorld()->getAllBodies())
|
for (const auto& body : getPhysicsWorld()->getAllBodies())
|
||||||
{
|
{
|
||||||
if (body->getTag() == DRAG_BODYS_TAG && body->getPosition().y < 0.0f)
|
if (body->getTag() == DRAG_BODYS_TAG && body->getPosition().y < 0.0f)
|
||||||
{
|
{
|
||||||
|
@ -1063,7 +1058,7 @@ void PhysicsDemoPump::update(float delta)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsBody* gear = _scene->getPhysicsWorld()->getBody(1);
|
PhysicsBody* gear = getPhysicsWorld()->getBody(1);
|
||||||
|
|
||||||
if (gear != nullptr)
|
if (gear != nullptr)
|
||||||
{
|
{
|
||||||
|
@ -1157,7 +1152,7 @@ std::string PhysicsDemoOneWayPlatform::title() const
|
||||||
void PhysicsDemoSlice::onEnter()
|
void PhysicsDemoSlice::onEnter()
|
||||||
{
|
{
|
||||||
PhysicsDemo::onEnter();
|
PhysicsDemo::onEnter();
|
||||||
_scene->toggleDebug();
|
toggleDebug();
|
||||||
|
|
||||||
_sliceTag = 1;
|
_sliceTag = 1;
|
||||||
|
|
||||||
|
@ -1245,7 +1240,7 @@ void PhysicsDemoSlice::clipPoly(PhysicsShapePolygon* shape, Point normal, float
|
||||||
void PhysicsDemoSlice::onTouchEnded(Touch *touch, Event *event)
|
void PhysicsDemoSlice::onTouchEnded(Touch *touch, Event *event)
|
||||||
{
|
{
|
||||||
auto func = CC_CALLBACK_3(PhysicsDemoSlice::slice, this);
|
auto func = CC_CALLBACK_3(PhysicsDemoSlice::slice, this);
|
||||||
_scene->getPhysicsWorld()->rayCast(func, touch->getStartLocation(), touch->getLocation(), nullptr);
|
getPhysicsWorld()->rayCast(func, touch->getStartLocation(), touch->getLocation(), nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string PhysicsDemoSlice::title() const
|
std::string PhysicsDemoSlice::title() const
|
||||||
|
@ -1262,8 +1257,8 @@ std::string PhysicsDemoSlice::subtitle() const
|
||||||
void PhysicsDemoBug3988::onEnter()
|
void PhysicsDemoBug3988::onEnter()
|
||||||
{
|
{
|
||||||
PhysicsDemo::onEnter();
|
PhysicsDemo::onEnter();
|
||||||
_scene->toggleDebug();
|
toggleDebug();
|
||||||
_scene->getPhysicsWorld()->setGravity(Vect::ZERO);
|
getPhysicsWorld()->setGravity(Vect::ZERO);
|
||||||
|
|
||||||
auto ball = Sprite::create("Images/YellowSquare.png");
|
auto ball = Sprite::create("Images/YellowSquare.png");
|
||||||
ball->setPosition(VisibleRect::center() - Point(100, 0));
|
ball->setPosition(VisibleRect::center() - Point(100, 0));
|
||||||
|
@ -1288,7 +1283,7 @@ std::string PhysicsDemoBug3988::subtitle() const
|
||||||
void PhysicsContactTest::onEnter()
|
void PhysicsContactTest::onEnter()
|
||||||
{
|
{
|
||||||
PhysicsDemo::onEnter();
|
PhysicsDemo::onEnter();
|
||||||
_scene->getPhysicsWorld()->setGravity(Vect::ZERO);
|
getPhysicsWorld()->setGravity(Vect::ZERO);
|
||||||
auto s = VisibleRect::getVisibleRect().size;
|
auto s = VisibleRect::getVisibleRect().size;
|
||||||
|
|
||||||
_yellowBoxNum = 50;
|
_yellowBoxNum = 50;
|
||||||
|
@ -1550,36 +1545,51 @@ std::string PhysicsContactTest::subtitle() const
|
||||||
void PhysicsPositionRotationTest::onEnter()
|
void PhysicsPositionRotationTest::onEnter()
|
||||||
{
|
{
|
||||||
PhysicsDemo::onEnter();
|
PhysicsDemo::onEnter();
|
||||||
_scene->toggleDebug();
|
|
||||||
_scene->getPhysicsWorld()->setGravity(Point::ZERO);
|
PhysicsDemo* layers[2] = {PhysicsDemo::createWithPhysics(), PhysicsDemo::createWithPhysics()};
|
||||||
|
Size halfSize = VisibleRect::getVisibleRect().size;
|
||||||
|
halfSize.width /= 2;
|
||||||
|
halfSize.height -= 40;
|
||||||
|
|
||||||
|
for (int i = 0; i < sizeof(layers) / sizeof(layers[0]); ++i)
|
||||||
|
{
|
||||||
|
layers[i]->ignoreAnchorPointForPosition(false);
|
||||||
|
layers[i]->setContentSize(halfSize);
|
||||||
|
layers[i]->setRoot(false);
|
||||||
|
layers[i]->getPhysicsWorld()->setGravity(Point::ZERO);
|
||||||
|
layers[i]->toggleDebug();
|
||||||
|
|
||||||
auto touchListener = EventListenerTouchOneByOne::create();
|
auto touchListener = EventListenerTouchOneByOne::create();
|
||||||
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, this);
|
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, layers[i]);
|
||||||
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemo::onTouchMoved, this);
|
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemo::onTouchMoved, layers[i]);
|
||||||
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemo::onTouchEnded, this);
|
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemo::onTouchEnded, layers[i]);
|
||||||
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, layers[i]);
|
||||||
|
|
||||||
auto wall = Node::create();
|
auto wall = Node::create();
|
||||||
wall->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size));
|
wall->setPhysicsBody(PhysicsBody::createEdgeBox(layers[i]->getContentSize(), PhysicsMaterial(0.1f, 1, 0.0f)));
|
||||||
wall->setPosition(VisibleRect::center());
|
wall->setPosition(Point(halfSize/2));
|
||||||
addChild(wall);
|
layers[i]->addChild(wall);
|
||||||
|
this->addChild(layers[i]);
|
||||||
|
}
|
||||||
|
layers[0]->setPosition(Point(halfSize.width/2, halfSize.height/2 + 60));
|
||||||
|
layers[1]->setPosition(Point(halfSize.width/2*3, halfSize.height/2 + 60));
|
||||||
|
|
||||||
// anchor test
|
// anchor test
|
||||||
auto anchorNode = Sprite::create("Images/YellowSquare.png");
|
auto anchorNode = Sprite::create("Images/YellowSquare.png");
|
||||||
anchorNode->setAnchorPoint(Point(0.1f, 0.9f));
|
anchorNode->setAnchorPoint(Point(0.1f, 0.9f));
|
||||||
anchorNode->setPosition(100, 100);
|
anchorNode->setPosition(100, 50);
|
||||||
anchorNode->setScale(0.25);
|
anchorNode->setScale(0.25);
|
||||||
anchorNode->setPhysicsBody(PhysicsBody::createBox(anchorNode->getContentSize()*anchorNode->getScale()));
|
anchorNode->setPhysicsBody(PhysicsBody::createBox(anchorNode->getContentSize()*anchorNode->getScale()));
|
||||||
anchorNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
anchorNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
addChild(anchorNode);
|
layers[0]->addChild(anchorNode);
|
||||||
|
|
||||||
//parent test
|
//parent test
|
||||||
auto parent = Sprite::create("Images/YellowSquare.png");
|
auto parent = Sprite::create("Images/YellowSquare.png");
|
||||||
parent->setPosition(200, 100);
|
parent->setPosition(160, 50);
|
||||||
parent->setScale(0.25);
|
parent->setScale(0.25);
|
||||||
parent->setPhysicsBody(PhysicsBody::createBox(parent->getContentSize()*anchorNode->getScale()));
|
parent->setPhysicsBody(PhysicsBody::createBox(parent->getContentSize()*anchorNode->getScale()));
|
||||||
parent->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
parent->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
addChild(parent);
|
layers[0]->addChild(parent);
|
||||||
|
|
||||||
auto leftBall = Sprite::create("Images/ball.png");
|
auto leftBall = Sprite::create("Images/ball.png");
|
||||||
leftBall->setPosition(-30, 0);
|
leftBall->setPosition(-30, 0);
|
||||||
|
@ -1590,12 +1600,37 @@ void PhysicsPositionRotationTest::onEnter()
|
||||||
|
|
||||||
// offset position rotation test
|
// offset position rotation test
|
||||||
auto offsetPosNode = Sprite::create("Images/YellowSquare.png");
|
auto offsetPosNode = Sprite::create("Images/YellowSquare.png");
|
||||||
offsetPosNode->setPosition(100, 200);
|
offsetPosNode->setPosition(100, 100);
|
||||||
offsetPosNode->setPhysicsBody(PhysicsBody::createBox(offsetPosNode->getContentSize()/2));
|
offsetPosNode->setPhysicsBody(PhysicsBody::createBox(offsetPosNode->getContentSize()/2));
|
||||||
offsetPosNode->getPhysicsBody()->setPositionOffset(-Point(offsetPosNode->getContentSize()/2));
|
offsetPosNode->getPhysicsBody()->setPositionOffset(-Point(offsetPosNode->getContentSize()/2));
|
||||||
offsetPosNode->getPhysicsBody()->setRotationOffset(45);
|
offsetPosNode->getPhysicsBody()->setRotationOffset(45);
|
||||||
offsetPosNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
offsetPosNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
addChild(offsetPosNode);
|
layers[0]->addChild(offsetPosNode);
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 0; i < 30; ++i)
|
||||||
|
{
|
||||||
|
Size size(10 + CCRANDOM_0_1()*10, 10 + CCRANDOM_0_1()*10);
|
||||||
|
Point position = Point(halfSize.width, halfSize.height) - Point(size.width, size.height);
|
||||||
|
position.x = position.x * CCRANDOM_0_1();
|
||||||
|
position.y = position.y * CCRANDOM_0_1();
|
||||||
|
position = position + Point(size.width/2, size.height/2);
|
||||||
|
Vect velocity((CCRANDOM_0_1() - 0.5)*200, (CCRANDOM_0_1() - 0.5)*200);
|
||||||
|
auto box = makeBox(position, size, 0, PhysicsMaterial(0.1f, 1, 0.0f));
|
||||||
|
box->getPhysicsBody()->setVelocity(velocity);
|
||||||
|
box->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||||
|
layers[1]->addChild(box);
|
||||||
|
}
|
||||||
|
|
||||||
|
MoveTo* moveTo = MoveTo::create(1.0f, Point(halfSize.width/2, halfSize.height/2 + 60));
|
||||||
|
MoveTo* moveBack = MoveTo::create(1.0f, Point(halfSize.width/2*3, halfSize.height/2 + 60));
|
||||||
|
ScaleTo* thrink = ScaleTo::create(1.0f, 0.5f);
|
||||||
|
ScaleTo* amplify = ScaleTo::create(1.0f, 2.0f);
|
||||||
|
ScaleTo* scaleBack = ScaleTo::create(1.0f, 1.0f);
|
||||||
|
RotateTo* rotateTo = RotateTo::create(1.0f, 180.0f);
|
||||||
|
RotateTo* rotateBack = RotateTo::create(1.0f, 360.0f);
|
||||||
|
|
||||||
|
layers[1]->runAction(Sequence::create(moveTo, thrink, rotateTo, amplify, rotateBack, scaleBack, moveBack, nullptr));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1605,6 +1640,11 @@ std::string PhysicsPositionRotationTest::title() const
|
||||||
return "Position/Rotation Test";
|
return "Position/Rotation Test";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string PhysicsPositionRotationTest::subtitle() const
|
||||||
|
{
|
||||||
|
return "Two Physics Worlds";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void PhysicsSetGravityEnableTest::onEnter()
|
void PhysicsSetGravityEnableTest::onEnter()
|
||||||
{
|
{
|
||||||
|
@ -1647,7 +1687,7 @@ void PhysicsSetGravityEnableTest::onScheduleOnce(float delta)
|
||||||
auto ball = getChildByTag(2);
|
auto ball = getChildByTag(2);
|
||||||
ball->getPhysicsBody()->setMass(200);
|
ball->getPhysicsBody()->setMass(200);
|
||||||
|
|
||||||
_scene->getPhysicsWorld()->setGravity(Vect(0, 98));
|
getPhysicsWorld()->setGravity(Vect(0, 98));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string PhysicsSetGravityEnableTest::title() const
|
std::string PhysicsSetGravityEnableTest::title() const
|
||||||
|
|
|
@ -15,11 +15,6 @@ public:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual void runThisTest();
|
virtual void runThisTest();
|
||||||
|
|
||||||
void toggleDebug();
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool _debugDraw;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#if CC_USE_PHYSICS == 0
|
#if CC_USE_PHYSICS == 0
|
||||||
|
@ -32,11 +27,26 @@ public:
|
||||||
};
|
};
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
#define CREATE_WITH_PHYSICS_FUNC(__TYPE__) \
|
||||||
|
static __TYPE__* createWithPhysics() \
|
||||||
|
{ \
|
||||||
|
__TYPE__ *ret = new __TYPE__(); \
|
||||||
|
if (ret && ret->initWithPhysics()) \
|
||||||
|
{ \
|
||||||
|
ret->autorelease(); \
|
||||||
|
return ret; \
|
||||||
|
} \
|
||||||
|
else \
|
||||||
|
{ \
|
||||||
|
delete ret; \
|
||||||
|
ret = nullptr; \
|
||||||
|
return nullptr; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
class PhysicsDemo : public BaseTest
|
class PhysicsDemo : public BaseTest
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CREATE_FUNC(PhysicsDemo);
|
CREATE_WITH_PHYSICS_FUNC(PhysicsDemo);
|
||||||
|
|
||||||
PhysicsDemo();
|
PhysicsDemo();
|
||||||
virtual ~PhysicsDemo();
|
virtual ~PhysicsDemo();
|
||||||
|
|
||||||
|
@ -58,17 +68,23 @@ public:
|
||||||
void onTouchMoved(Touch* touch, Event* event);
|
void onTouchMoved(Touch* touch, Event* event);
|
||||||
void onTouchEnded(Touch* touch, Event* event);
|
void onTouchEnded(Touch* touch, Event* event);
|
||||||
|
|
||||||
|
void toggleDebug();
|
||||||
|
inline void setRoot(bool isRoot) { _isRoot = isRoot; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
PhysicsTestScene* _scene;
|
|
||||||
Texture2D* _spriteTexture; // weak ref
|
Texture2D* _spriteTexture; // weak ref
|
||||||
SpriteBatchNode* _ball;
|
SpriteBatchNode* _ball;
|
||||||
std::unordered_map<int, Node*> _mouses;
|
std::unordered_map<int, Node*> _mouses;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool _debugDraw;
|
||||||
|
bool _isRoot;
|
||||||
};
|
};
|
||||||
|
|
||||||
class PhysicsDemoClickAdd : public PhysicsDemo
|
class PhysicsDemoClickAdd : public PhysicsDemo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CREATE_FUNC(PhysicsDemoClickAdd);
|
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoClickAdd);
|
||||||
|
|
||||||
virtual ~PhysicsDemoClickAdd();
|
virtual ~PhysicsDemoClickAdd();
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
|
@ -81,7 +97,7 @@ public:
|
||||||
class PhysicsDemoLogoSmash : public PhysicsDemo
|
class PhysicsDemoLogoSmash : public PhysicsDemo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CREATE_FUNC(PhysicsDemoLogoSmash);
|
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoLogoSmash);
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
virtual std::string title() const override;
|
virtual std::string title() const override;
|
||||||
|
@ -90,7 +106,7 @@ public:
|
||||||
class PhysicsDemoPyramidStack : public PhysicsDemo
|
class PhysicsDemoPyramidStack : public PhysicsDemo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CREATE_FUNC(PhysicsDemoPyramidStack);
|
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoPyramidStack);
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void updateOnce(float delta);
|
void updateOnce(float delta);
|
||||||
|
@ -100,7 +116,7 @@ public:
|
||||||
class PhysicsDemoRayCast : public PhysicsDemo
|
class PhysicsDemoRayCast : public PhysicsDemo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CREATE_FUNC(PhysicsDemoRayCast);
|
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoRayCast);
|
||||||
|
|
||||||
PhysicsDemoRayCast();
|
PhysicsDemoRayCast();
|
||||||
|
|
||||||
|
@ -122,7 +138,7 @@ private:
|
||||||
class PhysicsDemoJoints : public PhysicsDemo
|
class PhysicsDemoJoints : public PhysicsDemo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CREATE_FUNC(PhysicsDemoJoints);
|
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoJoints);
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
virtual std::string title() const override;
|
virtual std::string title() const override;
|
||||||
|
@ -131,7 +147,7 @@ public:
|
||||||
class PhysicsDemoActions : public PhysicsDemo
|
class PhysicsDemoActions : public PhysicsDemo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CREATE_FUNC(PhysicsDemoActions);
|
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoActions);
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
virtual std::string title() const override;
|
virtual std::string title() const override;
|
||||||
|
@ -140,7 +156,7 @@ public:
|
||||||
class PhysicsDemoPump : public PhysicsDemo
|
class PhysicsDemoPump : public PhysicsDemo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CREATE_FUNC(PhysicsDemoPump);
|
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoPump);
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void update(float delta) override;
|
void update(float delta) override;
|
||||||
|
@ -159,7 +175,7 @@ private:
|
||||||
class PhysicsDemoOneWayPlatform : public PhysicsDemo
|
class PhysicsDemoOneWayPlatform : public PhysicsDemo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CREATE_FUNC(PhysicsDemoOneWayPlatform);
|
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoOneWayPlatform);
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
virtual std::string title() const override;
|
virtual std::string title() const override;
|
||||||
|
@ -170,7 +186,7 @@ public:
|
||||||
class PhysicsDemoSlice : public PhysicsDemo
|
class PhysicsDemoSlice : public PhysicsDemo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CREATE_FUNC(PhysicsDemoSlice);
|
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoSlice);
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
virtual std::string title() const override;
|
virtual std::string title() const override;
|
||||||
|
@ -188,7 +204,7 @@ private:
|
||||||
class PhysicsDemoBug3988 : public PhysicsDemo
|
class PhysicsDemoBug3988 : public PhysicsDemo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CREATE_FUNC(PhysicsDemoBug3988);
|
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoBug3988);
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
virtual std::string title() const override;
|
virtual std::string title() const override;
|
||||||
|
@ -198,7 +214,7 @@ public:
|
||||||
class PhysicsContactTest : public PhysicsDemo
|
class PhysicsContactTest : public PhysicsDemo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CREATE_FUNC(PhysicsContactTest);
|
CREATE_WITH_PHYSICS_FUNC(PhysicsContactTest);
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void resetTest();
|
void resetTest();
|
||||||
|
@ -223,12 +239,13 @@ public:
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
virtual std::string title() const override;
|
virtual std::string title() const override;
|
||||||
|
virtual std::string subtitle() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class PhysicsSetGravityEnableTest : public PhysicsDemo
|
class PhysicsSetGravityEnableTest : public PhysicsDemo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CREATE_FUNC(PhysicsSetGravityEnableTest);
|
CREATE_WITH_PHYSICS_FUNC(PhysicsSetGravityEnableTest);
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onScheduleOnce(float delta);
|
void onScheduleOnce(float delta);
|
||||||
|
|
|
@ -3,20 +3,9 @@
|
||||||
#include "extensions/cocos-ext.h"
|
#include "extensions/cocos-ext.h"
|
||||||
#include "cocostudio/CocoStudio.h"
|
#include "cocostudio/CocoStudio.h"
|
||||||
|
|
||||||
TestScene::TestScene(bool bPortrait, bool physics/* = false*/)
|
TestScene::TestScene(bool bPortrait)
|
||||||
{
|
{
|
||||||
if (physics)
|
|
||||||
{
|
|
||||||
#if CC_USE_PHYSICS
|
|
||||||
TestScene::initWithPhysics();
|
|
||||||
#else
|
|
||||||
Scene::init();
|
Scene::init();
|
||||||
#endif
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Scene::init();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void testScene_callback(Ref *sender )
|
void testScene_callback(Ref *sender )
|
||||||
|
|
|
@ -9,7 +9,7 @@ USING_NS_CC;
|
||||||
class TestScene : public Scene
|
class TestScene : public Scene
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TestScene(bool bPortrait = false, bool physics = false);
|
TestScene(bool bPortrait = false);
|
||||||
virtual void onEnter() override;
|
virtual void onEnter() override;
|
||||||
|
|
||||||
virtual void runThisTest() = 0;
|
virtual void runThisTest() = 0;
|
||||||
|
|
|
@ -106,7 +106,7 @@ local function initWithLayer(layer, callback)
|
||||||
local debug = false
|
local debug = false
|
||||||
local function toggleDebugCallback(sender)
|
local function toggleDebugCallback(sender)
|
||||||
debug = not debug
|
debug = not debug
|
||||||
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setDebugDrawMask(debug and cc.PhysicsWorld.DEBUGDRAW_ALL or cc.PhysicsWorld.DEBUGDRAW_NONE)
|
layer:getPhysicsWorld():setDebugDrawMask(debug and cc.PhysicsWorld.DEBUGDRAW_ALL or cc.PhysicsWorld.DEBUGDRAW_NONE)
|
||||||
end
|
end
|
||||||
|
|
||||||
layer.toggleDebug = function(self) toggleDebugCallback(nil) end;
|
layer.toggleDebug = function(self) toggleDebugCallback(nil) end;
|
||||||
|
@ -144,7 +144,7 @@ end
|
||||||
|
|
||||||
local function onTouchBegan(touch, event)
|
local function onTouchBegan(touch, event)
|
||||||
local location = touch:getLocation()
|
local location = touch:getLocation()
|
||||||
local arr = cc.Director:getInstance():getRunningScene():getPhysicsWorld():getShapes(location)
|
local arr = curLayer:getPhysicsWorld():getShapes(location)
|
||||||
|
|
||||||
local body
|
local body
|
||||||
for _, obj in ipairs(arr) do
|
for _, obj in ipairs(arr) do
|
||||||
|
@ -162,7 +162,7 @@ local function onTouchBegan(touch, event)
|
||||||
curLayer:addChild(mouse);
|
curLayer:addChild(mouse);
|
||||||
local joint = cc.PhysicsJointPin:construct(mouse:getPhysicsBody(), body, location);
|
local joint = cc.PhysicsJointPin:construct(mouse:getPhysicsBody(), body, location);
|
||||||
joint:setMaxForce(5000.0 * body:getMass());
|
joint:setMaxForce(5000.0 * body:getMass());
|
||||||
cc.Director:getInstance():getRunningScene():getPhysicsWorld():addJoint(joint);
|
curLayer:getPhysicsWorld():addJoint(joint);
|
||||||
touch.mouse = mouse
|
touch.mouse = mouse
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -253,7 +253,7 @@ local function makeTriangle(point, size, color, material)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function PhysicsDemoClickAdd()
|
local function PhysicsDemoClickAdd()
|
||||||
local layer = cc.Layer:create()
|
local layer = cc.Layer:createWithPhysics()
|
||||||
local function onEnter()
|
local function onEnter()
|
||||||
local function onTouchEnded(touch, event)
|
local function onTouchEnded(touch, event)
|
||||||
local location = touch:getLocation();
|
local location = touch:getLocation();
|
||||||
|
@ -281,7 +281,7 @@ local function PhysicsDemoClickAdd()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function PhysicsDemoLogoSmash()
|
local function PhysicsDemoLogoSmash()
|
||||||
local layer = cc.Layer:create()
|
local layer = cc.Layer:createWithPhysics()
|
||||||
|
|
||||||
local function onEnter()
|
local function onEnter()
|
||||||
local logo_width = 188.0
|
local logo_width = 188.0
|
||||||
|
@ -327,8 +327,8 @@ local function PhysicsDemoLogoSmash()
|
||||||
return bit.band(bit.rshift(logo_image[bit.rshift(x, 3) + y*logo_raw_length + 1], bit.band(bit.bnot(x), 0x07)), 1)
|
return bit.band(bit.rshift(logo_image[bit.rshift(x, 3) + y*logo_raw_length + 1], bit.band(bit.bnot(x), 0x07)), 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0));
|
curLayer:getPhysicsWorld():setGravity(cc.p(0, 0));
|
||||||
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setUpdateRate(5.0);
|
curLayer:getPhysicsWorld():setUpdateRate(5.0);
|
||||||
|
|
||||||
layer.ball = cc.SpriteBatchNode:create("Images/ball.png", #logo_image);
|
layer.ball = cc.SpriteBatchNode:create("Images/ball.png", #logo_image);
|
||||||
layer:addChild(layer.ball);
|
layer:addChild(layer.ball);
|
||||||
|
@ -363,7 +363,7 @@ local function PhysicsDemoLogoSmash()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function PhysicsDemoJoints()
|
local function PhysicsDemoJoints()
|
||||||
local layer = cc.Layer:create()
|
local layer = cc.Layer:createWithPhysics()
|
||||||
local function onEnter()
|
local function onEnter()
|
||||||
layer:toggleDebug();
|
layer:toggleDebug();
|
||||||
|
|
||||||
|
@ -384,7 +384,6 @@ local function PhysicsDemoJoints()
|
||||||
node:setPosition(cc.p(0, 0));
|
node:setPosition(cc.p(0, 0));
|
||||||
layer:addChild(node);
|
layer:addChild(node);
|
||||||
|
|
||||||
local scene = cc.Director:getInstance():getRunningScene();
|
|
||||||
for i in range(0, 3) do
|
for i in range(0, 3) do
|
||||||
for j in range(0, 3) do
|
for j in range(0, 3) do
|
||||||
local offset = cc.p(VisibleRect:leftBottom().x + 5 + j * width + width/2, VisibleRect:leftBottom().y + 50 + i * height + height/2);
|
local offset = cc.p(VisibleRect:leftBottom().x + 5 + j * width + width/2, VisibleRect:leftBottom().y + 50 + i * height + height/2);
|
||||||
|
@ -397,7 +396,7 @@ local function PhysicsDemoJoints()
|
||||||
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
local joint = cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), offset);
|
local joint = cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), offset);
|
||||||
cc.Director:getInstance():getRunningScene():getPhysicsWorld():addJoint(joint);
|
curLayer:getPhysicsWorld():addJoint(joint);
|
||||||
|
|
||||||
layer:addChild(sp1);
|
layer:addChild(sp1);
|
||||||
layer:addChild(sp2);
|
layer:addChild(sp2);
|
||||||
|
@ -408,7 +407,7 @@ local function PhysicsDemoJoints()
|
||||||
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
local joint = cc.PhysicsJointFixed:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), offset);
|
local joint = cc.PhysicsJointFixed:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), offset);
|
||||||
scene:getPhysicsWorld():addJoint(joint);
|
curLayer:getPhysicsWorld():addJoint(joint);
|
||||||
|
|
||||||
layer:addChild(sp1);
|
layer:addChild(sp1);
|
||||||
layer:addChild(sp2);
|
layer:addChild(sp2);
|
||||||
|
@ -419,7 +418,7 @@ local function PhysicsDemoJoints()
|
||||||
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
local joint = cc.PhysicsJointDistance:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0));
|
local joint = cc.PhysicsJointDistance:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0));
|
||||||
scene:getPhysicsWorld():addJoint(joint);
|
curLayer:getPhysicsWorld():addJoint(joint);
|
||||||
|
|
||||||
layer:addChild(sp1);
|
layer:addChild(sp1);
|
||||||
layer:addChild(sp2);
|
layer:addChild(sp2);
|
||||||
|
@ -430,7 +429,7 @@ local function PhysicsDemoJoints()
|
||||||
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
local joint = cc.PhysicsJointLimit:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0), 30.0, 60.0);
|
local joint = cc.PhysicsJointLimit:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0), 30.0, 60.0);
|
||||||
scene:getPhysicsWorld():addJoint(joint);
|
curLayer:getPhysicsWorld():addJoint(joint);
|
||||||
|
|
||||||
layer:addChild(sp1);
|
layer:addChild(sp1);
|
||||||
layer:addChild(sp2);
|
layer:addChild(sp2);
|
||||||
|
@ -441,7 +440,7 @@ local function PhysicsDemoJoints()
|
||||||
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
local joint = cc.PhysicsJointSpring:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0), 500.0, 0.3);
|
local joint = cc.PhysicsJointSpring:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0), 500.0, 0.3);
|
||||||
scene:getPhysicsWorld():addJoint(joint);
|
curLayer:getPhysicsWorld():addJoint(joint);
|
||||||
|
|
||||||
layer:addChild(sp1);
|
layer:addChild(sp1);
|
||||||
layer:addChild(sp2);
|
layer:addChild(sp2);
|
||||||
|
@ -452,7 +451,7 @@ local function PhysicsDemoJoints()
|
||||||
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
local joint = cc.PhysicsJointGroove:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(30, 15), cc.p(30, -15), cc.p(-30, 0))
|
local joint = cc.PhysicsJointGroove:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(30, 15), cc.p(30, -15), cc.p(-30, 0))
|
||||||
scene:getPhysicsWorld():addJoint(joint);
|
curLayer:getPhysicsWorld():addJoint(joint);
|
||||||
|
|
||||||
layer:addChild(sp1);
|
layer:addChild(sp1);
|
||||||
layer:addChild(sp2);
|
layer:addChild(sp2);
|
||||||
|
@ -461,10 +460,10 @@ local function PhysicsDemoJoints()
|
||||||
sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
||||||
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
|
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
|
||||||
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
||||||
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
|
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
|
||||||
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
|
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
|
||||||
local joint = cc.PhysicsJointRotarySpring:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 3000.0, 60.0);
|
local joint = cc.PhysicsJointRotarySpring:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 3000.0, 60.0);
|
||||||
scene:getPhysicsWorld():addJoint(joint);
|
curLayer:getPhysicsWorld():addJoint(joint);
|
||||||
|
|
||||||
layer:addChild(sp1);
|
layer:addChild(sp1);
|
||||||
layer:addChild(sp2);
|
layer:addChild(sp2);
|
||||||
|
@ -474,10 +473,10 @@ local function PhysicsDemoJoints()
|
||||||
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
|
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
|
||||||
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
|
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
|
||||||
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
|
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
|
||||||
local joint = cc.PhysicsJointRotaryLimit:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, math.pi/2);
|
local joint = cc.PhysicsJointRotaryLimit:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, math.pi/2);
|
||||||
scene:getPhysicsWorld():addJoint(joint);
|
curLayer:getPhysicsWorld():addJoint(joint);
|
||||||
|
|
||||||
layer:addChild(sp1);
|
layer:addChild(sp1);
|
||||||
layer:addChild(sp2);
|
layer:addChild(sp2);
|
||||||
|
@ -487,10 +486,10 @@ local function PhysicsDemoJoints()
|
||||||
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
|
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
|
||||||
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
|
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
|
||||||
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
|
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
|
||||||
local joint = cc.PhysicsJointRatchet:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, math.pi/2);
|
local joint = cc.PhysicsJointRatchet:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, math.pi/2);
|
||||||
scene:getPhysicsWorld():addJoint(joint);
|
curLayer:getPhysicsWorld():addJoint(joint);
|
||||||
|
|
||||||
layer:addChild(sp1);
|
layer:addChild(sp1);
|
||||||
layer:addChild(sp2);
|
layer:addChild(sp2);
|
||||||
|
@ -500,10 +499,10 @@ local function PhysicsDemoJoints()
|
||||||
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
|
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
|
||||||
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
|
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
|
||||||
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
|
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
|
||||||
local joint = cc.PhysicsJointGear:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, 2.0);
|
local joint = cc.PhysicsJointGear:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, 2.0);
|
||||||
scene:getPhysicsWorld():addJoint(joint);
|
curLayer:getPhysicsWorld():addJoint(joint);
|
||||||
|
|
||||||
layer:addChild(sp1);
|
layer:addChild(sp1);
|
||||||
layer:addChild(sp2);
|
layer:addChild(sp2);
|
||||||
|
@ -513,10 +512,10 @@ local function PhysicsDemoJoints()
|
||||||
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
|
local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10));
|
||||||
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG);
|
||||||
|
|
||||||
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
|
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition())));
|
||||||
scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
|
curLayer:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition())));
|
||||||
local joint = cc.PhysicsJointMotor:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), math.pi/2);
|
local joint = cc.PhysicsJointMotor:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), math.pi/2);
|
||||||
scene:getPhysicsWorld():addJoint(joint);
|
curLayer:getPhysicsWorld():addJoint(joint);
|
||||||
|
|
||||||
layer:addChild(sp1);
|
layer:addChild(sp1);
|
||||||
layer:addChild(sp2);
|
layer:addChild(sp2);
|
||||||
|
@ -531,7 +530,7 @@ local function PhysicsDemoJoints()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function PhysicsDemoPyramidStack()
|
local function PhysicsDemoPyramidStack()
|
||||||
local layer = cc.Layer:create()
|
local layer = cc.Layer:createWithPhysics()
|
||||||
|
|
||||||
local function onEnter()
|
local function onEnter()
|
||||||
local touchListener = cc.EventListenerTouchOneByOne:create();
|
local touchListener = cc.EventListenerTouchOneByOne:create();
|
||||||
|
@ -569,7 +568,7 @@ local function PhysicsDemoPyramidStack()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function PhysicsDemoRayCast()
|
local function PhysicsDemoRayCast()
|
||||||
local layer = cc.Layer:create()
|
local layer = cc.Layer:createWithPhysics()
|
||||||
|
|
||||||
local function onEnter()
|
local function onEnter()
|
||||||
local function onTouchEnded(touch, event)
|
local function onTouchEnded(touch, event)
|
||||||
|
@ -591,7 +590,7 @@ local function PhysicsDemoRayCast()
|
||||||
local eventDispatcher = layer:getEventDispatcher()
|
local eventDispatcher = layer:getEventDispatcher()
|
||||||
eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer);
|
eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer);
|
||||||
|
|
||||||
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0,0));
|
curLayer:getPhysicsWorld():setGravity(cc.p(0,0));
|
||||||
|
|
||||||
local node = cc.DrawNode:create();
|
local node = cc.DrawNode:create();
|
||||||
node:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50)))
|
node:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50)))
|
||||||
|
@ -636,7 +635,7 @@ local function PhysicsDemoRayCast()
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2);
|
curLayer:getPhysicsWorld():rayCast(func, point1, point2);
|
||||||
drawNode:drawSegment(point1, point3, 1, STATIC_COLOR);
|
drawNode:drawSegment(point1, point3, 1, STATIC_COLOR);
|
||||||
|
|
||||||
if point2.x ~= point3.x or point2.y ~= point3.y then
|
if point2.x ~= point3.x or point2.y ~= point3.y then
|
||||||
|
@ -654,7 +653,7 @@ local function PhysicsDemoRayCast()
|
||||||
return true;
|
return true;
|
||||||
end
|
end
|
||||||
|
|
||||||
cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2);
|
curLayer:getPhysicsWorld():rayCast(func, point1, point2);
|
||||||
drawNode:drawSegment(point1, point3, 1, STATIC_COLOR);
|
drawNode:drawSegment(point1, point3, 1, STATIC_COLOR);
|
||||||
|
|
||||||
if point2.x ~= point3.x or point2.y ~= point3.y then
|
if point2.x ~= point3.x or point2.y ~= point3.y then
|
||||||
|
@ -669,7 +668,7 @@ local function PhysicsDemoRayCast()
|
||||||
return true;
|
return true;
|
||||||
end
|
end
|
||||||
|
|
||||||
cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2);
|
curLayer:getPhysicsWorld():rayCast(func, point1, point2);
|
||||||
drawNode:drawSegment(point1, point2, 1, STATIC_COLOR);
|
drawNode:drawSegment(point1, point2, 1, STATIC_COLOR);
|
||||||
|
|
||||||
for _, p in ipairs(points) do
|
for _, p in ipairs(points) do
|
||||||
|
@ -693,7 +692,7 @@ local function PhysicsDemoRayCast()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function PhysicsDemoOneWayPlatform()
|
local function PhysicsDemoOneWayPlatform()
|
||||||
local layer = cc.Layer:create()
|
local layer = cc.Layer:createWithPhysics()
|
||||||
local function onEnter()
|
local function onEnter()
|
||||||
|
|
||||||
local touchListener = cc.EventListenerTouchOneByOne:create();
|
local touchListener = cc.EventListenerTouchOneByOne:create();
|
||||||
|
@ -734,7 +733,7 @@ local function PhysicsDemoOneWayPlatform()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function PhysicsDemoActions()
|
local function PhysicsDemoActions()
|
||||||
local layer = cc.Layer:create()
|
local layer = cc.Layer:createWithPhysics()
|
||||||
local function onEnter()
|
local function onEnter()
|
||||||
local touchListener = cc.EventListenerTouchOneByOne:create()
|
local touchListener = cc.EventListenerTouchOneByOne:create()
|
||||||
touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
|
touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
|
||||||
|
@ -773,7 +772,7 @@ local function PhysicsDemoActions()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function PhysicsDemoPump()
|
local function PhysicsDemoPump()
|
||||||
local layer = cc.Layer:create()
|
local layer = cc.Layer:createWithPhysics()
|
||||||
local function onEnter()
|
local function onEnter()
|
||||||
layer:toggleDebug();
|
layer:toggleDebug();
|
||||||
|
|
||||||
|
@ -803,14 +802,14 @@ local function PhysicsDemoPump()
|
||||||
eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer)
|
eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer)
|
||||||
|
|
||||||
local function update()
|
local function update()
|
||||||
for _, body in ipairs(cc.Director:getInstance():getRunningScene():getPhysicsWorld():getAllBodies()) do
|
for _, body in ipairs(curLayer:getPhysicsWorld():getAllBodies()) do
|
||||||
if body:getTag() == DRAG_BODYS_TAG and body:getPosition().y < 0.0 then
|
if body:getTag() == DRAG_BODYS_TAG and body:getPosition().y < 0.0 then
|
||||||
body:getNode():setPosition(cc.p(VisibleRect:leftTop().x + 75, VisibleRect:leftTop().y + math.random() * 90, 0));
|
body:getNode():setPosition(cc.p(VisibleRect:leftTop().x + 75, VisibleRect:leftTop().y + math.random() * 90, 0));
|
||||||
body:setVelocity(cc.p(0, 0));
|
body:setVelocity(cc.p(0, 0));
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local gear = cc.Director:getInstance():getRunningScene():getPhysicsWorld():getBody(1);
|
local gear = curLayer:getPhysicsWorld():getBody(1);
|
||||||
if gear then
|
if gear then
|
||||||
if distance ~= 0.0 then
|
if distance ~= 0.0 then
|
||||||
rotationV = rotationV + distance/2500.0;
|
rotationV = rotationV + distance/2500.0;
|
||||||
|
@ -871,7 +870,7 @@ local function PhysicsDemoPump()
|
||||||
cc.p(VisibleRect:leftBottom().x + 102, VisibleRect:leftBottom().y + 20)
|
cc.p(VisibleRect:leftBottom().x + 102, VisibleRect:leftBottom().y + 20)
|
||||||
};
|
};
|
||||||
|
|
||||||
local world = cc.Director:getInstance():getRunningScene():getPhysicsWorld();
|
local world = curLayer:getPhysicsWorld();
|
||||||
|
|
||||||
-- small gear
|
-- small gear
|
||||||
local sgear = cc.Node:create();
|
local sgear = cc.Node:create();
|
||||||
|
@ -934,7 +933,7 @@ local function PhysicsDemoPump()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function PhysicsDemoSlice()
|
local function PhysicsDemoSlice()
|
||||||
local layer = cc.Layer:create()
|
local layer = cc.Layer:createWithPhysics()
|
||||||
local function onEnter()
|
local function onEnter()
|
||||||
layer:toggleDebug()
|
layer:toggleDebug()
|
||||||
local sliceTag = 1;
|
local sliceTag = 1;
|
||||||
|
@ -994,7 +993,7 @@ local function PhysicsDemoSlice()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function onTouchEnded(touch, event)
|
local function onTouchEnded(touch, event)
|
||||||
cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(slice, touch:getStartLocation(), touch:getLocation());
|
curLayer:getPhysicsWorld():rayCast(slice, touch:getStartLocation(), touch:getLocation());
|
||||||
end
|
end
|
||||||
|
|
||||||
local touchListener = cc.EventListenerTouchOneByOne:create();
|
local touchListener = cc.EventListenerTouchOneByOne:create();
|
||||||
|
@ -1025,10 +1024,10 @@ end
|
||||||
|
|
||||||
|
|
||||||
local function PhysicsDemoBug3988()
|
local function PhysicsDemoBug3988()
|
||||||
local layer = cc.Layer:create()
|
local layer = cc.Layer:createWithPhysics()
|
||||||
local function onEnter()
|
local function onEnter()
|
||||||
layer:toggleDebug();
|
layer:toggleDebug();
|
||||||
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0));
|
curLayer:getPhysicsWorld():setGravity(cc.p(0, 0));
|
||||||
|
|
||||||
local ball = cc.Sprite:create("Images/YellowSquare.png");
|
local ball = cc.Sprite:create("Images/YellowSquare.png");
|
||||||
ball:setPosition(cc.p(VisibleRect:center().x-100, VisibleRect:center().y));
|
ball:setPosition(cc.p(VisibleRect:center().x-100, VisibleRect:center().y));
|
||||||
|
@ -1048,9 +1047,9 @@ local function PhysicsDemoBug3988()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function PhysicsContactTest()
|
local function PhysicsContactTest()
|
||||||
local layer = cc.Layer:create()
|
local layer = cc.Layer:createWithPhysics()
|
||||||
local function onEnter()
|
local function onEnter()
|
||||||
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0));
|
curLayer:getPhysicsWorld():setGravity(cc.p(0, 0));
|
||||||
local s = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height);
|
local s = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height);
|
||||||
|
|
||||||
layer.yellowBoxNum = 50;
|
layer.yellowBoxNum = 50;
|
||||||
|
@ -1291,11 +1290,11 @@ local function PhysicsContactTest()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function PhysicsPositionRotationTest()
|
local function PhysicsPositionRotationTest()
|
||||||
local layer = cc.Layer:create()
|
local layer = cc.Layer:createWithPhysics()
|
||||||
local function onEnter()
|
local function onEnter()
|
||||||
layer:toggleDebug()
|
layer:toggleDebug()
|
||||||
|
|
||||||
cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0));
|
curLayer:getPhysicsWorld():setGravity(cc.p(0, 0));
|
||||||
|
|
||||||
local touchListener = cc.EventListenerTouchOneByOne:create()
|
local touchListener = cc.EventListenerTouchOneByOne:create()
|
||||||
touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
|
touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
|
||||||
|
@ -1351,10 +1350,9 @@ end
|
||||||
|
|
||||||
function PhysicsTest()
|
function PhysicsTest()
|
||||||
cclog("PhysicsTest")
|
cclog("PhysicsTest")
|
||||||
local scene = cc.Scene:createWithPhysics()
|
local scene = cc.Scene:create()
|
||||||
|
|
||||||
|
|
||||||
Helper.usePhysics = true
|
|
||||||
Helper.createFunctionTable = {
|
Helper.createFunctionTable = {
|
||||||
PhysicsDemoLogoSmash,
|
PhysicsDemoLogoSmash,
|
||||||
PhysicsDemoPyramidStack,
|
PhysicsDemoPyramidStack,
|
||||||
|
|
|
@ -31,7 +31,6 @@ end
|
||||||
|
|
||||||
-- back menu callback
|
-- back menu callback
|
||||||
local function MainMenuCallback()
|
local function MainMenuCallback()
|
||||||
Helper.usePhysics = false
|
|
||||||
local scene = cc.Scene:create()
|
local scene = cc.Scene:create()
|
||||||
scene:addChild(CreateTestMenu())
|
scene:addChild(CreateTestMenu())
|
||||||
|
|
||||||
|
@ -84,12 +83,7 @@ function Helper.restartAction()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Helper.newScene()
|
function Helper.newScene()
|
||||||
local scene
|
local scene = cc.Scene:create()
|
||||||
if Helper.usePhysics then
|
|
||||||
scene = cc.Scene:createWithPhysics()
|
|
||||||
else
|
|
||||||
scene = cc.Scene:create()
|
|
||||||
end
|
|
||||||
Helper.currentLayer = Helper.createFunctionTable[Helper.index]()
|
Helper.currentLayer = Helper.createFunctionTable[Helper.index]()
|
||||||
scene:addChild(Helper.currentLayer)
|
scene:addChild(Helper.currentLayer)
|
||||||
scene:addChild(CreateBackMenuItem())
|
scene:addChild(CreateBackMenuItem())
|
||||||
|
|
|
@ -42,7 +42,7 @@ skip = PhysicsBody::[getJoints createPolygon createEdgeChain createEdgePolygon],
|
||||||
PhysicsShapePolygon::[create calculateArea calculateMoment ^getPoints$],
|
PhysicsShapePolygon::[create calculateArea calculateMoment ^getPoints$],
|
||||||
PhysicsShapeEdgePolygon::[create ^getPoints$],
|
PhysicsShapeEdgePolygon::[create ^getPoints$],
|
||||||
PhysicsShapeEdgeChain::[create ^getPoints$],
|
PhysicsShapeEdgeChain::[create ^getPoints$],
|
||||||
PhysicsWorld::[getScene queryPoint queryRect rayCast],
|
PhysicsWorld::[getLayer queryPoint queryRect rayCast],
|
||||||
PhysicsContact::[getData setData]
|
PhysicsContact::[getData setData]
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue