2012-11-12 15:22:26 +08:00
|
|
|
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
|
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2012-11-12 15:22:26 +08:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CCPhysicsSprite.h"
|
2015-08-01 16:50:16 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
2012-11-12 15:22:26 +08:00
|
|
|
|
2014-09-22 13:25:47 +08:00
|
|
|
#if (CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION)
|
2014-09-19 00:35:01 +08:00
|
|
|
|
2014-01-13 11:24:49 +08:00
|
|
|
#if (CC_ENABLE_CHIPMUNK_INTEGRATION && CC_ENABLE_BOX2D_INTEGRATION)
|
2013-07-02 02:52:04 +08:00
|
|
|
#error "Either Chipmunk or Box2d should be enabled, but not both at the same time"
|
|
|
|
#endif
|
|
|
|
|
2012-11-12 15:22:26 +08:00
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
2016-03-01 05:53:00 +08:00
|
|
|
#include "chipmunk/chipmunk.h"
|
2012-11-12 15:22:26 +08:00
|
|
|
#elif CC_ENABLE_BOX2D_INTEGRATION
|
2020-11-08 13:11:40 +08:00
|
|
|
#include "box2d/box2d.h"
|
2012-11-12 15:22:26 +08:00
|
|
|
#endif
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
NS_CC_EXT_BEGIN
|
2012-11-12 15:22:26 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsSprite::PhysicsSprite()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _ignoreBodyRotation(false)
|
2014-01-26 17:56:14 +08:00
|
|
|
, _CPBody(nullptr)
|
|
|
|
, _pB2Body(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _PTMRatio(0.0f)
|
2015-08-01 16:50:16 +08:00
|
|
|
, _syncTransform(nullptr)
|
2012-11-12 15:22:26 +08:00
|
|
|
{}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsSprite* PhysicsSprite::create()
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
|
2012-11-12 15:22:26 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsSprite* PhysicsSprite::createWithTexture(Texture2D *pTexture)
|
2012-11-20 16:17:20 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
|
2012-11-20 16:17:20 +08:00
|
|
|
if (pRet && pRet->initWithTexture(pTexture))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsSprite* PhysicsSprite::createWithTexture(Texture2D *pTexture, const Rect& rect)
|
2012-11-20 16:17:20 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
|
2012-11-20 16:17:20 +08:00
|
|
|
if (pRet && pRet->initWithTexture(pTexture, rect))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsSprite* PhysicsSprite::createWithSpriteFrame(SpriteFrame *pSpriteFrame)
|
2012-11-20 16:17:20 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
|
2012-11-20 16:17:20 +08:00
|
|
|
if (pRet && pRet->initWithSpriteFrame(pSpriteFrame))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsSprite* PhysicsSprite::createWithSpriteFrameName(const char *pszSpriteFrameName)
|
2012-11-20 16:17:20 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
|
2012-11-20 16:17:20 +08:00
|
|
|
if (pRet && pRet->initWithSpriteFrameName(pszSpriteFrameName))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsSprite* PhysicsSprite::create(const char *pszFileName)
|
2012-11-20 16:17:20 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
|
2012-11-20 16:17:20 +08:00
|
|
|
if (pRet && pRet->initWithFile(pszFileName))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
PhysicsSprite* PhysicsSprite::create(const char *pszFileName, const Rect& rect)
|
2012-11-20 16:17:20 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
|
2012-11-20 16:17:20 +08:00
|
|
|
if (pRet && pRet->initWithFile(pszFileName, rect))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-11-12 15:22:26 +08:00
|
|
|
// this method will only get called if the sprite is batched.
|
|
|
|
// return YES if the physic's values (angles, position ) changed.
|
2013-07-17 09:16:04 +08:00
|
|
|
// If you return NO, then getNodeToParentTransform won't be called.
|
2013-07-05 13:58:59 +08:00
|
|
|
bool PhysicsSprite::isDirty() const
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool PhysicsSprite::isIgnoreBodyRotation() const
|
2012-11-22 09:49:37 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _ignoreBodyRotation;
|
2012-11-22 09:49:37 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void PhysicsSprite::setIgnoreBodyRotation(bool bIgnoreBodyRotation)
|
2012-11-22 09:49:37 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_ignoreBodyRotation = bIgnoreBodyRotation;
|
2012-11-22 09:49:37 +08:00
|
|
|
}
|
|
|
|
|
2013-03-20 08:35:52 +08:00
|
|
|
// Override the setters and getters to always reflect the body's properties.
|
2014-05-15 01:07:09 +08:00
|
|
|
const Vec2& PhysicsSprite::getPosition() const
|
2013-03-20 08:35:52 +08:00
|
|
|
{
|
2013-07-05 15:06:38 +08:00
|
|
|
return getPosFromPhysics();
|
2013-03-20 08:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 15:06:38 +08:00
|
|
|
void PhysicsSprite::getPosition(float* x, float* y) const
|
2013-03-20 08:35:52 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
if (x == nullptr || y == nullptr) {
|
2013-07-05 15:06:38 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-05-15 01:07:09 +08:00
|
|
|
const Vec2& pos = getPosFromPhysics();
|
2013-07-05 15:06:38 +08:00
|
|
|
*x = pos.x;
|
|
|
|
*y = pos.y;
|
2013-03-20 08:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 15:06:38 +08:00
|
|
|
float PhysicsSprite::getPositionX() const
|
2013-03-20 08:35:52 +08:00
|
|
|
{
|
2013-07-05 15:06:38 +08:00
|
|
|
return getPosFromPhysics().x;
|
2013-03-20 08:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 15:06:38 +08:00
|
|
|
float PhysicsSprite::getPositionY() const
|
2013-03-20 08:35:52 +08:00
|
|
|
{
|
2013-07-05 15:06:38 +08:00
|
|
|
return getPosFromPhysics().y;
|
2013-03-20 08:35:52 +08:00
|
|
|
}
|
|
|
|
|
2015-11-19 17:17:59 +08:00
|
|
|
Vec3 PhysicsSprite::getPosition3D() const
|
|
|
|
{
|
|
|
|
Vec2 pos = getPosFromPhysics();
|
|
|
|
return Vec3(pos.x, pos.y, 0);
|
|
|
|
}
|
|
|
|
|
2013-07-02 02:52:04 +08:00
|
|
|
//
|
|
|
|
// Chipmunk only
|
|
|
|
//
|
|
|
|
|
2014-01-26 17:56:14 +08:00
|
|
|
|
2012-11-12 15:22:26 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
cpBody* PhysicsSprite::getCPBody() const
|
2012-11-20 16:17:20 +08:00
|
|
|
{
|
2014-01-26 17:56:14 +08:00
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
2013-06-15 14:03:30 +08:00
|
|
|
return _CPBody;
|
2014-01-26 17:56:14 +08:00
|
|
|
#else
|
|
|
|
CCASSERT(false, "Can't call chipmunk methods when Chipmunk is disabled");
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
2012-11-20 16:17:20 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void PhysicsSprite::setCPBody(cpBody *pBody)
|
2012-11-20 16:17:20 +08:00
|
|
|
{
|
2014-01-26 17:56:14 +08:00
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
2013-06-15 14:03:30 +08:00
|
|
|
_CPBody = pBody;
|
2014-01-26 17:56:14 +08:00
|
|
|
#else
|
|
|
|
CCASSERT(false, "Can't call chipmunk methods when Chipmunk is disabled");
|
|
|
|
#endif
|
2012-11-20 16:17:20 +08:00
|
|
|
}
|
|
|
|
|
2013-07-02 02:52:04 +08:00
|
|
|
b2Body* PhysicsSprite::getB2Body() const
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2014-01-26 17:56:14 +08:00
|
|
|
#if CC_ENABLE_BOX2D_INTEGRATION
|
2013-06-15 14:03:30 +08:00
|
|
|
return _pB2Body;
|
2014-01-26 17:56:14 +08:00
|
|
|
#else
|
|
|
|
CCASSERT(false, "Can't call box2d methods when Box2d is disabled");
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void PhysicsSprite::setB2Body(b2Body *pBody)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2014-01-26 17:56:14 +08:00
|
|
|
#if CC_ENABLE_BOX2D_INTEGRATION
|
2013-06-15 14:03:30 +08:00
|
|
|
_pB2Body = pBody;
|
2014-01-26 17:56:14 +08:00
|
|
|
#else
|
2016-11-16 09:48:37 +08:00
|
|
|
CC_UNUSED_PARAM(pBody);
|
2014-01-26 17:56:14 +08:00
|
|
|
CCASSERT(false, "Can't call box2d methods when Box2d is disabled");
|
|
|
|
#endif
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
float PhysicsSprite::getPTMRatio() const
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2014-01-26 17:56:14 +08:00
|
|
|
#if CC_ENABLE_BOX2D_INTEGRATION
|
2013-06-15 14:03:30 +08:00
|
|
|
return _PTMRatio;
|
2014-01-26 17:56:14 +08:00
|
|
|
#else
|
|
|
|
CCASSERT(false, "Can't call box2d methods when Box2d is disabled");
|
|
|
|
return 0;
|
|
|
|
#endif
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void PhysicsSprite::setPTMRatio(float fRatio)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2014-01-26 17:56:14 +08:00
|
|
|
#if CC_ENABLE_BOX2D_INTEGRATION
|
|
|
|
_PTMRatio = fRatio;
|
|
|
|
#else
|
2016-11-16 09:48:37 +08:00
|
|
|
CC_UNUSED_PARAM(fRatio);
|
2014-01-26 17:56:14 +08:00
|
|
|
CCASSERT(false, "Can't call box2d methods when Box2d is disabled");
|
2013-07-02 02:52:04 +08:00
|
|
|
#endif
|
2014-01-26 17:56:14 +08:00
|
|
|
}
|
2013-07-02 02:52:04 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Common to Box2d and Chipmunk
|
|
|
|
//
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
const Vec2& PhysicsSprite::getPosFromPhysics() const
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
static Vec2 s_physicPosion;
|
2013-07-02 02:52:04 +08:00
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
|
|
|
|
2016-03-01 05:53:00 +08:00
|
|
|
cpVect cpPos = cpBodyGetPosition(_CPBody);
|
|
|
|
s_physicPosion = Vec2(cpPos.x, cpPos.y);
|
2013-07-02 02:52:04 +08:00
|
|
|
|
|
|
|
#elif CC_ENABLE_BOX2D_INTEGRATION
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
b2Vec2 pos = _pB2Body->GetPosition();
|
|
|
|
float x = pos.x * _PTMRatio;
|
|
|
|
float y = pos.y * _PTMRatio;
|
2015-04-20 01:40:52 +08:00
|
|
|
s_physicPosion.set(x,y);
|
2013-07-02 02:52:04 +08:00
|
|
|
#endif
|
2013-07-05 15:06:38 +08:00
|
|
|
return s_physicPosion;
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2015-11-19 17:17:59 +08:00
|
|
|
void PhysicsSprite::setPosition(float x, float y)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-07-02 02:52:04 +08:00
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
2016-03-01 05:53:00 +08:00
|
|
|
|
2015-11-19 17:17:59 +08:00
|
|
|
cpVect cpPos = cpv(x, y);
|
2016-03-01 05:53:00 +08:00
|
|
|
cpBodySetPosition(_CPBody, cpPos);
|
|
|
|
|
2013-07-02 02:52:04 +08:00
|
|
|
#elif CC_ENABLE_BOX2D_INTEGRATION
|
2015-11-19 17:17:59 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float angle = _pB2Body->GetAngle();
|
2015-11-19 17:17:59 +08:00
|
|
|
_pB2Body->SetTransform(b2Vec2(x / _PTMRatio, y / _PTMRatio), angle);
|
2013-07-02 02:52:04 +08:00
|
|
|
#endif
|
2015-11-19 17:17:59 +08:00
|
|
|
}
|
2013-07-02 02:52:04 +08:00
|
|
|
|
2015-11-19 17:17:59 +08:00
|
|
|
void PhysicsSprite::setPosition(const Vec2 &pos)
|
|
|
|
{
|
|
|
|
setPosition(pos.x, pos.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsSprite::setPositionX(float x)
|
|
|
|
{
|
|
|
|
setPosition(x, getPositionY());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsSprite::setPositionY(float y)
|
|
|
|
{
|
|
|
|
setPosition(getPositionX(), y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsSprite::setPosition3D(const Vec3& position)
|
|
|
|
{
|
|
|
|
setPosition(position.x, position.y);
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 15:06:38 +08:00
|
|
|
float PhysicsSprite::getRotation() const
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-07-02 02:52:04 +08:00
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
|
|
|
|
|
|
|
return (_ignoreBodyRotation ? Sprite::getRotation() : -CC_RADIANS_TO_DEGREES(cpBodyGetAngle(_CPBody)));
|
|
|
|
|
|
|
|
#elif CC_ENABLE_BOX2D_INTEGRATION
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
return (_ignoreBodyRotation ? Sprite::getRotation() :
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_RADIANS_TO_DEGREES(_pB2Body->GetAngle()));
|
2014-09-22 13:19:08 +08:00
|
|
|
#else
|
|
|
|
return 0.0f;
|
2013-07-02 02:52:04 +08:00
|
|
|
#endif
|
|
|
|
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void PhysicsSprite::setRotation(float fRotation)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_ignoreBodyRotation)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Sprite::setRotation(fRotation);
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
2013-07-02 02:52:04 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cpBodySetAngle(_CPBody, -CC_DEGREES_TO_RADIANS(fRotation));
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif CC_ENABLE_BOX2D_INTEGRATION
|
2012-11-12 15:22:26 +08:00
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
b2Vec2 p = _pB2Body->GetPosition();
|
2012-11-12 15:22:26 +08:00
|
|
|
float radians = CC_DEGREES_TO_RADIANS(fRotation);
|
2013-06-15 14:03:30 +08:00
|
|
|
_pB2Body->SetTransform(p, radians);
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
2013-07-02 02:52:04 +08:00
|
|
|
#endif
|
|
|
|
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2014-04-23 17:11:55 +08:00
|
|
|
void PhysicsSprite::syncPhysicsTransform() const
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-07-02 02:52:04 +08:00
|
|
|
// Although scale is not used by physics engines, it is calculated just in case
|
2016-08-19 16:28:47 +08:00
|
|
|
// the sprite is animated (scaled up/down) using actions.
|
|
|
|
// For more info see: http://www.cocos2d-iphone.org/forum/topic/68990
|
2014-04-23 17:11:55 +08:00
|
|
|
|
2013-07-02 02:52:04 +08:00
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
2014-04-23 17:11:55 +08:00
|
|
|
|
2016-08-19 16:28:47 +08:00
|
|
|
cpVect rot = (_ignoreBodyRotation ? cpvforangle(-CC_DEGREES_TO_RADIANS(_rotationX)) : cpBodyGetRotation(_CPBody));
|
|
|
|
float x = cpBodyGetPosition(_CPBody).x + rot.x * -_anchorPointInPoints.x * _scaleX - rot.y * -_anchorPointInPoints.y * _scaleY;
|
|
|
|
float y = cpBodyGetPosition(_CPBody).y + rot.y * -_anchorPointInPoints.x * _scaleX + rot.x * -_anchorPointInPoints.y * _scaleY;
|
2014-04-23 17:11:55 +08:00
|
|
|
|
2016-08-19 16:28:47 +08:00
|
|
|
if (_ignoreAnchorPointForPosition)
|
2013-07-02 02:52:04 +08:00
|
|
|
{
|
2016-08-19 16:28:47 +08:00
|
|
|
x += _anchorPointInPoints.x;
|
|
|
|
y += _anchorPointInPoints.y;
|
|
|
|
}
|
2013-12-17 14:50:40 +08:00
|
|
|
|
2014-04-16 14:08:58 +08:00
|
|
|
float mat[] = { (float)rot.x * _scaleX, (float)rot.y * _scaleX, 0, 0,
|
2014-04-25 16:20:24 +08:00
|
|
|
(float)-rot.y * _scaleY, (float)rot.x * _scaleY, 0, 0,
|
2014-04-23 17:11:55 +08:00
|
|
|
0, 0, 1, 0,
|
2016-08-19 16:28:47 +08:00
|
|
|
x, y, 0, 1};
|
2014-04-23 17:11:55 +08:00
|
|
|
|
2014-04-08 23:05:33 +08:00
|
|
|
|
2014-04-16 10:46:10 +08:00
|
|
|
_transform.set(mat);
|
2013-12-17 14:50:40 +08:00
|
|
|
|
2013-07-02 02:52:04 +08:00
|
|
|
#elif CC_ENABLE_BOX2D_INTEGRATION
|
2014-04-23 17:11:55 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
b2Vec2 pos = _pB2Body->GetPosition();
|
2014-04-23 17:11:55 +08:00
|
|
|
|
2016-08-19 16:28:47 +08:00
|
|
|
float x = pos.x * _PTMRatio;
|
|
|
|
float y = pos.y * _PTMRatio;
|
2014-04-23 17:11:55 +08:00
|
|
|
|
2016-08-19 16:28:47 +08:00
|
|
|
if (_ignoreAnchorPointForPosition)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2016-08-19 16:28:47 +08:00
|
|
|
x += _anchorPointInPoints.x;
|
|
|
|
y += _anchorPointInPoints.y;
|
|
|
|
}
|
2014-04-23 17:11:55 +08:00
|
|
|
|
2016-08-19 16:28:47 +08:00
|
|
|
// Make matrix
|
|
|
|
float radians = _pB2Body->GetAngle();
|
|
|
|
float c = cosf(radians);
|
|
|
|
float s = sinf(radians);
|
2014-04-23 17:11:55 +08:00
|
|
|
|
2016-08-19 16:28:47 +08:00
|
|
|
if (!_anchorPointInPoints.isZero())
|
2013-02-27 16:12:28 +08:00
|
|
|
{
|
2016-08-19 16:28:47 +08:00
|
|
|
x += ((c * -_anchorPointInPoints.x * _scaleX) + (-s * -_anchorPointInPoints.y * _scaleY));
|
|
|
|
y += ((s * -_anchorPointInPoints.x * _scaleX) + (c * -_anchorPointInPoints.y * _scaleY));
|
|
|
|
}
|
2014-04-23 17:11:55 +08:00
|
|
|
|
2016-08-19 16:28:47 +08:00
|
|
|
// Rot, Translate Matrix
|
2014-01-26 18:32:50 +08:00
|
|
|
|
2014-04-11 17:27:55 +08:00
|
|
|
float mat[] = { (float)c * _scaleX, (float)s * _scaleX, 0, 0,
|
|
|
|
(float)-s * _scaleY, (float)c * _scaleY, 0, 0,
|
2014-01-26 18:32:50 +08:00
|
|
|
0, 0, 1, 0,
|
2016-08-19 16:28:47 +08:00
|
|
|
x, y, 0, 1};
|
2014-01-26 18:32:50 +08:00
|
|
|
|
2014-04-11 17:27:55 +08:00
|
|
|
_transform.set(mat);
|
2014-04-23 17:11:55 +08:00
|
|
|
#endif
|
|
|
|
}
|
2012-11-12 15:22:26 +08:00
|
|
|
|
2015-06-08 13:43:51 +08:00
|
|
|
void PhysicsSprite::onEnter()
|
2014-04-23 17:11:55 +08:00
|
|
|
{
|
2015-06-08 13:43:51 +08:00
|
|
|
Node::onEnter();
|
2015-08-01 16:50:16 +08:00
|
|
|
_syncTransform = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_AFTER_UPDATE, std::bind(&PhysicsSprite::afterUpdate, this, std::placeholders::_1));
|
|
|
|
_syncTransform->retain();
|
2014-04-23 17:11:55 +08:00
|
|
|
}
|
|
|
|
|
2015-08-01 16:50:16 +08:00
|
|
|
void PhysicsSprite::onExit()
|
|
|
|
{
|
|
|
|
if (_syncTransform != nullptr)
|
|
|
|
{
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_syncTransform);
|
|
|
|
_syncTransform->release();
|
|
|
|
}
|
|
|
|
Node::onExit();
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void PhysicsSprite::afterUpdate(EventCustom* /*event*/)
|
2014-04-23 17:11:55 +08:00
|
|
|
{
|
2015-06-08 13:43:51 +08:00
|
|
|
syncPhysicsTransform();
|
2014-04-23 17:11:55 +08:00
|
|
|
|
2015-06-08 13:43:51 +08:00
|
|
|
_transformDirty = false;
|
|
|
|
_transformUpdated = true;
|
|
|
|
setDirtyRecursively(true);
|
2013-07-02 02:52:04 +08:00
|
|
|
}
|
2012-11-12 15:22:26 +08:00
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
NS_CC_EXT_END
|
2014-09-19 00:35:01 +08:00
|
|
|
|
2014-09-22 13:25:47 +08:00
|
|
|
#endif // CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION
|