Merge pull request #2231 from sbc100/physics_fixes

fixed #2055: Physics fixes.
1) Fix CCPhysicsSprite getPosition variants …
CCPhysicsSprite was overriding only some of the getPosition
methods on a node resulting in, for example, getPositionX
and getPositionY not working for physics nodes.

This change also makes this overloads shared between box2d
and chipmunk implementations.

2) Fix typo in make-all-linux-project.sh
3) The ENABLE_BOX2D/ENABLE_CHIPMUNK defines were previously
hardcoded in the Makefiles. Now you get chipmunk by default
on both platforms and can enble Box2D by setting USE_BOX2D in
your environment.

Also remove erroneous -D__CC_PLATFORM_FILEUTILS_CPP__ and
-D__CC_PLATFORM_IMAGE_CPP__ from linux Makefiles. These should
never be defined globally like this.
This commit is contained in:
James Chen 2013-04-15 08:06:15 -07:00
commit bf4344a53f
12 changed files with 73 additions and 61 deletions

View File

@ -308,21 +308,21 @@ public:
* @param x X coordinate for position * @param x X coordinate for position
* @param y Y coordinate for position * @param y Y coordinate for position
*/ */
void setPosition(float x, float y); virtual void setPosition(float x, float y);
/** /**
* Gets position in a more efficient way, returns two number instead of a CCPoint object * Gets position in a more efficient way, returns two number instead of a CCPoint object
* *
* @see setPosition(float, float) * @see setPosition(float, float)
*/ */
void getPosition(float* x, float* y); virtual void getPosition(float* x, float* y);
/** /**
* Gets/Sets x or y coordinate individually for position. * Gets/Sets x or y coordinate individually for position.
* These methods are used in Lua and Javascript Bindings * These methods are used in Lua and Javascript Bindings
*/ */
void setPositionX(float x); virtual void setPositionX(float x);
float getPositionX(void); virtual float getPositionX(void);
void setPositionY(float y); virtual void setPositionY(float y);
float getPositionY(void); virtual float getPositionY(void);
/** /**

View File

@ -9,9 +9,6 @@ INCLUDES += \
-I../../external/chipmunk/include/chipmunk \ -I../../external/chipmunk/include/chipmunk \
-I../../extensions/network \ -I../../extensions/network \
DEFINES += -D__CC_PLATFORM_FILEUTILS_CPP__
DEFINES += -DCC_ENABLE_CHIPMUNK_INTEGRATION
SOURCES = ../actions/CCAction.cpp \ SOURCES = ../actions/CCAction.cpp \
../actions/CCActionCamera.cpp \ ../actions/CCActionCamera.cpp \
../actions/CCActionEase.cpp \ ../actions/CCActionEase.cpp \

View File

@ -8,6 +8,12 @@ ARFLAGS = cr
DEFINES += -DLINUX DEFINES += -DLINUX
ifdef USE_BOX2D
DEFINES += -DCC_ENABLE_BOX2D_INTEGRATION
else
DEFINES += -DCC_ENABLE_CHIPMUNK_INTEGRATION
endif
THIS_MAKEFILE := $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) THIS_MAKEFILE := $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
ifndef COCOS_ROOT ifndef COCOS_ROOT
COCOS_ROOT := $(realpath $(dir $(THIS_MAKEFILE))/../..) COCOS_ROOT := $(realpath $(dir $(THIS_MAKEFILE))/../..)

View File

@ -137,8 +137,6 @@ CXXFLAGS += -Wno-sequence-point
TARGET = $(LIB_DIR)/libcocos2d.a TARGET = $(LIB_DIR)/libcocos2d.a
DEFINES += -DCC_ENABLE_CHIPMUNK_INTEGRATION
all: $(TARGET) all: $(TARGET)
$(TARGET): $(OBJECTS) $(CORE_MAKEFILE_LIST) $(TARGET): $(OBJECTS) $(CORE_MAKEFILE_LIST)

View File

@ -18,13 +18,15 @@ THIS_MAKEFILE := $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
# The top level of the cocos2dx-x source tree. The parent Makefile will # The top level of the cocos2dx-x source tree. The parent Makefile will
# often define this, but in case is doesn't we can find it relative to # often define this, but in case is doesn't we can find it relative to
# THIS_MAKEFILE # THIS_MAKEFILE
COCOS_ROOT ?= $(realpath $(dir $(THIS_MAKEFILE))/../..) ifndef COCOS_ROOT
COCOS_SRC = $(COCOS_ROOT)/cocos2dx COCOS_ROOT := $(realpath $(dir $(THIS_MAKEFILE))/../..)
endif
COCOS_SRC := $(COCOS_ROOT)/cocos2dx
ifeq ($(NACL_ARCH), i686) ifeq ($(NACL_ARCH), i686)
ARCH_DIR=$(NACL_LIBC)_x86_32 ARCH_DIR := $(NACL_LIBC)_x86_32
else else
ARCH_DIR=$(NACL_LIBC)_$(NACL_ARCH) ARCH_DIR := $(NACL_LIBC)_$(NACL_ARCH)
endif endif
NACLPORTS_ROOT ?= $(NACL_SDK_ROOT)/ports NACLPORTS_ROOT ?= $(NACL_SDK_ROOT)/ports
@ -33,6 +35,12 @@ OUT_DIR ?= obj
OBJ_DIR ?= $(OUT_DIR)/$(NACL_ARCH) OBJ_DIR ?= $(OUT_DIR)/$(NACL_ARCH)
LIB_DIR ?= $(COCOS_ROOT)/lib/nacl/$(ARCH_DIR) LIB_DIR ?= $(COCOS_ROOT)/lib/nacl/$(ARCH_DIR)
ifdef USE_BOX2D
DEFINES += -DCC_ENABLE_BOX2D_INTEGRATION
else
DEFINES += -DCC_ENABLE_CHIPMUNK_INTEGRATION
endif
INCLUDES += -I$(COCOS_SRC) \ INCLUDES += -I$(COCOS_SRC) \
-I$(COCOS_SRC)/cocoa \ -I$(COCOS_SRC)/cocoa \
-I$(COCOS_SRC)/include \ -I$(COCOS_SRC)/include \

View File

@ -42,16 +42,9 @@
#include "network/HttpClient.h" #include "network/HttpClient.h"
// Physics integration // Physics integration
#if CC_ENABLE_CHIPMUNK_INTEGRATION #if CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION
#include "physics_nodes/CCPhysicsDebugNode.h" #include "physics_nodes/CCPhysicsDebugNode.h"
#include "physics_nodes/CCPhysicsSprite.h" #include "physics_nodes/CCPhysicsSprite.h"
#endif #endif
#if CC_ENABLE_BOX2D_INTEGRATION
#include "physics_nodes/CCPhysicsDebugNode.h"
#include "physics_nodes/CCPhysicsSprite.h"
#endif
#endif /* __COCOS2D_EXT_H__ */ #endif /* __COCOS2D_EXT_H__ */

View File

@ -164,6 +164,31 @@ void CCPhysicsSprite::setIgnoreBodyRotation(bool bIgnoreBodyRotation)
m_bIgnoreBodyRotation = bIgnoreBodyRotation; m_bIgnoreBodyRotation = bIgnoreBodyRotation;
} }
// 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();
return m_obPosition.x;
}
float CCPhysicsSprite::getPositionY()
{
updatePosFromPhysics();
return m_obPosition.y;
}
#if CC_ENABLE_CHIPMUNK_INTEGRATION #if CC_ENABLE_CHIPMUNK_INTEGRATION
cpBody* CCPhysicsSprite::getCPBody() const cpBody* CCPhysicsSprite::getCPBody() const
@ -176,12 +201,10 @@ void CCPhysicsSprite::setCPBody(cpBody *pBody)
m_pCPBody = pBody; m_pCPBody = pBody;
} }
// Override the setters and getters to always reflect the body's properties. void CCPhysicsSprite::updatePosFromPhysics()
const CCPoint& CCPhysicsSprite::getPosition()
{ {
cpVect cpPos = cpBodyGetPos(m_pCPBody); cpVect cpPos = cpBodyGetPos(m_pCPBody);
m_obPosition = ccp(cpPos.x, cpPos.y); m_obPosition = ccp(cpPos.x, cpPos.y);
return m_obPosition;
} }
void CCPhysicsSprite::setPosition(const CCPoint &pos) void CCPhysicsSprite::setPosition(const CCPoint &pos)
@ -251,14 +274,12 @@ void CCPhysicsSprite::setPTMRatio(float fRatio)
} }
// Override the setters and getters to always reflect the body's properties. // Override the setters and getters to always reflect the body's properties.
const CCPoint& CCPhysicsSprite::getPosition() void CCPhysicsSprite::updatePosFromPhysics()
{ {
b2Vec2 pos = m_pB2Body->GetPosition(); b2Vec2 pos = m_pB2Body->GetPosition();
float x = pos.x * m_fPTMRatio; float x = pos.x * m_fPTMRatio;
float y = pos.y * m_fPTMRatio; float y = pos.y * m_fPTMRatio;
m_obPosition = ccp(x,y); m_obPosition = ccp(x,y);
return m_obPosition;
} }
void CCPhysicsSprite::setPosition(const CCPoint &pos) void CCPhysicsSprite::setPosition(const CCPoint &pos)

View File

@ -27,10 +27,11 @@
#if CC_ENABLE_CHIPMUNK_INTEGRATION #if CC_ENABLE_CHIPMUNK_INTEGRATION
#include "chipmunk.h" #include "chipmunk.h"
#elif CC_ENABLE_BOX2D_INTEGRATION #elif CC_ENABLE_BOX2D_INTEGRATION
class b2Body; class b2Body;
#endif // CC_ENABLE_BOX2D_INTEGRATION #else // CC_ENABLE_BOX2D_INTEGRATION
#error "You must define either CC_ENABLE_CHIPMUNK_INTEGRATION or CC_ENABLE_BOX2D_INTEGRATION to use CCPhysicsSprite.h"
#endif
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN
/** A CCSprite subclass that is bound to a physics body. /** A CCSprite subclass that is bound to a physics body.
@ -100,35 +101,30 @@ public:
bool isIgnoreBodyRotation() const; bool isIgnoreBodyRotation() const;
void setIgnoreBodyRotation(bool bIgnoreBodyRotation); void setIgnoreBodyRotation(bool bIgnoreBodyRotation);
#if CC_ENABLE_CHIPMUNK_INTEGRATION
virtual const CCPoint& getPosition(); virtual const CCPoint& getPosition();
virtual void getPosition(float* x, float* y);
virtual float getPositionX();
virtual float getPositionY();
virtual void setPosition(const CCPoint &position); virtual void setPosition(const CCPoint &position);
virtual float getRotation(); virtual float getRotation();
virtual void setRotation(float fRotation); virtual void setRotation(float fRotation);
virtual CCAffineTransform nodeToParentTransform(); virtual CCAffineTransform nodeToParentTransform();
#if CC_ENABLE_CHIPMUNK_INTEGRATION
/** Body accessor when using regular Chipmunk */ /** Body accessor when using regular Chipmunk */
cpBody* getCPBody() const; cpBody* getCPBody() const;
void setCPBody(cpBody *pBody); void setCPBody(cpBody *pBody);
#elif CC_ENABLE_BOX2D_INTEGRATION #elif CC_ENABLE_BOX2D_INTEGRATION
virtual const CCPoint& getPosition();
virtual void setPosition(const CCPoint &position);
virtual float getRotation();
virtual void setRotation(float fRotation);
virtual CCAffineTransform nodeToParentTransform();
/** Body accessor when using box2d */ /** Body accessor when using box2d */
b2Body* getB2Body() const; b2Body* getB2Body() const;
void setB2Body(b2Body *pBody); void setB2Body(b2Body *pBody);
float getPTMRatio() const; float getPTMRatio() const;
void setPTMRatio(float fPTMRatio); void setPTMRatio(float fPTMRatio);
#endif // CC_ENABLE_BOX2D_INTEGRATION #endif // CC_ENABLE_BOX2D_INTEGRATION
protected:
void updatePosFromPhysics();
}; };
NS_CC_EXT_END NS_CC_EXT_END

View File

@ -10,9 +10,6 @@ INCLUDES = -I$(COCOS_ROOT)/external \
-I../GUI/CCControlExtension \ -I../GUI/CCControlExtension \
-I../network -I../network
DEFINES += -DCC_ENABLE_CHIPMUNK_INTEGRATION=1
DEFINES += -D__CC_PLATFORM_IMAGE_CPP__
SOURCES = ../CCBReader/CCBFileLoader.cpp \ SOURCES = ../CCBReader/CCBFileLoader.cpp \
../CCBReader/CCMenuItemImageLoader.cpp \ ../CCBReader/CCMenuItemImageLoader.cpp \
../CCBReader/CCBReader.cpp \ ../CCBReader/CCBReader.cpp \

View File

@ -45,8 +45,8 @@ cd $(dirname ${BASH_SOURCE[0]})
export MAKEFLAGS=-j10 export MAKEFLAGS=-j10
make PROJECT=linux DEBUG=1 clean make PLATFORM=linux DEBUG=1 clean
make PROJECT=linux DEBUG=0 clean make PLATFORM=linux DEBUG=0 clean
make PROJECT=linux DEBUG=1 all make PLATFORM=linux DEBUG=1 all
make PROJECT=linux DEBUG=0 all make PLATFORM=linux DEBUG=0 all

View File

@ -110,8 +110,6 @@ STATICLIBS += \
$(LIB_DIR)/libbox2d.a \ $(LIB_DIR)/libbox2d.a \
$(LIB_DIR)/libchipmunk.a $(LIB_DIR)/libchipmunk.a
DEFINES += -DCC_ENABLE_CHIPMUNK_INTEGRATION=1
####### Build rules ####### Build rules
$(TARGET): $(OBJECTS) $(STATICLIBS) $(COCOS_LIBS) $(CORE_MAKEFILE_LIST) $(TARGET): $(OBJECTS) $(STATICLIBS) $(COCOS_LIBS) $(CORE_MAKEFILE_LIST)
@mkdir -p $(@D) @mkdir -p $(@D)

View File

@ -1,5 +1,3 @@
DEFINES += -DCC_ENABLE_BOX2D_INTEGRATION
COCOS_ROOT = ../../../.. COCOS_ROOT = ../../../..
COCOS2DX_PATH = $(COCOS_ROOT)/cocos2dx COCOS2DX_PATH = $(COCOS_ROOT)/cocos2dx