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()
|
|
|
|
: m_bIgnoreBodyRotation(false)
|
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
|
|
|
, m_pBody(NULL)
|
|
|
|
#elif CC_ENABLE_BOX2D_INTEGRATION
|
|
|
|
, m_pBody(NULL)
|
2012-11-20 16:17:20 +08:00
|
|
|
, m_fPTMRatio(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CC_ENABLE_CHIPMUNK_INTEGRATION
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
cpBody* CCPhysicsSprite::getBody() const
|
|
|
|
{
|
|
|
|
return m_pBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCPhysicsSprite::setBody(cpBody *pBody)
|
|
|
|
{
|
|
|
|
m_pBody = pBody;
|
|
|
|
}
|
|
|
|
|
2012-11-12 15:22:26 +08:00
|
|
|
// Override the setters and getters to always reflect the body's properties.
|
2012-11-20 16:17:20 +08:00
|
|
|
CCPoint CCPhysicsSprite::getPosition()
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2012-11-20 16:17:20 +08:00
|
|
|
cpVect cpPos = cpBodyGetPos(m_pBody);
|
|
|
|
return 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);
|
|
|
|
cpBodySetPos(m_pBody, 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
|
|
|
{
|
|
|
|
return (m_bIgnoreBodyRotation ? CCSprite::getRotation() : -CC_RADIANS_TO_DEGREES(cpBodyGetAngle(m_pBody)));
|
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
void CCPhysicsSprite::setRotation(float fRotation)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
|
|
|
if (m_bIgnoreBodyRotation)
|
|
|
|
{
|
|
|
|
CCSprite::setRotation(fRotation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cpBodySetAngle(m_pBody, -CC_DEGREES_TO_RADIANS(fRotation));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
|
|
|
cpVect rot = (m_bIgnoreBodyRotation ? cpvforangle(-CC_DEGREES_TO_RADIANS(m_fRotationX)) : m_pBody->rot);
|
2012-11-20 16:17:20 +08:00
|
|
|
float x = m_pBody->p.x + rot.x*(-m_obAnchorPointInPoints.x) - rot.y*(-m_obAnchorPointInPoints.y);
|
|
|
|
float y = m_pBody->p.y + rot.y*(-m_obAnchorPointInPoints.x) + rot.x*(-m_obAnchorPointInPoints.y);
|
2012-11-12 15:22:26 +08:00
|
|
|
|
|
|
|
if (m_bIgnoreAnchorPointForPosition)
|
|
|
|
{
|
2012-11-20 16:17:20 +08:00
|
|
|
x += m_obAnchorPointInPoints.x;
|
|
|
|
y += m_obAnchorPointInPoints.y;
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
return (m_sTransform = CCAffineTransformMake(rot.x, rot.y, -rot.y, rot.x, x, y));
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif CC_ENABLE_BOX2D_INTEGRATION
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
b2Body* CCPhysicsSprite::getBody() const
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
|
|
|
return m_pBody;
|
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
void CCPhysicsSprite::setBody(b2Body *pBody)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
|
|
|
m_pBody = pBody;
|
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
float CCPhysicsSprite::getPTMRatio() const
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
|
|
|
return m_fPTMRatio;
|
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
void CCPhysicsSprite::setPTMRatio(float fRatio)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
|
|
|
m_fPTMRatio = fRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override the setters and getters to always reflect the body's properties.
|
2012-11-20 16:17:20 +08:00
|
|
|
CCPoint CCPhysicsSprite::getPosition()
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
|
|
|
b2Vec2 pos = m_pBody->GetPosition();
|
|
|
|
|
|
|
|
float x = pos.x * m_fPTMRatio;
|
|
|
|
float y = pos.y * m_fPTMRatio;
|
|
|
|
return ccp(x,y);
|
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
void CCPhysicsSprite::setPosition(const CCPoint &pos)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
|
|
|
float angle = m_pBody->GetAngle();
|
2012-11-20 16:17:20 +08:00
|
|
|
m_pBody->SetTransform(b2Vec2(pos.x / m_fPTMRatio, pos.y / m_fPTMRatio), 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
|
|
|
{
|
|
|
|
return (m_bIgnoreBodyRotation ? CCSprite::getRotation() :
|
|
|
|
CC_RADIANS_TO_DEGREES(m_pBody->GetAngle()));
|
|
|
|
}
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
void CCPhysicsSprite::setRotation(float fRotation)
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
|
|
|
if (m_bIgnoreBodyRotation)
|
|
|
|
{
|
|
|
|
CCSprite::setRotation(fRotation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b2Vec2 p = m_pBody->GetPosition();
|
|
|
|
float radians = CC_DEGREES_TO_RADIANS(fRotation);
|
|
|
|
m_pBody->SetTransform(p, radians);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
|
|
|
b2Vec2 pos = m_pBody->GetPosition();
|
|
|
|
|
|
|
|
float x = pos.x * m_fPTMRatio;
|
|
|
|
float y = pos.y * m_fPTMRatio;
|
|
|
|
|
|
|
|
if (m_bIgnoreAnchorPointForPosition)
|
|
|
|
{
|
2012-11-20 16:17:20 +08:00
|
|
|
x += m_obAnchorPointInPoints.x;
|
|
|
|
y += m_obAnchorPointInPoints.y;
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make matrix
|
|
|
|
float radians = m_pBody->GetAngle();
|
|
|
|
float c = cosf(radians);
|
|
|
|
float s = sinf(radians);
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
if (! m_obAnchorPointInPoints.equals(CCPointZero))
|
2012-11-12 15:22:26 +08:00
|
|
|
{
|
2012-11-20 16:17:20 +08:00
|
|
|
x += c*(-m_obAnchorPointInPoints.x) + -s*(-m_obAnchorPointInPoints.y);
|
|
|
|
y += s*(-m_obAnchorPointInPoints.x) + c*(-m_obAnchorPointInPoints.y);
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rot, Transition Matrix
|
2012-11-20 16:17:20 +08:00
|
|
|
m_sTransform = CCAffineTransformMake(c, s,
|
2012-11-12 15:22:26 +08:00
|
|
|
-s, c,
|
|
|
|
x, y);
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
return m_sTransform;
|
2012-11-12 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-11-20 16:17:20 +08:00
|
|
|
NS_CC_EXT_END
|