mirror of https://github.com/axmolengine/axmol.git
Merge pull request #6545 from ricardoquesada/physics_rollback
Physics rollback
This commit is contained in:
commit
ff626f6825
|
@ -63,9 +63,6 @@ Layer::Layer()
|
|||
, _accelerationListener(nullptr)
|
||||
, _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
|
||||
, _swallowsTouches(true)
|
||||
#if CC_USE_PHYSICS
|
||||
, _physicsWorld(nullptr)
|
||||
#endif
|
||||
{
|
||||
_ignoreAnchorPointForPosition = true;
|
||||
setAnchorPoint(Vector2(0.5f, 0.5f));
|
||||
|
@ -73,9 +70,7 @@ Layer::Layer()
|
|||
|
||||
Layer::~Layer()
|
||||
{
|
||||
#if CC_USE_PHYSICS
|
||||
CC_SAFE_DELETE(_physicsWorld);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
bool Layer::init()
|
||||
|
@ -439,93 +434,6 @@ std::string Layer::getDescription() const
|
|||
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()
|
||||
{
|
||||
CCLOG("LayerRGBA deprecated.");
|
||||
|
|
|
@ -171,27 +171,6 @@ CC_CONSTRUCTOR_ACCESS:
|
|||
|
||||
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:
|
||||
//add the api for avoid use deprecated api
|
||||
void _addTouchListener();
|
||||
|
|
|
@ -43,7 +43,7 @@ THE SOFTWARE.
|
|||
#include "base/CCEventDispatcher.h"
|
||||
#include "base/CCEvent.h"
|
||||
#include "base/CCEventTouch.h"
|
||||
#include "2d/CCLayer.h"
|
||||
#include "2d/CCScene.h"
|
||||
|
||||
#if CC_USE_PHYSICS
|
||||
#include "physics/CCPhysicsBody.h"
|
||||
|
@ -267,8 +267,8 @@ void Node::setRotation(float rotation)
|
|||
#if CC_USE_PHYSICS
|
||||
if (_physicsBody && !_physicsBody->_rotationResetTag)
|
||||
{
|
||||
Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr;
|
||||
updatePhysicsBodyRotation(layer);
|
||||
Scene* scene = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getScene() : nullptr;
|
||||
updatePhysicsBodyRotation(scene);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -296,8 +296,8 @@ void Node::setRotation3D(const Vector3& rotation)
|
|||
#if CC_USE_PHYSICS
|
||||
if (_physicsBody)
|
||||
{
|
||||
Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr;
|
||||
updatePhysicsBodyRotation(layer);
|
||||
Scene* scene = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getScene() : nullptr;
|
||||
updatePhysicsBodyRotation(scene);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -428,8 +428,8 @@ void Node::setPosition(const Vector2& position)
|
|||
#if CC_USE_PHYSICS
|
||||
if (_physicsBody != nullptr && !_physicsBody->_positionResetTag)
|
||||
{
|
||||
Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr;
|
||||
updatePhysicsBodyPosition(layer);
|
||||
Scene* scene = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getScene() : nullptr;
|
||||
updatePhysicsBodyPosition(scene);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -734,10 +734,10 @@ void Node::addChild(Node *child, int zOrder, int tag)
|
|||
// 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)
|
||||
Scene* scene = dynamic_cast<Scene*>(node);
|
||||
if (scene != nullptr && scene->getPhysicsWorld() != nullptr)
|
||||
{
|
||||
layer->addChildToPhysicsWorld(child);
|
||||
scene->addChildToPhysicsWorld(child);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -1580,13 +1580,13 @@ void Node::removeAllComponents()
|
|||
|
||||
#if CC_USE_PHYSICS
|
||||
|
||||
void Node::updatePhysicsBodyPosition(Layer* layer)
|
||||
void Node::updatePhysicsBodyPosition(Scene* scene)
|
||||
{
|
||||
if (_physicsBody != nullptr)
|
||||
{
|
||||
if (layer != nullptr && layer->getPhysicsWorld() != nullptr)
|
||||
if (scene != nullptr && scene->getPhysicsWorld() != nullptr)
|
||||
{
|
||||
Vector2 pos = getParent() == layer ? getPosition() : layer->convertToNodeSpace(_parent->convertToWorldSpace(getPosition()));
|
||||
Vector2 pos = getParent() == scene ? getPosition() : scene->convertToNodeSpace(_parent->convertToWorldSpace(getPosition()));
|
||||
_physicsBody->setPosition(pos);
|
||||
}
|
||||
else
|
||||
|
@ -1596,14 +1596,14 @@ void Node::updatePhysicsBodyPosition(Layer* layer)
|
|||
}
|
||||
}
|
||||
|
||||
void Node::updatePhysicsBodyRotation(Layer* layer)
|
||||
void Node::updatePhysicsBodyRotation(Scene* scene)
|
||||
{
|
||||
if (_physicsBody != nullptr)
|
||||
{
|
||||
if (layer != nullptr && layer->getPhysicsWorld() != nullptr)
|
||||
if (scene != nullptr && scene->getPhysicsWorld() != nullptr)
|
||||
{
|
||||
float rotation = _rotationZ_X;
|
||||
for (Node* parent = _parent; parent != layer; parent = parent->getParent())
|
||||
for (Node* parent = _parent; parent != scene; parent = parent->getParent())
|
||||
{
|
||||
rotation += parent->getRotation();
|
||||
}
|
||||
|
@ -1650,19 +1650,19 @@ void Node::setPhysicsBody(PhysicsBody* body)
|
|||
if (body != nullptr)
|
||||
{
|
||||
Node* node;
|
||||
Layer* layer = nullptr;
|
||||
Scene* scene = nullptr;
|
||||
for (node = this->getParent(); node != nullptr; node = node->getParent())
|
||||
{
|
||||
Layer* tmpLayer = dynamic_cast<Layer*>(node);
|
||||
if (tmpLayer != nullptr && tmpLayer->getPhysicsWorld() != nullptr)
|
||||
Scene* tmpScene = dynamic_cast<Scene*>(node);
|
||||
if (tmpScene != nullptr && tmpScene->getPhysicsWorld() != nullptr)
|
||||
{
|
||||
layer = tmpLayer;
|
||||
scene = tmpScene;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
updatePhysicsBodyPosition(layer);
|
||||
updatePhysicsBodyRotation(layer);
|
||||
updatePhysicsBodyPosition(scene);
|
||||
updatePhysicsBodyRotation(scene);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1367,8 +1367,8 @@ protected:
|
|||
virtual void updateColor() {}
|
||||
|
||||
#if CC_USE_PHYSICS
|
||||
virtual void updatePhysicsBodyPosition(Layer* layer);
|
||||
virtual void updatePhysicsBodyRotation(Layer* layer);
|
||||
virtual void updatePhysicsBodyPosition(Scene* layer);
|
||||
virtual void updatePhysicsBodyRotation(Scene* layer);
|
||||
#endif // CC_USE_PHYSICS
|
||||
|
||||
float _rotationX; ///< rotation on the X-axis
|
||||
|
|
|
@ -36,6 +36,9 @@ THE SOFTWARE.
|
|||
NS_CC_BEGIN
|
||||
|
||||
Scene::Scene()
|
||||
#if CC_USE_PHYSICS
|
||||
: _physicsWorld(nullptr)
|
||||
#endif
|
||||
{
|
||||
_ignoreAnchorPointForPosition = true;
|
||||
setAnchorPoint(Vector2(0.5f, 0.5f));
|
||||
|
@ -43,6 +46,9 @@ Scene::Scene()
|
|||
|
||||
Scene::~Scene()
|
||||
{
|
||||
#if CC_USE_PHYSICS
|
||||
CC_SAFE_DELETE(_physicsWorld);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Scene::init()
|
||||
|
@ -84,4 +90,75 @@ Scene* Scene::getScene()
|
|||
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
|
||||
|
|
|
@ -67,10 +67,28 @@ CC_CONSTRUCTOR_ACCESS:
|
|||
virtual bool init() override;
|
||||
|
||||
protected:
|
||||
friend class Node;
|
||||
friend class ProtectedNode;
|
||||
friend class SpriteBatchNode;
|
||||
|
||||
private:
|
||||
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
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
|
||||
#include "chipmunk.h"
|
||||
|
||||
#include "2d/CCLayer.h"
|
||||
#include "2d/CCScene.h"
|
||||
|
||||
#include "physics/CCPhysicsShape.h"
|
||||
#include "physics/CCPhysicsJoint.h"
|
||||
|
@ -764,11 +764,11 @@ void PhysicsBody::update(float delta)
|
|||
if (_node != nullptr)
|
||||
{
|
||||
Node* parent = _node->getParent();
|
||||
Layer* layer = &_world->getLayer();
|
||||
Scene* scene = &_world->getScene();
|
||||
|
||||
Vector2 position = parent != layer ? parent->convertToNodeSpace(layer->convertToWorldSpace(getPosition())) : getPosition();
|
||||
Vector2 position = parent != scene ? parent->convertToNodeSpace(scene->convertToWorldSpace(getPosition())) : getPosition();
|
||||
float rotation = getRotation();
|
||||
for (; parent != layer; parent = parent->getParent())
|
||||
for (; parent != scene; parent = parent->getParent())
|
||||
{
|
||||
rotation -= parent->getRotation();
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@
|
|||
#include "chipmunk/CCPhysicsHelper_chipmunk.h"
|
||||
|
||||
#include "2d/CCDrawNode.h"
|
||||
#include "2d/CCLayer.h"
|
||||
#include "2d/CCScene.h"
|
||||
#include "base/CCDirector.h"
|
||||
#include "base/CCEventDispatcher.h"
|
||||
#include "base/CCEventCustom.h"
|
||||
|
@ -289,7 +289,7 @@ int PhysicsWorld::collisionBeginCallback(PhysicsContact& contact)
|
|||
{
|
||||
contact.setEventCode(PhysicsContact::EventCode::BEGIN);
|
||||
contact.setWorld(this);
|
||||
_layer->getEventDispatcher()->dispatchEvent(&contact);
|
||||
_scene->getEventDispatcher()->dispatchEvent(&contact);
|
||||
}
|
||||
|
||||
return ret ? contact.resetResult() : false;
|
||||
|
@ -305,7 +305,7 @@ int PhysicsWorld::collisionPreSolveCallback(PhysicsContact& contact)
|
|||
|
||||
contact.setEventCode(PhysicsContact::EventCode::PRESOLVE);
|
||||
contact.setWorld(this);
|
||||
_layer->getEventDispatcher()->dispatchEvent(&contact);
|
||||
_scene->getEventDispatcher()->dispatchEvent(&contact);
|
||||
|
||||
return contact.resetResult();
|
||||
}
|
||||
|
@ -319,7 +319,7 @@ void PhysicsWorld::collisionPostSolveCallback(PhysicsContact& contact)
|
|||
|
||||
contact.setEventCode(PhysicsContact::EventCode::POSTSOLVE);
|
||||
contact.setWorld(this);
|
||||
_layer->getEventDispatcher()->dispatchEvent(&contact);
|
||||
_scene->getEventDispatcher()->dispatchEvent(&contact);
|
||||
}
|
||||
|
||||
void PhysicsWorld::collisionSeparateCallback(PhysicsContact& contact)
|
||||
|
@ -331,7 +331,7 @@ void PhysicsWorld::collisionSeparateCallback(PhysicsContact& contact)
|
|||
|
||||
contact.setEventCode(PhysicsContact::EventCode::SEPERATE);
|
||||
contact.setWorld(this);
|
||||
_layer->getEventDispatcher()->dispatchEvent(&contact);
|
||||
_scene->getEventDispatcher()->dispatchEvent(&contact);
|
||||
}
|
||||
|
||||
void PhysicsWorld::rayCast(PhysicsRayCastCallbackFunc func, const Vector2& point1, const Vector2& point2, void* data)
|
||||
|
@ -416,10 +416,10 @@ PhysicsShape* PhysicsWorld::getShape(const Vector2& point) const
|
|||
return shape == nullptr ? nullptr : PhysicsShapeInfo::getMap().find(shape)->second->getShape();
|
||||
}
|
||||
|
||||
PhysicsWorld* PhysicsWorld::construct(Layer& layer)
|
||||
PhysicsWorld* PhysicsWorld::construct(Scene& scene)
|
||||
{
|
||||
PhysicsWorld * world = new PhysicsWorld();
|
||||
if(world && world->init(layer))
|
||||
if(world && world->init(scene))
|
||||
{
|
||||
return world;
|
||||
}
|
||||
|
@ -428,14 +428,14 @@ PhysicsWorld* PhysicsWorld::construct(Layer& layer)
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bool PhysicsWorld::init(Layer& layer)
|
||||
bool PhysicsWorld::init(Scene& scene)
|
||||
{
|
||||
do
|
||||
{
|
||||
_info = new PhysicsWorldInfo();
|
||||
CC_BREAK_IF(_info == nullptr);
|
||||
|
||||
_layer = &layer;
|
||||
_scene = &scene;
|
||||
|
||||
_info->setGravity(_gravity);
|
||||
|
||||
|
@ -911,7 +911,7 @@ PhysicsWorld::PhysicsWorld()
|
|||
, _updateRateCount(0)
|
||||
, _updateTime(0.0f)
|
||||
, _info(nullptr)
|
||||
, _layer(nullptr)
|
||||
, _scene(nullptr)
|
||||
, _delayDirty(false)
|
||||
, _debugDraw(nullptr)
|
||||
, _debugDrawMask(DEBUGDRAW_NONE)
|
||||
|
@ -932,7 +932,7 @@ PhysicsDebugDraw::PhysicsDebugDraw(PhysicsWorld& world)
|
|||
, _world(world)
|
||||
{
|
||||
_drawNode = DrawNode::create();
|
||||
_world.getLayer().addChild(_drawNode);
|
||||
_world.getScene().addChild(_drawNode);
|
||||
}
|
||||
|
||||
PhysicsDebugDraw::~PhysicsDebugDraw()
|
||||
|
|
|
@ -46,7 +46,7 @@ typedef Vector2 Vect;
|
|||
|
||||
class Node;
|
||||
class Sprite;
|
||||
class Layer;
|
||||
class Scene;
|
||||
class DrawNode;
|
||||
class PhysicsDebugDraw;
|
||||
|
||||
|
@ -119,8 +119,8 @@ public:
|
|||
/** Get body by tag */
|
||||
PhysicsBody* getBody(int tag) const;
|
||||
|
||||
/** Get layer contain this physics world */
|
||||
inline Layer& getLayer() const { return *_layer; }
|
||||
/** Get scene contain this physics world */
|
||||
inline Scene& getScene() const { return *_scene; }
|
||||
/** get the gravity value */
|
||||
inline Vect getGravity() const { return _gravity; }
|
||||
/** set the gravity value */
|
||||
|
@ -144,8 +144,8 @@ public:
|
|||
inline int getDebugDrawMask() { return _debugDrawMask; }
|
||||
|
||||
protected:
|
||||
static PhysicsWorld* construct(Layer& layer);
|
||||
bool init(Layer& layer);
|
||||
static PhysicsWorld* construct(Scene& scene);
|
||||
bool init(Scene& scene);
|
||||
|
||||
virtual void addBody(PhysicsBody* body);
|
||||
virtual void addShape(PhysicsShape* shape);
|
||||
|
@ -180,7 +180,7 @@ protected:
|
|||
|
||||
Vector<PhysicsBody*> _bodies;
|
||||
std::list<PhysicsJoint*> _joints;
|
||||
Layer* _layer;
|
||||
Scene* _scene;
|
||||
|
||||
bool _delayDirty;
|
||||
PhysicsDebugDraw* _debugDraw;
|
||||
|
@ -198,7 +198,7 @@ protected:
|
|||
|
||||
friend class Node;
|
||||
friend class Sprite;
|
||||
friend class Layer;
|
||||
friend class Scene;
|
||||
friend class PhysicsBody;
|
||||
friend class PhysicsShape;
|
||||
friend class PhysicsJoint;
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
#if CC_USE_PHYSICS
|
||||
#include "physics/CCPhysicsBody.h"
|
||||
#endif
|
||||
#include "2d/CCLayer.h"
|
||||
#include "2d/CCScene.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
|
@ -104,10 +104,10 @@ void ProtectedNode::addProtectedChild(Node *child, int zOrder, int tag)
|
|||
// 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)
|
||||
Scene* scene = dynamic_cast<Scene*>(node);
|
||||
if (scene != nullptr && scene->getPhysicsWorld() != nullptr)
|
||||
{
|
||||
layer->addChildToPhysicsWorld(child);
|
||||
scene->addChildToPhysicsWorld(child);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,27 +3,23 @@
|
|||
#include "../testResource.h"
|
||||
USING_NS_CC;
|
||||
|
||||
#if CC_USE_PHYSICS
|
||||
#define CLPHYSICS(__className__) [](){ return __className__::createWithPhysics();}
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
static std::function<Layer*()> createFunctions[] = {
|
||||
#if CC_USE_PHYSICS
|
||||
CLPHYSICS(PhysicsDemoLogoSmash),
|
||||
CLPHYSICS(PhysicsDemoPyramidStack),
|
||||
CLPHYSICS(PhysicsDemoClickAdd),
|
||||
CLPHYSICS(PhysicsDemoRayCast),
|
||||
CLPHYSICS(PhysicsDemoJoints),
|
||||
CLPHYSICS(PhysicsDemoActions),
|
||||
CLPHYSICS(PhysicsDemoPump),
|
||||
CLPHYSICS(PhysicsDemoOneWayPlatform),
|
||||
CLPHYSICS(PhysicsDemoSlice),
|
||||
CLPHYSICS(PhysicsDemoBug3988),
|
||||
CLPHYSICS(PhysicsContactTest),
|
||||
CL(PhysicsDemoLogoSmash),
|
||||
CL(PhysicsDemoPyramidStack),
|
||||
CL(PhysicsDemoClickAdd),
|
||||
CL(PhysicsDemoRayCast),
|
||||
CL(PhysicsDemoJoints),
|
||||
CL(PhysicsDemoActions),
|
||||
CL(PhysicsDemoPump),
|
||||
CL(PhysicsDemoOneWayPlatform),
|
||||
CL(PhysicsDemoSlice),
|
||||
CL(PhysicsDemoBug3988),
|
||||
CL(PhysicsContactTest),
|
||||
CL(PhysicsPositionRotationTest),
|
||||
CLPHYSICS(PhysicsSetGravityEnableTest),
|
||||
CL(PhysicsSetGravityEnableTest),
|
||||
#else
|
||||
CL(PhysicsDemoDisabled),
|
||||
#endif
|
||||
|
@ -63,7 +59,12 @@ namespace
|
|||
}
|
||||
|
||||
PhysicsTestScene::PhysicsTestScene()
|
||||
#if CC_USE_PHYSICS
|
||||
: TestScene(false, true)
|
||||
#else
|
||||
: TestScene()
|
||||
#endif
|
||||
, _debugDraw(false)
|
||||
{}
|
||||
|
||||
void PhysicsTestScene::runThisTest()
|
||||
|
@ -74,6 +75,14 @@ void PhysicsTestScene::runThisTest()
|
|||
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
|
||||
void PhysicsDemoDisabled::onEnter()
|
||||
{
|
||||
|
@ -88,10 +97,9 @@ void PhysicsDemoDisabled::onEnter()
|
|||
#else
|
||||
|
||||
PhysicsDemo::PhysicsDemo()
|
||||
: _spriteTexture(nullptr)
|
||||
: _scene(nullptr)
|
||||
, _spriteTexture(nullptr)
|
||||
, _ball(nullptr)
|
||||
, _debugDraw(false)
|
||||
, _isRoot(true)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -100,12 +108,6 @@ PhysicsDemo::~PhysicsDemo()
|
|||
|
||||
}
|
||||
|
||||
void PhysicsDemo::toggleDebug()
|
||||
{
|
||||
_debugDraw = !_debugDraw;
|
||||
getPhysicsWorld()->setDebugDrawMask(_debugDraw ? PhysicsWorld::DEBUGDRAW_ALL : PhysicsWorld::DEBUGDRAW_NONE);
|
||||
}
|
||||
|
||||
std::string PhysicsDemo::title() const
|
||||
{
|
||||
return "PhysicsTest";
|
||||
|
@ -142,26 +144,19 @@ void PhysicsDemo::backCallback(Ref* sender)
|
|||
|
||||
void PhysicsDemo::onEnter()
|
||||
{
|
||||
if (_isRoot)
|
||||
{
|
||||
BaseTest::onEnter();
|
||||
}
|
||||
else
|
||||
{
|
||||
Layer::onEnter();
|
||||
}
|
||||
BaseTest::onEnter();
|
||||
|
||||
_scene = dynamic_cast<PhysicsTestScene*>(this->getParent());
|
||||
|
||||
_spriteTexture = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 100)->getTexture();
|
||||
|
||||
if (this->_physicsWorld != nullptr)
|
||||
{
|
||||
// menu for debug layer
|
||||
MenuItemFont::setFontSize(18);
|
||||
auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(PhysicsDemo::toggleDebugCallback, this));
|
||||
auto menu = Menu::create(item, nullptr);
|
||||
this->addChild(menu);
|
||||
menu->setPosition(Vector2(getContentSize().width-50, getContentSize().height/2));
|
||||
}
|
||||
// menu for debug layer
|
||||
MenuItemFont::setFontSize(18);
|
||||
auto item = MenuItemFont::create("Toggle debug", CC_CALLBACK_1(PhysicsDemo::toggleDebugCallback, this));
|
||||
|
||||
auto menu = Menu::create(item, NULL);
|
||||
this->addChild(menu);
|
||||
menu->setPosition(Vector2(VisibleRect::right().x-50, VisibleRect::top().y-10));
|
||||
}
|
||||
|
||||
Sprite* PhysicsDemo::addGrossiniAtPosition(Vector2 p, float scale/* = 1.0*/)
|
||||
|
@ -188,7 +183,10 @@ Sprite* PhysicsDemo::addGrossiniAtPosition(Vector2 p, float scale/* = 1.0*/)
|
|||
|
||||
void PhysicsDemo::toggleDebugCallback(Ref* sender)
|
||||
{
|
||||
toggleDebug();
|
||||
if (_scene != nullptr)
|
||||
{
|
||||
_scene->toggleDebug();
|
||||
}
|
||||
}
|
||||
|
||||
PhysicsDemoClickAdd::~PhysicsDemoClickAdd()
|
||||
|
@ -248,7 +246,10 @@ void PhysicsDemoClickAdd::onAcceleration(Acceleration* acc, Event* event)
|
|||
auto v = Vector2( accelX, accelY);
|
||||
v = v * 200;
|
||||
|
||||
getPhysicsWorld()->setGravity(v);
|
||||
if(_scene != nullptr)
|
||||
{
|
||||
_scene->getPhysicsWorld()->setGravity(v);
|
||||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
|
@ -380,8 +381,8 @@ Sprite* PhysicsDemo::makeTriangle(Vector2 point, Size size, int color, PhysicsMa
|
|||
|
||||
bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event)
|
||||
{
|
||||
auto location = this->convertTouchToNodeSpace(touch);
|
||||
auto arr = getPhysicsWorld()->getShapes(location);
|
||||
auto location = touch->getLocation();
|
||||
auto arr = _scene->getPhysicsWorld()->getShapes(location);
|
||||
|
||||
PhysicsBody* body = nullptr;
|
||||
for (auto& obj : arr)
|
||||
|
@ -402,7 +403,7 @@ bool PhysicsDemo::onTouchBegan(Touch* touch, Event* event)
|
|||
this->addChild(mouse);
|
||||
PhysicsJointPin* joint = PhysicsJointPin::construct(mouse->getPhysicsBody(), body, location);
|
||||
joint->setMaxForce(5000.0f * body->getMass());
|
||||
getPhysicsWorld()->addJoint(joint);
|
||||
_scene->getPhysicsWorld()->addJoint(joint);
|
||||
_mouses.insert(std::make_pair(touch->getID(), mouse));
|
||||
|
||||
return true;
|
||||
|
@ -417,7 +418,7 @@ void PhysicsDemo::onTouchMoved(Touch* touch, Event* event)
|
|||
|
||||
if (it != _mouses.end())
|
||||
{
|
||||
it->second->setPosition(this->convertTouchToNodeSpace(touch));
|
||||
it->second->setPosition(touch->getLocation());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -436,8 +437,9 @@ void PhysicsDemo::onTouchEnded(Touch* touch, Event* event)
|
|||
void PhysicsDemoLogoSmash::onEnter()
|
||||
{
|
||||
PhysicsDemo::onEnter();
|
||||
getPhysicsWorld()->setGravity(Vector2::ZERO);
|
||||
getPhysicsWorld()->setUpdateRate(5.0f);
|
||||
|
||||
_scene->getPhysicsWorld()->setGravity(Vector2(0, 0));
|
||||
_scene->getPhysicsWorld()->setUpdateRate(5.0f);
|
||||
|
||||
_ball = SpriteBatchNode::create("Images/ball.png", sizeof(logo_image)/sizeof(logo_image[0]));
|
||||
addChild(_ball);
|
||||
|
@ -463,6 +465,7 @@ void PhysicsDemoLogoSmash::onEnter()
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
auto bullet = makeBall(Vector2(400, 0), 10, PhysicsMaterial(PHYSICS_INFINITY, 0, 0));
|
||||
bullet->getPhysicsBody()->setVelocity(Vector2(200, 0));
|
||||
|
||||
|
@ -524,7 +527,7 @@ std::string PhysicsDemoPyramidStack::title() const
|
|||
}
|
||||
|
||||
PhysicsDemoRayCast::PhysicsDemoRayCast()
|
||||
: _angle(0.0f)
|
||||
: _angle(0.0f)
|
||||
, _node(nullptr)
|
||||
, _mode(0)
|
||||
{}
|
||||
|
@ -536,9 +539,8 @@ void PhysicsDemoRayCast::onEnter()
|
|||
auto listener = EventListenerTouchAllAtOnce::create();
|
||||
listener->onTouchesEnded = CC_CALLBACK_2(PhysicsDemoRayCast::onTouchesEnded, this);
|
||||
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
scheduleUpdate();
|
||||
|
||||
getPhysicsWorld()->setGravity(Point::ZERO);
|
||||
_scene->getPhysicsWorld()->setGravity(Point::ZERO);
|
||||
|
||||
auto node = DrawNode::create();
|
||||
node->setPhysicsBody(PhysicsBody::createEdgeSegment(VisibleRect::leftBottom() + Vector2(0, 50), VisibleRect::rightBottom() + Vector2(0, 50)));
|
||||
|
@ -551,6 +553,8 @@ void PhysicsDemoRayCast::onEnter()
|
|||
auto menu = Menu::create(item, NULL);
|
||||
this->addChild(menu);
|
||||
menu->setPosition(Vector2(VisibleRect::left().x+100, VisibleRect::top().y-10));
|
||||
|
||||
scheduleUpdate();
|
||||
}
|
||||
|
||||
void PhysicsDemoRayCast::changeModeCallback(Ref* sender)
|
||||
|
@ -596,7 +600,7 @@ void PhysicsDemoRayCast::update(float delta)
|
|||
Vector2 point3 = point2;
|
||||
auto func = CC_CALLBACK_3(PhysicsDemoRayCast::anyRay, this);
|
||||
|
||||
getPhysicsWorld()->rayCast(func, point1, point2, &point3);
|
||||
_scene->getPhysicsWorld()->rayCast(func, point1, point2, &point3);
|
||||
_node->drawSegment(point1, point3, 1, STATIC_COLOR);
|
||||
|
||||
if (point2 != point3)
|
||||
|
@ -622,7 +626,7 @@ void PhysicsDemoRayCast::update(float delta)
|
|||
return true;
|
||||
};
|
||||
|
||||
getPhysicsWorld()->rayCast(func, point1, point2, nullptr);
|
||||
_scene->getPhysicsWorld()->rayCast(func, point1, point2, nullptr);
|
||||
_node->drawSegment(point1, point3, 1, STATIC_COLOR);
|
||||
|
||||
if (point2 != point3)
|
||||
|
@ -649,7 +653,7 @@ void PhysicsDemoRayCast::update(float delta)
|
|||
return true;
|
||||
};
|
||||
|
||||
getPhysicsWorld()->rayCast(func, point1, point2, nullptr);
|
||||
_scene->getPhysicsWorld()->rayCast(func, point1, point2, nullptr);
|
||||
|
||||
_node->drawSegment(point1, point2, 1, STATIC_COLOR);
|
||||
|
||||
|
@ -701,7 +705,7 @@ std::string PhysicsDemoRayCast::title() const
|
|||
void PhysicsDemoJoints::onEnter()
|
||||
{
|
||||
PhysicsDemo::onEnter();
|
||||
toggleDebug();
|
||||
_scene->toggleDebug();
|
||||
|
||||
auto listener = EventListenerTouchOneByOne::create();
|
||||
listener->onTouchBegan = CC_CALLBACK_2(PhysicsDemoJoints::onTouchBegan, this);
|
||||
|
@ -709,6 +713,8 @@ void PhysicsDemoJoints::onEnter()
|
|||
listener->onTouchEnded = CC_CALLBACK_2(PhysicsDemoJoints::onTouchEnded, this);
|
||||
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
|
||||
//_scene->getPhysicsWorld()->setGravity(Point::ZERO);
|
||||
|
||||
float width = (VisibleRect::getVisibleRect().size.width - 10) / 4;
|
||||
float height = (VisibleRect::getVisibleRect().size.height - 50) / 4;
|
||||
|
||||
|
@ -716,7 +722,7 @@ void PhysicsDemoJoints::onEnter()
|
|||
PhysicsBody* box = PhysicsBody::create();
|
||||
node->setPhysicsBody(box);
|
||||
box->setDynamic(false);
|
||||
node->setPosition(Vector2::ZERO);
|
||||
node->setPosition(Point::ZERO);
|
||||
this->addChild(node);
|
||||
|
||||
for (int i = 0; i < 4; ++i)
|
||||
|
@ -736,7 +742,7 @@ void PhysicsDemoJoints::onEnter()
|
|||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
|
||||
PhysicsJointPin* joint = PhysicsJointPin::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), offset);
|
||||
getPhysicsWorld()->addJoint(joint);
|
||||
_scene->getPhysicsWorld()->addJoint(joint);
|
||||
|
||||
this->addChild(sp1);
|
||||
this->addChild(sp2);
|
||||
|
@ -751,7 +757,7 @@ void PhysicsDemoJoints::onEnter()
|
|||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
|
||||
PhysicsJointFixed* joint = PhysicsJointFixed::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), offset);
|
||||
getPhysicsWorld()->addJoint(joint);
|
||||
_scene->getPhysicsWorld()->addJoint(joint);
|
||||
|
||||
this->addChild(sp1);
|
||||
this->addChild(sp2);
|
||||
|
@ -766,7 +772,7 @@ void PhysicsDemoJoints::onEnter()
|
|||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
|
||||
PhysicsJointDistance* joint = PhysicsJointDistance::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO);
|
||||
getPhysicsWorld()->addJoint(joint);
|
||||
_scene->getPhysicsWorld()->addJoint(joint);
|
||||
|
||||
this->addChild(sp1);
|
||||
this->addChild(sp2);
|
||||
|
@ -780,7 +786,7 @@ void PhysicsDemoJoints::onEnter()
|
|||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
|
||||
PhysicsJointLimit* joint = PhysicsJointLimit::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO, 30.0f, 60.0f);
|
||||
getPhysicsWorld()->addJoint(joint);
|
||||
_scene->getPhysicsWorld()->addJoint(joint);
|
||||
|
||||
this->addChild(sp1);
|
||||
this->addChild(sp2);
|
||||
|
@ -794,7 +800,7 @@ void PhysicsDemoJoints::onEnter()
|
|||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
|
||||
PhysicsJointSpring* joint = PhysicsJointSpring::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Point::ZERO, Point::ZERO, 500.0f, 0.3f);
|
||||
getPhysicsWorld()->addJoint(joint);
|
||||
_scene->getPhysicsWorld()->addJoint(joint);
|
||||
|
||||
this->addChild(sp1);
|
||||
this->addChild(sp2);
|
||||
|
@ -808,7 +814,7 @@ void PhysicsDemoJoints::onEnter()
|
|||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
|
||||
PhysicsJointGroove* joint = PhysicsJointGroove::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), Vector2(30, 15), Vector2(30, -15), Vector2(-30, 0));
|
||||
getPhysicsWorld()->addJoint(joint);
|
||||
_scene->getPhysicsWorld()->addJoint(joint);
|
||||
|
||||
this->addChild(sp1);
|
||||
this->addChild(sp2);
|
||||
|
@ -821,10 +827,10 @@ void PhysicsDemoJoints::onEnter()
|
|||
auto sp2 = makeBox(offset + Vector2(30, 0), Size(30, 10));
|
||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
|
||||
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||
PhysicsJointRotarySpring* joint = PhysicsJointRotarySpring::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 3000.0f, 60.0f);
|
||||
getPhysicsWorld()->addJoint(joint);
|
||||
_scene->getPhysicsWorld()->addJoint(joint);
|
||||
|
||||
this->addChild(sp1);
|
||||
this->addChild(sp2);
|
||||
|
@ -837,10 +843,10 @@ void PhysicsDemoJoints::onEnter()
|
|||
auto sp2 = makeBox(offset + Vector2(30, 0), Size(30, 10));
|
||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
|
||||
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||
PhysicsJointRotaryLimit* joint = PhysicsJointRotaryLimit::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f,(float) M_PI_2);
|
||||
getPhysicsWorld()->addJoint(joint);
|
||||
_scene->getPhysicsWorld()->addJoint(joint);
|
||||
|
||||
this->addChild(sp1);
|
||||
this->addChild(sp2);
|
||||
|
@ -853,10 +859,10 @@ void PhysicsDemoJoints::onEnter()
|
|||
auto sp2 = makeBox(offset + Vector2(30, 0), Size(30, 10));
|
||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
|
||||
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||
PhysicsJointRatchet* joint = PhysicsJointRatchet::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f, (float)M_PI_2);
|
||||
getPhysicsWorld()->addJoint(joint);
|
||||
_scene->getPhysicsWorld()->addJoint(joint);
|
||||
|
||||
this->addChild(sp1);
|
||||
this->addChild(sp2);
|
||||
|
@ -869,10 +875,10 @@ void PhysicsDemoJoints::onEnter()
|
|||
auto sp2 = makeBox(offset + Vector2(30, 0), Size(30, 10));
|
||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
|
||||
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||
PhysicsJointGear* joint = PhysicsJointGear::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), 0.0f, 2.0f);
|
||||
getPhysicsWorld()->addJoint(joint);
|
||||
_scene->getPhysicsWorld()->addJoint(joint);
|
||||
|
||||
this->addChild(sp1);
|
||||
this->addChild(sp2);
|
||||
|
@ -885,10 +891,10 @@ void PhysicsDemoJoints::onEnter()
|
|||
auto sp2 = makeBox(offset + Vector2(30, 0), Size(30, 10));
|
||||
sp2->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
|
||||
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||
getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp1->getPhysicsBody(), box, sp1->getPosition()));
|
||||
_scene->getPhysicsWorld()->addJoint(PhysicsJointPin::construct(sp2->getPhysicsBody(), box, sp2->getPosition()));
|
||||
PhysicsJointMotor* joint = PhysicsJointMotor::construct(sp1->getPhysicsBody(), sp2->getPhysicsBody(), (float)M_PI_2);
|
||||
getPhysicsWorld()->addJoint(joint);
|
||||
_scene->getPhysicsWorld()->addJoint(joint);
|
||||
|
||||
this->addChild(sp1);
|
||||
this->addChild(sp2);
|
||||
|
@ -947,7 +953,7 @@ std::string PhysicsDemoActions::title() const
|
|||
void PhysicsDemoPump::onEnter()
|
||||
{
|
||||
PhysicsDemo::onEnter();
|
||||
toggleDebug();
|
||||
_scene->toggleDebug();
|
||||
|
||||
_distance = 0.0f;
|
||||
_rotationV = 0.0f;
|
||||
|
@ -992,7 +998,7 @@ void PhysicsDemoPump::onEnter()
|
|||
VisibleRect::leftBottom() + Vector2(102, 20)
|
||||
};
|
||||
|
||||
auto _world = getPhysicsWorld();
|
||||
auto _world = _scene->getPhysicsWorld();
|
||||
|
||||
// small gear
|
||||
auto sgear = Node::create();
|
||||
|
@ -1048,7 +1054,7 @@ void PhysicsDemoPump::onEnter()
|
|||
|
||||
void PhysicsDemoPump::update(float delta)
|
||||
{
|
||||
for (const auto& body : getPhysicsWorld()->getAllBodies())
|
||||
for (const auto& body : _scene->getPhysicsWorld()->getAllBodies())
|
||||
{
|
||||
if (body->getTag() == DRAG_BODYS_TAG && body->getPosition().y < 0.0f)
|
||||
{
|
||||
|
@ -1057,7 +1063,7 @@ void PhysicsDemoPump::update(float delta)
|
|||
}
|
||||
}
|
||||
|
||||
PhysicsBody* gear = getPhysicsWorld()->getBody(1);
|
||||
PhysicsBody* gear = _scene->getPhysicsWorld()->getBody(1);
|
||||
|
||||
if (gear != nullptr)
|
||||
{
|
||||
|
@ -1151,7 +1157,7 @@ std::string PhysicsDemoOneWayPlatform::title() const
|
|||
void PhysicsDemoSlice::onEnter()
|
||||
{
|
||||
PhysicsDemo::onEnter();
|
||||
toggleDebug();
|
||||
_scene->toggleDebug();
|
||||
|
||||
_sliceTag = 1;
|
||||
|
||||
|
@ -1239,7 +1245,7 @@ void PhysicsDemoSlice::clipPoly(PhysicsShapePolygon* shape, Vector2 normal, floa
|
|||
void PhysicsDemoSlice::onTouchEnded(Touch *touch, Event *event)
|
||||
{
|
||||
auto func = CC_CALLBACK_3(PhysicsDemoSlice::slice, this);
|
||||
getPhysicsWorld()->rayCast(func, touch->getStartLocation(), touch->getLocation(), nullptr);
|
||||
_scene->getPhysicsWorld()->rayCast(func, touch->getStartLocation(), touch->getLocation(), nullptr);
|
||||
}
|
||||
|
||||
std::string PhysicsDemoSlice::title() const
|
||||
|
@ -1256,8 +1262,8 @@ std::string PhysicsDemoSlice::subtitle() const
|
|||
void PhysicsDemoBug3988::onEnter()
|
||||
{
|
||||
PhysicsDemo::onEnter();
|
||||
toggleDebug();
|
||||
getPhysicsWorld()->setGravity(Vect::ZERO);
|
||||
_scene->toggleDebug();
|
||||
_scene->getPhysicsWorld()->setGravity(Vect::ZERO);
|
||||
|
||||
auto ball = Sprite::create("Images/YellowSquare.png");
|
||||
ball->setPosition(VisibleRect::center() - Vector2(100, 0));
|
||||
|
@ -1282,7 +1288,7 @@ std::string PhysicsDemoBug3988::subtitle() const
|
|||
void PhysicsContactTest::onEnter()
|
||||
{
|
||||
PhysicsDemo::onEnter();
|
||||
getPhysicsWorld()->setGravity(Vect::ZERO);
|
||||
_scene->getPhysicsWorld()->setGravity(Vect::ZERO);
|
||||
auto s = VisibleRect::getVisibleRect().size;
|
||||
|
||||
_yellowBoxNum = 50;
|
||||
|
@ -1544,51 +1550,36 @@ std::string PhysicsContactTest::subtitle() const
|
|||
void PhysicsPositionRotationTest::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;
|
||||
auto touchListener = EventListenerTouchOneByOne::create();
|
||||
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, this);
|
||||
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemo::onTouchMoved, this);
|
||||
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemo::onTouchEnded, this);
|
||||
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
|
||||
|
||||
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();
|
||||
touchListener->onTouchBegan = CC_CALLBACK_2(PhysicsDemo::onTouchBegan, layers[i]);
|
||||
touchListener->onTouchMoved = CC_CALLBACK_2(PhysicsDemo::onTouchMoved, layers[i]);
|
||||
touchListener->onTouchEnded = CC_CALLBACK_2(PhysicsDemo::onTouchEnded, layers[i]);
|
||||
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, layers[i]);
|
||||
|
||||
auto wall = Node::create();
|
||||
wall->setPhysicsBody(PhysicsBody::createEdgeBox(layers[i]->getContentSize(), PhysicsMaterial(0.1f, 1, 0.0f)));
|
||||
wall->setPosition(Vector2(halfSize/2));
|
||||
layers[i]->addChild(wall);
|
||||
this->addChild(layers[i]);
|
||||
}
|
||||
layers[0]->setPosition(Vector2(halfSize.width/2, halfSize.height/2 + 60));
|
||||
layers[1]->setPosition(Vector2(halfSize.width/2*3, halfSize.height/2 + 60));
|
||||
auto wall = Node::create();
|
||||
wall->setPhysicsBody(PhysicsBody::createEdgeBox(VisibleRect::getVisibleRect().size));
|
||||
wall->setPosition(VisibleRect::center());
|
||||
addChild(wall);
|
||||
|
||||
// anchor test
|
||||
auto anchorNode = Sprite::create("Images/YellowSquare.png");
|
||||
anchorNode->setAnchorPoint(Vector2(0.1f, 0.9f));
|
||||
anchorNode->setPosition(100, 50);
|
||||
anchorNode->setPosition(100, 100);
|
||||
anchorNode->setScale(0.25);
|
||||
anchorNode->setPhysicsBody(PhysicsBody::createBox(anchorNode->getContentSize()*anchorNode->getScale()));
|
||||
anchorNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
layers[0]->addChild(anchorNode);
|
||||
addChild(anchorNode);
|
||||
|
||||
//parent test
|
||||
auto parent = Sprite::create("Images/YellowSquare.png");
|
||||
parent->setPosition(160, 50);
|
||||
parent->setPosition(200, 100);
|
||||
parent->setScale(0.25);
|
||||
parent->setPhysicsBody(PhysicsBody::createBox(parent->getContentSize()*anchorNode->getScale()));
|
||||
parent->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
layers[0]->addChild(parent);
|
||||
addChild(parent);
|
||||
|
||||
auto leftBall = Sprite::create("Images/ball.png");
|
||||
leftBall->setPosition(-30, 0);
|
||||
|
@ -1599,37 +1590,12 @@ void PhysicsPositionRotationTest::onEnter()
|
|||
|
||||
// offset position rotation test
|
||||
auto offsetPosNode = Sprite::create("Images/YellowSquare.png");
|
||||
offsetPosNode->setPosition(100, 100);
|
||||
offsetPosNode->setPosition(100, 200);
|
||||
offsetPosNode->setPhysicsBody(PhysicsBody::createBox(offsetPosNode->getContentSize()/2));
|
||||
offsetPosNode->getPhysicsBody()->setPositionOffset(-Vector2(offsetPosNode->getContentSize()/2));
|
||||
offsetPosNode->getPhysicsBody()->setRotationOffset(45);
|
||||
offsetPosNode->getPhysicsBody()->setTag(DRAG_BODYS_TAG);
|
||||
layers[0]->addChild(offsetPosNode);
|
||||
|
||||
|
||||
for (int i = 0; i < 30; ++i)
|
||||
{
|
||||
Size size(10 + CCRANDOM_0_1()*10, 10 + CCRANDOM_0_1()*10);
|
||||
Vector2 position = Vector2(halfSize.width, halfSize.height) - Vector2(size.width, size.height);
|
||||
position.x = position.x * CCRANDOM_0_1();
|
||||
position.y = position.y * CCRANDOM_0_1();
|
||||
position = position + Vector2(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, Vector2(halfSize.width/2, halfSize.height/2 + 60));
|
||||
MoveTo* moveBack = MoveTo::create(1.0f, Vector2(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));
|
||||
addChild(offsetPosNode);
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -1639,11 +1605,6 @@ std::string PhysicsPositionRotationTest::title() const
|
|||
return "Position/Rotation Test";
|
||||
}
|
||||
|
||||
std::string PhysicsPositionRotationTest::subtitle() const
|
||||
{
|
||||
return "Two Physics Worlds";
|
||||
}
|
||||
|
||||
|
||||
void PhysicsSetGravityEnableTest::onEnter()
|
||||
{
|
||||
|
@ -1686,7 +1647,7 @@ void PhysicsSetGravityEnableTest::onScheduleOnce(float delta)
|
|||
auto ball = getChildByTag(2);
|
||||
ball->getPhysicsBody()->setMass(200);
|
||||
|
||||
getPhysicsWorld()->setGravity(Vect(0, 98));
|
||||
_scene->getPhysicsWorld()->setGravity(Vect(0, 98));
|
||||
}
|
||||
|
||||
std::string PhysicsSetGravityEnableTest::title() const
|
||||
|
|
|
@ -15,6 +15,11 @@ public:
|
|||
|
||||
public:
|
||||
virtual void runThisTest();
|
||||
|
||||
void toggleDebug();
|
||||
|
||||
private:
|
||||
bool _debugDraw;
|
||||
};
|
||||
|
||||
#if CC_USE_PHYSICS == 0
|
||||
|
@ -27,26 +32,11 @@ public:
|
|||
};
|
||||
#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
|
||||
{
|
||||
public:
|
||||
CREATE_WITH_PHYSICS_FUNC(PhysicsDemo);
|
||||
CREATE_FUNC(PhysicsDemo);
|
||||
|
||||
PhysicsDemo();
|
||||
virtual ~PhysicsDemo();
|
||||
|
||||
|
@ -68,23 +58,17 @@ public:
|
|||
void onTouchMoved(Touch* touch, Event* event);
|
||||
void onTouchEnded(Touch* touch, Event* event);
|
||||
|
||||
void toggleDebug();
|
||||
inline void setRoot(bool isRoot) { _isRoot = isRoot; }
|
||||
|
||||
protected:
|
||||
PhysicsTestScene* _scene;
|
||||
Texture2D* _spriteTexture; // weak ref
|
||||
SpriteBatchNode* _ball;
|
||||
std::unordered_map<int, Node*> _mouses;
|
||||
|
||||
private:
|
||||
bool _debugDraw;
|
||||
bool _isRoot;
|
||||
};
|
||||
|
||||
class PhysicsDemoClickAdd : public PhysicsDemo
|
||||
{
|
||||
public:
|
||||
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoClickAdd);
|
||||
CREATE_FUNC(PhysicsDemoClickAdd);
|
||||
|
||||
virtual ~PhysicsDemoClickAdd();
|
||||
void onEnter() override;
|
||||
|
@ -97,7 +81,7 @@ public:
|
|||
class PhysicsDemoLogoSmash : public PhysicsDemo
|
||||
{
|
||||
public:
|
||||
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoLogoSmash);
|
||||
CREATE_FUNC(PhysicsDemoLogoSmash);
|
||||
|
||||
void onEnter() override;
|
||||
virtual std::string title() const override;
|
||||
|
@ -106,7 +90,7 @@ public:
|
|||
class PhysicsDemoPyramidStack : public PhysicsDemo
|
||||
{
|
||||
public:
|
||||
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoPyramidStack);
|
||||
CREATE_FUNC(PhysicsDemoPyramidStack);
|
||||
|
||||
void onEnter() override;
|
||||
void updateOnce(float delta);
|
||||
|
@ -116,7 +100,7 @@ public:
|
|||
class PhysicsDemoRayCast : public PhysicsDemo
|
||||
{
|
||||
public:
|
||||
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoRayCast);
|
||||
CREATE_FUNC(PhysicsDemoRayCast);
|
||||
|
||||
PhysicsDemoRayCast();
|
||||
|
||||
|
@ -138,7 +122,7 @@ private:
|
|||
class PhysicsDemoJoints : public PhysicsDemo
|
||||
{
|
||||
public:
|
||||
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoJoints);
|
||||
CREATE_FUNC(PhysicsDemoJoints);
|
||||
|
||||
void onEnter() override;
|
||||
virtual std::string title() const override;
|
||||
|
@ -147,7 +131,7 @@ public:
|
|||
class PhysicsDemoActions : public PhysicsDemo
|
||||
{
|
||||
public:
|
||||
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoActions);
|
||||
CREATE_FUNC(PhysicsDemoActions);
|
||||
|
||||
void onEnter() override;
|
||||
virtual std::string title() const override;
|
||||
|
@ -156,7 +140,7 @@ public:
|
|||
class PhysicsDemoPump : public PhysicsDemo
|
||||
{
|
||||
public:
|
||||
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoPump);
|
||||
CREATE_FUNC(PhysicsDemoPump);
|
||||
|
||||
void onEnter() override;
|
||||
void update(float delta) override;
|
||||
|
@ -175,7 +159,7 @@ private:
|
|||
class PhysicsDemoOneWayPlatform : public PhysicsDemo
|
||||
{
|
||||
public:
|
||||
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoOneWayPlatform);
|
||||
CREATE_FUNC(PhysicsDemoOneWayPlatform);
|
||||
|
||||
void onEnter() override;
|
||||
virtual std::string title() const override;
|
||||
|
@ -186,7 +170,7 @@ public:
|
|||
class PhysicsDemoSlice : public PhysicsDemo
|
||||
{
|
||||
public:
|
||||
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoSlice);
|
||||
CREATE_FUNC(PhysicsDemoSlice);
|
||||
|
||||
void onEnter() override;
|
||||
virtual std::string title() const override;
|
||||
|
@ -204,7 +188,7 @@ private:
|
|||
class PhysicsDemoBug3988 : public PhysicsDemo
|
||||
{
|
||||
public:
|
||||
CREATE_WITH_PHYSICS_FUNC(PhysicsDemoBug3988);
|
||||
CREATE_FUNC(PhysicsDemoBug3988);
|
||||
|
||||
void onEnter() override;
|
||||
virtual std::string title() const override;
|
||||
|
@ -214,7 +198,7 @@ public:
|
|||
class PhysicsContactTest : public PhysicsDemo
|
||||
{
|
||||
public:
|
||||
CREATE_WITH_PHYSICS_FUNC(PhysicsContactTest);
|
||||
CREATE_FUNC(PhysicsContactTest);
|
||||
|
||||
void onEnter() override;
|
||||
void resetTest();
|
||||
|
@ -239,13 +223,12 @@ public:
|
|||
|
||||
void onEnter() override;
|
||||
virtual std::string title() const override;
|
||||
virtual std::string subtitle() const override;
|
||||
};
|
||||
|
||||
class PhysicsSetGravityEnableTest : public PhysicsDemo
|
||||
{
|
||||
public:
|
||||
CREATE_WITH_PHYSICS_FUNC(PhysicsSetGravityEnableTest);
|
||||
CREATE_FUNC(PhysicsSetGravityEnableTest);
|
||||
|
||||
void onEnter() override;
|
||||
void onScheduleOnce(float delta);
|
||||
|
|
|
@ -3,9 +3,20 @@
|
|||
#include "extensions/cocos-ext.h"
|
||||
#include "cocostudio/CocoStudio.h"
|
||||
|
||||
TestScene::TestScene(bool bPortrait)
|
||||
TestScene::TestScene(bool bPortrait, bool physics/* = false*/)
|
||||
{
|
||||
Scene::init();
|
||||
if (physics)
|
||||
{
|
||||
#if CC_USE_PHYSICS
|
||||
TestScene::initWithPhysics();
|
||||
#else
|
||||
Scene::init();
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
Scene::init();
|
||||
}
|
||||
}
|
||||
|
||||
void testScene_callback(Ref *sender )
|
||||
|
|
|
@ -9,7 +9,7 @@ USING_NS_CC;
|
|||
class TestScene : public Scene
|
||||
{
|
||||
public:
|
||||
TestScene(bool bPortrait = false);
|
||||
TestScene(bool bPortrait = false, bool physics = false);
|
||||
virtual void onEnter() override;
|
||||
|
||||
virtual void runThisTest() = 0;
|
||||
|
|
Loading…
Reference in New Issue