axmol/extensions/physics_nodes/CCPhysicsSprite.cpp

309 lines
7.8 KiB
C++
Raw Normal View History

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
#include "Box2D/Box2D.h"
2012-11-12 15:22:26 +08:00
#endif
NS_CC_EXT_BEGIN
2012-11-12 15:22:26 +08:00
CCPhysicsSprite::CCPhysicsSprite()
: m_bIgnoreBodyRotation(false)
#if CC_ENABLE_CHIPMUNK_INTEGRATION
, m_pCPBody(NULL)
2012-11-12 15:22:26 +08:00
#elif CC_ENABLE_BOX2D_INTEGRATION
, m_pB2Body(NULL)
, 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;
}
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;
}
bool CCPhysicsSprite::isIgnoreBodyRotation() const
{
return m_bIgnoreBodyRotation;
}
void CCPhysicsSprite::setIgnoreBodyRotation(bool bIgnoreBodyRotation)
{
m_bIgnoreBodyRotation = bIgnoreBodyRotation;
}
2012-11-12 15:22:26 +08:00
#if CC_ENABLE_CHIPMUNK_INTEGRATION
cpBody* CCPhysicsSprite::getCPBody() const
{
return m_pCPBody;
}
void CCPhysicsSprite::setCPBody(cpBody *pBody)
{
m_pCPBody = pBody;
}
2012-11-12 15:22:26 +08:00
// Override the setters and getters to always reflect the body's properties.
const CCPoint& CCPhysicsSprite::getPosition()
2012-11-12 15:22:26 +08:00
{
cpVect cpPos = cpBodyGetPos(m_pCPBody);
m_obPosition = ccp(cpPos.x, cpPos.y);
return m_obPosition;
2012-11-12 15:22:26 +08:00
}
void CCPhysicsSprite::setPosition(const CCPoint &pos)
2012-11-12 15:22:26 +08:00
{
cpVect cpPos = cpv(pos.x, pos.y);
cpBodySetPos(m_pCPBody, cpPos);
2012-11-12 15:22:26 +08:00
}
float CCPhysicsSprite::getRotation()
2012-11-12 15:22:26 +08:00
{
return (m_bIgnoreBodyRotation ? CCSprite::getRotation() : -CC_RADIANS_TO_DEGREES(cpBodyGetAngle(m_pCPBody)));
2012-11-12 15:22:26 +08:00
}
void CCPhysicsSprite::setRotation(float fRotation)
2012-11-12 15:22:26 +08:00
{
if (m_bIgnoreBodyRotation)
{
CCSprite::setRotation(fRotation);
}
else
{
cpBodySetAngle(m_pCPBody, -CC_DEGREES_TO_RADIANS(fRotation));
2012-11-12 15:22:26 +08:00
}
}
// returns the transform matrix according the Chipmunk Body values
CCAffineTransform CCPhysicsSprite::nodeToParentTransform()
2012-11-12 15:22:26 +08:00
{
cpVect rot = (m_bIgnoreBodyRotation ? cpvforangle(-CC_DEGREES_TO_RADIANS(m_fRotationX)) : m_pCPBody->rot);
float x = m_pCPBody->p.x + rot.x*(-m_obAnchorPointInPoints.x) - rot.y*(-m_obAnchorPointInPoints.y);
float y = m_pCPBody->p.y + rot.y*(-m_obAnchorPointInPoints.x) + rot.x*(-m_obAnchorPointInPoints.y);
2012-11-12 15:22:26 +08:00
if (m_bIgnoreAnchorPointForPosition)
{
x += m_obAnchorPointInPoints.x;
y += m_obAnchorPointInPoints.y;
2012-11-12 15:22:26 +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
b2Body* CCPhysicsSprite::getB2Body() const
2012-11-12 15:22:26 +08:00
{
return m_pB2Body;
2012-11-12 15:22:26 +08:00
}
void CCPhysicsSprite::setB2Body(b2Body *pBody)
2012-11-12 15:22:26 +08:00
{
m_pB2Body = pBody;
2012-11-12 15:22:26 +08:00
}
float CCPhysicsSprite::getPTMRatio() const
2012-11-12 15:22:26 +08:00
{
return m_fPTMRatio;
}
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.
const CCPoint& CCPhysicsSprite::getPosition()
2012-11-12 15:22:26 +08:00
{
b2Vec2 pos = m_pB2Body->GetPosition();
2012-11-12 15:22:26 +08:00
float x = pos.x * m_fPTMRatio;
float y = pos.y * m_fPTMRatio;
m_obPosition = ccp(x,y);
return m_obPosition;
2012-11-12 15:22:26 +08:00
}
void CCPhysicsSprite::setPosition(const CCPoint &pos)
2012-11-12 15:22:26 +08:00
{
float angle = m_pB2Body->GetAngle();
m_pB2Body->SetTransform(b2Vec2(pos.x / m_fPTMRatio, pos.y / m_fPTMRatio), angle);
2012-11-12 15:22:26 +08:00
}
float CCPhysicsSprite::getRotation()
2012-11-12 15:22:26 +08:00
{
return (m_bIgnoreBodyRotation ? CCSprite::getRotation() :
CC_RADIANS_TO_DEGREES(m_pB2Body->GetAngle()));
2012-11-12 15:22:26 +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_pB2Body->GetPosition();
2012-11-12 15:22:26 +08:00
float radians = CC_DEGREES_TO_RADIANS(fRotation);
m_pB2Body->SetTransform(p, radians);
2012-11-12 15:22:26 +08:00
}
}
// returns the transform matrix according the Box2D Body values
CCAffineTransform CCPhysicsSprite::nodeToParentTransform()
2012-11-12 15:22:26 +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
cpVect rot = (m_bIgnoreBodyRotation ? cpvforangle(-CC_DEGREES_TO_RADIANS(m_fRotationX)) : m_pCPBody->rot);
float x = m_pCPBody->p.x + rot.x * -m_obAnchorPointInPoints.x * m_fScaleX - rot.y * -m_obAnchorPointInPoints.y * m_fScaleY;
float y = m_pCPBody->p.y + rot.y * -m_obAnchorPointInPoints.x * m_fScaleX + rot.x * -m_obAnchorPointInPoints.y * m_fScaleY;
if (m_bIgnoreAnchorPointForPosition)
2012-11-12 15:22:26 +08:00
{
x += m_obAnchorPointInPoints.x;
y += m_obAnchorPointInPoints.y;
}
return (m_sTransform = CCAffineTransformMake(rot.x * m_fScaleX, rot.y * m_fScaleX,
-rot.y * m_fScaleY, rot.x * m_fScaleY,
x, y));
2012-11-12 15:22:26 +08:00
}
#endif
NS_CC_EXT_END