2012-11-12 15:22:26 +08:00
|
|
|
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
|
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
#include "support/CCPointExtension.h"
|
|
|
|
|
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
|
|
|
#include "chipmunk.h"
|
|
|
|
#elif CC_ENABLE_BOX2D_INTEGRATION
|
2012-11-20 16:17:20 +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
|
|
|
|
|
|
|
CCPhysicsSprite::CCPhysicsSprite()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _ignoreBodyRotation(false)
|
2012-11-12 15:22:26 +08:00
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
2013-06-15 14:03:30 +08:00
|
|
|
, _CPBody(NULL)
|
2012-11-12 15:22:26 +08:00
|
|
|
#elif CC_ENABLE_BOX2D_INTEGRATION
|
2013-06-15 14:03:30 +08:00
|
|
|
, _pB2Body(NULL)
|
|
|
|
, _PTMRatio(0.0f)
|
2012-11-12 15:22:26 +08:00
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
|
|
|
CCPhysicsSprite* CCPhysicsSprite::create()
|
|
|
|
{
|
|
|
|
CCPhysicsSprite* pRet = new CCPhysicsSprite();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
CCPhysicsSprite* CCPhysicsSprite::createWithTexture(CCTexture2D *pTexture)
|
|
|
|
{
|
|
|
|
CCPhysicsSprite* pRet = new CCPhysicsSprite();
|
|
|
|
if (pRet && pRet->initWithTexture(pTexture))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCPhysicsSprite* CCPhysicsSprite::createWithTexture(CCTexture2D *pTexture, const CCRect& rect)
|
|
|
|
{
|
|
|
|
CCPhysicsSprite* pRet = new CCPhysicsSprite();
|
|
|
|
if (pRet && pRet->initWithTexture(pTexture, rect))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCPhysicsSprite* CCPhysicsSprite::createWithSpriteFrame(CCSpriteFrame *pSpriteFrame)
|
|
|
|
{
|
|
|
|
CCPhysicsSprite* pRet = new CCPhysicsSprite();
|
|
|
|
if (pRet && pRet->initWithSpriteFrame(pSpriteFrame))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCPhysicsSprite* CCPhysicsSprite::createWithSpriteFrameName(const char *pszSpriteFrameName)
|
|
|
|
{
|
|
|
|
CCPhysicsSprite* pRet = new CCPhysicsSprite();
|
|
|
|
if (pRet && pRet->initWithSpriteFrameName(pszSpriteFrameName))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCPhysicsSprite* CCPhysicsSprite::create(const char *pszFileName)
|
|
|
|
{
|
|
|
|
CCPhysicsSprite* pRet = new CCPhysicsSprite();
|
|
|
|
if (pRet && pRet->initWithFile(pszFileName))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCPhysicsSprite* CCPhysicsSprite::create(const char *pszFileName, const CCRect& rect)
|
|
|
|
{
|
|
|
|
CCPhysicsSprite* pRet = new CCPhysicsSprite();
|
|
|
|
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.
|
|
|
|
// If you return NO, then nodeToParentTransform won't be called.
|
|
|
|
bool CCPhysicsSprite::isDirty()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-22 09:49:37 +08:00
|
|
|
bool CCPhysicsSprite::isIgnoreBodyRotation() const
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _ignoreBodyRotation;
|
2012-11-22 09:49:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCPhysicsSprite::setIgnoreBodyRotation(bool bIgnoreBodyRotation)
|
|
|
|
{
|
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.
|
|
|
|
const CCPoint& CCPhysicsSprite::getPosition()
|
|
|
|
{
|
|
|
|
updatePosFromPhysics();
|
|
|
|
return CCNode::getPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCPhysicsSprite::getPosition(float* x, float* y)
|
|
|
|
{
|
|
|
|
updatePosFromPhysics();
|
|
|
|
return CCNode::getPosition(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCPhysicsSprite::getPositionX()
|
|
|
|
{
|
|
|
|
updatePosFromPhysics();
|
2013-06-15 14:03:30 +08:00
|
|
|
return _position.x;
|
2013-03-20 08:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float CCPhysicsSprite::getPositionY()
|
|
|
|
{
|
|
|
|
updatePosFromPhysics();
|
2013-06-15 14:03:30 +08:00
|
|
|
return _position.y;
|
2013-03-20 08:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-11-12 15:22:26 +08:00
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
|
|
|
|
2013-01-05 15:09:19 +08:00
|
|
|
cpBody* CCPhysicsSprite::getCPBody() const
|
2012-11-20 16:17:20 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _CPBody;
|
2012-11-20 16:17:20 +08:00
|
|
|
}
|
|
|
|
|
2013-01-05 15:09:19 +08:00
|
|
|
void CCPhysicsSprite::setCPBody(cpBody *pBody)
|
2012-11-20 16:17:20 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_CPBody = pBody;
|
2012-11-20 16:17:20 +08:00
|
|
|
}
|
|
|
|
|
2013-03-20 08:35:52 +08:00
|
|
|
void CCPhysicsSprite::updatePosFromPhysics()
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
cpVect cpPos = cpBodyGetPos(_CPBody);
|
|
|
|
_position = ccp(cpPos.x, cpPos.y);
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
void CCPhysicsSprite::setPosition(const CCPoint &pos)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2012-11-20 16:17:20 +08:00
|
|
|
cpVect cpPos = cpv(pos.x, pos.y);
|
2013-06-15 14:03:30 +08:00
|
|
|
cpBodySetPos(_CPBody, cpPos);
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
float CCPhysicsSprite::getRotation()
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return (_ignoreBodyRotation ? CCSprite::getRotation() : -CC_RADIANS_TO_DEGREES(cpBodyGetAngle(_CPBody)));
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
void CCPhysicsSprite::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
|
|
|
{
|
|
|
|
CCSprite::setRotation(fRotation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
cpBodySetAngle(_CPBody, -CC_DEGREES_TO_RADIANS(fRotation));
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the transform matrix according the Chipmunk Body values
|
2012-11-20 16:17:20 +08:00
|
|
|
CCAffineTransform CCPhysicsSprite::nodeToParentTransform()
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-02-27 16:12:28 +08:00
|
|
|
// Although scale is not used by physics engines, it is calculated just in case
|
|
|
|
// the sprite is animated (scaled up/down) using actions.
|
|
|
|
// For more info see: http://www.cocos2d-iphone.org/forum/topic/68990
|
2013-06-15 14:03:30 +08:00
|
|
|
cpVect rot = (_ignoreBodyRotation ? cpvforangle(-CC_DEGREES_TO_RADIANS(_rotationX)) : _CPBody->rot);
|
|
|
|
float x = _CPBody->p.x + rot.x * -_anchorPointInPoints.x * _scaleX - rot.y * -_anchorPointInPoints.y * _scaleY;
|
|
|
|
float y = _CPBody->p.y + rot.y * -_anchorPointInPoints.x * _scaleX + rot.x * -_anchorPointInPoints.y * _scaleY;
|
2013-02-27 16:12:28 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_ignoreAnchorPointForPosition)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
x += _anchorPointInPoints.x;
|
|
|
|
y += _anchorPointInPoints.y;
|
2013-02-27 16:12:28 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
return (_transform = CCAffineTransformMake(rot.x * _scaleX, rot.y * _scaleX,
|
|
|
|
-rot.y * _scaleY, rot.x * _scaleY,
|
2013-02-27 16:12:28 +08:00
|
|
|
x, y));
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif CC_ENABLE_BOX2D_INTEGRATION
|
|
|
|
|
2013-01-05 15:09:19 +08:00
|
|
|
b2Body* CCPhysicsSprite::getB2Body() const
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _pB2Body;
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2013-01-05 15:09:19 +08:00
|
|
|
void CCPhysicsSprite::setB2Body(b2Body *pBody)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_pB2Body = pBody;
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
float CCPhysicsSprite::getPTMRatio() const
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _PTMRatio;
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
void CCPhysicsSprite::setPTMRatio(float fRatio)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_PTMRatio = fRatio;
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Override the setters and getters to always reflect the body's properties.
|
2013-03-20 08:35:52 +08:00
|
|
|
void CCPhysicsSprite::updatePosFromPhysics()
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
b2Vec2 pos = _pB2Body->GetPosition();
|
|
|
|
float x = pos.x * _PTMRatio;
|
|
|
|
float y = pos.y * _PTMRatio;
|
|
|
|
_position = ccp(x,y);
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
void CCPhysicsSprite::setPosition(const CCPoint &pos)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
float angle = _pB2Body->GetAngle();
|
|
|
|
_pB2Body->SetTransform(b2Vec2(pos.x / _PTMRatio, pos.y / _PTMRatio), angle);
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
float CCPhysicsSprite::getRotation()
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return (_ignoreBodyRotation ? CCSprite::getRotation() :
|
|
|
|
CC_RADIANS_TO_DEGREES(_pB2Body->GetAngle()));
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
void CCPhysicsSprite::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
|
|
|
{
|
|
|
|
CCSprite::setRotation(fRotation);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the transform matrix according the Box2D Body values
|
2012-11-20 16:17:20 +08:00
|
|
|
CCAffineTransform CCPhysicsSprite::nodeToParentTransform()
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
b2Vec2 pos = _pB2Body->GetPosition();
|
2013-02-27 16:12:28 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float x = pos.x * _PTMRatio;
|
|
|
|
float y = pos.y * _PTMRatio;
|
2013-02-27 16:04:03 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_ignoreAnchorPointForPosition)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
x += _anchorPointInPoints.x;
|
|
|
|
y += _anchorPointInPoints.y;
|
2013-02-27 16:04:03 +08:00
|
|
|
}
|
|
|
|
|
2013-02-27 16:12:28 +08:00
|
|
|
// Make matrix
|
2013-06-15 14:03:30 +08:00
|
|
|
float radians = _pB2Body->GetAngle();
|
2013-02-27 16:12:28 +08:00
|
|
|
float c = cosf(radians);
|
|
|
|
float s = sinf(radians);
|
|
|
|
|
|
|
|
// Although scale is not used by physics engines, it is calculated just in case
|
|
|
|
// the sprite is animated (scaled up/down) using actions.
|
|
|
|
// For more info see: http://www.cocos2d-iphone.org/forum/topic/68990
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_anchorPointInPoints.equals(CCPointZero))
|
2013-02-27 16:12:28 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
x += ((c * -_anchorPointInPoints.x * _scaleX) + (-s * -_anchorPointInPoints.y * _scaleY));
|
|
|
|
y += ((s * -_anchorPointInPoints.x * _scaleX) + (c * -_anchorPointInPoints.y * _scaleY));
|
2013-02-27 16:12:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rot, Translate Matrix
|
2013-06-15 14:03:30 +08:00
|
|
|
_transform = CCAffineTransformMake( c * _scaleX, s * _scaleX,
|
|
|
|
-s * _scaleY, c * _scaleY,
|
2013-02-27 16:12:28 +08:00
|
|
|
x, y );
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
return _transform;
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
NS_CC_EXT_END
|