Last active
August 29, 2015 13:56
-
-
Save chucai/9304927 to your computer and use it in GitHub Desktop.
COCOS2DX:B2Sprite extension Sprite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 "B2Sprite.h" | |
#include "support/CCPointExtension.h" | |
#include "Box2D/Box2D.h" | |
B2Sprite::B2Sprite() | |
: m_bIgnoreBodyRotation(false) | |
, m_pB2Body(NULL) | |
, m_fPTMRatio(0.0f) | |
{} | |
B2Sprite* B2Sprite::create() | |
{ | |
B2Sprite* pRet = new B2Sprite(); | |
if (pRet && pRet->init()) | |
{ | |
pRet->autorelease(); | |
} | |
else | |
{ | |
CC_SAFE_DELETE(pRet); | |
} | |
return pRet; | |
} | |
B2Sprite* B2Sprite::createWithTexture(CCTexture2D *pTexture) | |
{ | |
B2Sprite* pRet = new B2Sprite(); | |
if (pRet && pRet->initWithTexture(pTexture)) | |
{ | |
pRet->autorelease(); | |
} | |
else | |
{ | |
CC_SAFE_DELETE(pRet); | |
} | |
return pRet; | |
} | |
B2Sprite* B2Sprite::createWithTexture(CCTexture2D *pTexture, const CCRect& rect) | |
{ | |
B2Sprite* pRet = new B2Sprite(); | |
if (pRet && pRet->initWithTexture(pTexture, rect)) | |
{ | |
pRet->autorelease(); | |
} | |
else | |
{ | |
CC_SAFE_DELETE(pRet); | |
} | |
return pRet; | |
} | |
B2Sprite* B2Sprite::createWithSpriteFrame(CCSpriteFrame *pSpriteFrame) | |
{ | |
B2Sprite* pRet = new B2Sprite(); | |
if (pRet && pRet->initWithSpriteFrame(pSpriteFrame)) | |
{ | |
pRet->autorelease(); | |
} | |
else | |
{ | |
CC_SAFE_DELETE(pRet); | |
} | |
return pRet; | |
} | |
B2Sprite* B2Sprite::createWithSpriteFrameName(const char *pszSpriteFrameName) | |
{ | |
B2Sprite* pRet = new B2Sprite(); | |
if (pRet && pRet->initWithSpriteFrameName(pszSpriteFrameName)) | |
{ | |
pRet->autorelease(); | |
} | |
else | |
{ | |
CC_SAFE_DELETE(pRet); | |
} | |
return pRet; | |
} | |
B2Sprite* B2Sprite::create(const char *pszFileName) | |
{ | |
B2Sprite* pRet = new B2Sprite(); | |
if (pRet && pRet->initWithFile(pszFileName)) | |
{ | |
pRet->autorelease(); | |
} | |
else | |
{ | |
CC_SAFE_DELETE(pRet); | |
} | |
return pRet; | |
} | |
B2Sprite* B2Sprite::create(const char *pszFileName, const CCRect& rect) | |
{ | |
B2Sprite* pRet = new B2Sprite(); | |
if (pRet && pRet->initWithFile(pszFileName, rect)) | |
{ | |
pRet->autorelease(); | |
} | |
else | |
{ | |
CC_SAFE_DELETE(pRet); | |
} | |
return pRet; | |
} | |
// 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 B2Sprite::isDirty() | |
{ | |
return true; | |
} | |
bool B2Sprite::isIgnoreBodyRotation() const | |
{ | |
return m_bIgnoreBodyRotation; | |
} | |
void B2Sprite::setIgnoreBodyRotation(bool bIgnoreBodyRotation) | |
{ | |
m_bIgnoreBodyRotation = bIgnoreBodyRotation; | |
} | |
// Override the setters and getters to always reflect the body's properties. | |
const CCPoint& B2Sprite::getPosition() | |
{ | |
updatePosFromPhysics(); | |
return CCNode::getPosition(); | |
} | |
void B2Sprite::getPosition(float* x, float* y) | |
{ | |
updatePosFromPhysics(); | |
return CCNode::getPosition(x, y); | |
} | |
float B2Sprite::getPositionX() | |
{ | |
updatePosFromPhysics(); | |
return m_obPosition.x; | |
} | |
float B2Sprite::getPositionY() | |
{ | |
updatePosFromPhysics(); | |
return m_obPosition.y; | |
} | |
b2Body* B2Sprite::getB2Body() const | |
{ | |
return m_pB2Body; | |
} | |
void B2Sprite::setB2Body(b2Body *pBody) | |
{ | |
m_pB2Body = pBody; | |
pBody->SetUserData(this); | |
} | |
float B2Sprite::getPTMRatio() const | |
{ | |
return m_fPTMRatio; | |
} | |
void B2Sprite::setPTMRatio(float fRatio) | |
{ | |
m_fPTMRatio = fRatio; | |
} | |
// Override the setters and getters to always reflect the body's properties. | |
void B2Sprite::updatePosFromPhysics() | |
{ | |
b2Vec2 pos = m_pB2Body->GetPosition(); | |
float x = pos.x * m_fPTMRatio; | |
float y = pos.y * m_fPTMRatio; | |
m_obPosition = ccp(x,y); | |
} | |
void B2Sprite::setPosition(const CCPoint &pos) | |
{ | |
float angle = m_pB2Body->GetAngle(); | |
m_pB2Body->SetTransform(b2Vec2(pos.x / m_fPTMRatio, pos.y / m_fPTMRatio), angle); | |
} | |
float B2Sprite::getRotation() | |
{ | |
return (m_bIgnoreBodyRotation ? CCSprite::getRotation() : | |
CC_RADIANS_TO_DEGREES(m_pB2Body->GetAngle())); | |
} | |
void B2Sprite::setRotation(float fRotation) | |
{ | |
if (m_bIgnoreBodyRotation) | |
{ | |
CCSprite::setRotation(fRotation); | |
} | |
else | |
{ | |
b2Vec2 p = m_pB2Body->GetPosition(); | |
float radians = CC_DEGREES_TO_RADIANS(fRotation); | |
m_pB2Body->SetTransform(p, radians); | |
} | |
} | |
// returns the transform matrix according the Box2D Body values | |
CCAffineTransform B2Sprite::nodeToParentTransform() | |
{ | |
b2Vec2 pos = m_pB2Body->GetPosition(); | |
float x = pos.x * m_fPTMRatio; | |
float y = pos.y * m_fPTMRatio; | |
if (m_bIgnoreAnchorPointForPosition) | |
{ | |
x += m_obAnchorPointInPoints.x; | |
y += m_obAnchorPointInPoints.y; | |
} | |
// Make matrix | |
float radians = m_pB2Body->GetAngle(); | |
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 | |
if (!m_obAnchorPointInPoints.equals(CCPointZero)) | |
{ | |
x += ((c * -m_obAnchorPointInPoints.x * m_fScaleX) + (-s * -m_obAnchorPointInPoints.y * m_fScaleY)); | |
y += ((s * -m_obAnchorPointInPoints.x * m_fScaleX) + (c * -m_obAnchorPointInPoints.y * m_fScaleY)); | |
} | |
// Rot, Translate Matrix | |
m_sTransform = CCAffineTransformMake( c * m_fScaleX, s * m_fScaleX, | |
-s * m_fScaleY, c * m_fScaleY, | |
x, y ); | |
return m_sTransform; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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. | |
*/ | |
#ifndef __PHYSICSNODES_CCPHYSICSSPRITE_H__ | |
#define __PHYSICSNODES_CCPHYSICSSPRITE_H__ | |
#include "cocos2d.h" | |
#include "ExtensionMacros.h" | |
USING_NS_CC; | |
class b2Body; | |
/** A CCSprite subclass that is bound to a physics body. | |
It works with: | |
- Chipmunk: Preprocessor macro CC_ENABLE_CHIPMUNK_INTEGRATION should be defined | |
- Objective-Chipmunk: Preprocessor macro CC_ENABLE_CHIPMUNK_INTEGRATION should be defined | |
- Box2d: Preprocessor macro CC_ENABLE_BOX2D_INTEGRATION should be defined | |
Features and Limitations: | |
- Scale and Skew properties are ignored. | |
- Position and rotation are going to updated from the physics body | |
- If you update the rotation or position manually, the physics body will be updated | |
- You can't enble both Chipmunk support and Box2d support at the same time. Only one can be enabled at compile time | |
* @js NA | |
* @lua NA | |
*/ | |
class B2Sprite : public CCSprite | |
{ | |
protected: | |
bool m_bIgnoreBodyRotation; | |
b2Body *m_pB2Body; | |
// Pixels to Meters ratio | |
float m_fPTMRatio; | |
public: | |
B2Sprite(); | |
static B2Sprite* create(); | |
/** Creates an sprite with a texture. | |
The rect used will be the size of the texture. | |
The offset will be (0,0). | |
*/ | |
static B2Sprite* createWithTexture(CCTexture2D *pTexture); | |
/** Creates an sprite with a texture and a rect. | |
The offset will be (0,0). | |
*/ | |
static B2Sprite* createWithTexture(CCTexture2D *pTexture, const CCRect& rect); | |
/** Creates an sprite with an sprite frame. */ | |
static B2Sprite* createWithSpriteFrame(CCSpriteFrame *pSpriteFrame); | |
/** Creates an sprite with an sprite frame name. | |
An CCSpriteFrame will be fetched from the CCSpriteFrameCache by name. | |
If the CCSpriteFrame doesn't exist it will raise an exception. | |
@since v0.9 | |
*/ | |
static B2Sprite* createWithSpriteFrameName(const char *pszSpriteFrameName); | |
/** Creates an sprite with an image filename. | |
The rect used will be the size of the image. | |
The offset will be (0,0). | |
*/ | |
static B2Sprite* create(const char *pszFileName); | |
/** Creates an sprite with an image filename and a rect. | |
The offset will be (0,0). | |
*/ | |
static B2Sprite* create(const char *pszFileName, const CCRect& rect); | |
virtual bool isDirty(); | |
/** Keep the sprite's rotation separate from the body. */ | |
bool isIgnoreBodyRotation() const; | |
void setIgnoreBodyRotation(bool bIgnoreBodyRotation); | |
virtual const CCPoint& getPosition(); | |
virtual void getPosition(float* x, float* y); | |
virtual float getPositionX(); | |
virtual float getPositionY(); | |
virtual void setPosition(const CCPoint &position); | |
virtual float getRotation(); | |
virtual void setRotation(float fRotation); | |
virtual CCAffineTransform nodeToParentTransform(); | |
/** Body accessor when using box2d */ | |
b2Body* getB2Body() const; | |
void setB2Body(b2Body *pBody); | |
float getPTMRatio() const; | |
void setPTMRatio(float fPTMRatio); | |
protected: | |
void updatePosFromPhysics(); | |
}; | |
#endif // __PHYSICSNODES_CCPHYSICSSPRITE_H__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment