Skip to content

Commit

Permalink
move
Browse files Browse the repository at this point in the history
  • Loading branch information
dualface committed May 13, 2013
1 parent fa31b55 commit 0bc4f81
Show file tree
Hide file tree
Showing 17 changed files with 827 additions and 108 deletions.
6 changes: 6 additions & 0 deletions lib/proj.ios/libquickcocos2dx.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@
F4D752AF173887840094A93B /* PhysicsWorld.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4D752AD173887840094A93B /* PhysicsWorld.cpp */; };
F4D752B217389F260094A93B /* PhysicsBody.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4D752B117389F260094A93B /* PhysicsBody.cpp */; };
F4D752B51738C8BB0094A93B /* PhysicsWorld_luabinding.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4D752B31738C8BB0094A93B /* PhysicsWorld_luabinding.cpp */; };
F4DFA53B173BDC2400973237 /* PhysicsDebugNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4DFA539173BDC2400973237 /* PhysicsDebugNode.cpp */; };
F4E50FE016CF7AB200AA7E90 /* cocos2dx_extensions_luabinding.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4E50FDD16CF7AB200AA7E90 /* cocos2dx_extensions_luabinding.cpp */; };
F4EF0A5D16D70C1E00B81E8C /* CCFileUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4EF0A5C16D70C1E00B81E8C /* CCFileUtils.cpp */; };
F4F26D2F1727E73D002D3935 /* auxiliar.c in Sources */ = {isa = PBXBuildFile; fileRef = F4F26D0F1727E73D002D3935 /* auxiliar.c */; };
Expand Down Expand Up @@ -871,6 +872,8 @@
F4D752D11738CA5A0094A93B /* cpSpace.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cpSpace.h; sourceTree = "<group>"; };
F4D752D21738CA5A0094A93B /* cpSpatialIndex.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cpSpatialIndex.h; sourceTree = "<group>"; };
F4D752D31738CA5A0094A93B /* cpVect.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cpVect.h; sourceTree = "<group>"; };
F4DFA539173BDC2400973237 /* PhysicsDebugNode.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PhysicsDebugNode.cpp; sourceTree = "<group>"; };
F4DFA53A173BDC2400973237 /* PhysicsDebugNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PhysicsDebugNode.h; sourceTree = "<group>"; };
F4E50FDD16CF7AB200AA7E90 /* cocos2dx_extensions_luabinding.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cocos2dx_extensions_luabinding.cpp; sourceTree = "<group>"; };
F4E50FDE16CF7AB200AA7E90 /* cocos2dx_extensions_luabinding.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cocos2dx_extensions_luabinding.h; sourceTree = "<group>"; };
F4EF0A5C16D70C1E00B81E8C /* CCFileUtils.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCFileUtils.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2178,6 +2181,8 @@
children = (
F4D752B117389F260094A93B /* PhysicsBody.cpp */,
F4D752B017389F1A0094A93B /* PhysicsBody.h */,
F4DFA539173BDC2400973237 /* PhysicsDebugNode.cpp */,
F4DFA53A173BDC2400973237 /* PhysicsDebugNode.h */,
F43D103F1738D50C00B189FF /* PhysicsShape.cpp */,
F43D10401738D50C00B189FF /* PhysicsShape.h */,
F4D752AD173887840094A93B /* PhysicsWorld.cpp */,
Expand Down Expand Up @@ -2648,6 +2653,7 @@
F4D752B217389F260094A93B /* PhysicsBody.cpp in Sources */,
F4D752B51738C8BB0094A93B /* PhysicsWorld_luabinding.cpp in Sources */,
F43D10411738D50D00B189FF /* PhysicsShape.cpp in Sources */,
F4DFA53B173BDC2400973237 /* PhysicsDebugNode.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion lib/proj.mac/libquickcocos2dx.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1523,12 +1523,12 @@
F4DE082916800061005DB2DB /* libquickcocos2dx */ = {
isa = PBXGroup;
children = (
F43D10441738DA2500B189FF /* third_party */,
F4DE084216800151005DB2DB /* cocos2d-x */,
F4DCAD28168C153000679DBE /* cocos2dx_extensions_luabinding */,
F425ABD016B7777A00A1C8DB /* cocos2dx_extra */,
F4B4EF3516C7E0AE00736FFE /* lua_extensions */,
F43991E216816AB600AE90D1 /* luajit2 */,
F43D10441738DA2500B189FF /* third_party */,
);
name = libquickcocos2dx;
path = lib;
Expand Down
33 changes: 33 additions & 0 deletions lib/third_party/chipmunk/luabinding/PhysicsBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,39 @@ PhysicsShape *PhysicsBody::addBoxShape(float width, float height)
return addShape(cpBoxShapeNew(m_body, width, height));
}

PhysicsShape *PhysicsBody::addPolygonShape(CCPointArray *vertexs, float offsetX, float offsetY)
{
int numVertexs = vertexs->count();
cpVect *cpVertexs = new cpVect[numVertexs];
for (int i = 0; i < numVertexs; ++i)
{
const CCPoint pos = vertexs->get(i);
cpVertexs[i] = cpv(pos.x, pos.y);
}

PhysicsShape *shape = addShape(cpPolyShapeNew(m_body, numVertexs, cpVertexs, cpv(offsetX, offsetY)));
delete []cpVertexs;
return shape;
}

PhysicsShape *PhysicsBody::addPolygonShape(int numVertexs, CCPoint *vertexs, float offsetX, float offsetY)
{
cpVect *cpVertexs = new cpVect[numVertexs];
for (int i = 0; i < numVertexs; ++i)
{
cpVertexs[i] = cpv(vertexs[i].x, vertexs[i].y);
}

PhysicsShape *shape = addShape(cpPolyShapeNew(m_body, numVertexs, cpVertexs, cpv(offsetX, offsetY)));
delete []cpVertexs;
return shape;
}

PhysicsShape *PhysicsBody::addPolygonShape(int numVertexs, cpVect *vertexs, float offsetX, float offsetY)
{
return addShape(cpPolyShapeNew(m_body, numVertexs, vertexs, cpv(offsetX, offsetY)));
}

void PhysicsBody::removeShapeAtIndex(int index)
{
CCAssert(index >= 0 && index < m_shapes->count(), "PhysicsBody::removeShapeAtIndex() - Invalid index");
Expand Down
5 changes: 4 additions & 1 deletion lib/third_party/chipmunk/luabinding/PhysicsBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ class PhysicsBody : public CCObject
void setPosition(float x, float y);

PhysicsShape *addSegmentShape(float lowerLeftX, float lowerLeftY, float lowerRightX, float lowerRightY, float thickness);
PhysicsShape *addCircleShape(float radius, float offsetX, float offsetY);
PhysicsShape *addCircleShape(float radius, float offsetX = 0, float offsetY = 0);
PhysicsShape *addBoxShape(float width, float height);
PhysicsShape *addPolygonShape(CCPointArray *vertexs, float offsetX = 0, float offsetY = 0);
PhysicsShape *addPolygonShape(int numVertexs, CCPoint *vertexs, float offsetX = 0, float offsetY = 0);
PhysicsShape *addPolygonShape(int numVertexs, cpVect *vertexs, float offsetX = 0, float offsetY = 0);

void removeShapeAtIndex(int index);
void removeShape(PhysicsShape *shapeObject);
Expand Down
160 changes: 135 additions & 25 deletions lib/third_party/chipmunk/luabinding/PhysicsWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,41 @@
#include "PhysicsBody.h"
#include "PhysicsDebugNode.h"

#if CC_LUA_ENGINE_ENABLED > 0
#include "CCLuaEngine.h"
#include "CCLuaStack.h"
#endif

using namespace cocos2d;

cpVectArray *cpVectArray::createFromCCPointArray(CCPointArray *points)
{
cpVectArray *array = new cpVectArray();
array->initWithCCPointArray(points);
array->autorelease();
return array;
}

cpVectArray::~cpVectArray(void)
{
free(m_verts);
}

bool cpVectArray::initWithCCPointArray(CCPointArray *points)
{
CCAssert(points->count() > 0, "cpVectArray::initWithCCPointArray() - can't convert empty array");
m_count = points->count();
m_verts = (cpVect*)malloc(sizeof(cpVect) * m_count);
for (int i = 0; i < m_count; ++i)
{
const CCPoint pos = points->get(i);
m_verts[i] = cpv(pos.x, pos.y);
}
return true;
}

// ----------------------------------------

PhysicsWorld *PhysicsWorld::create(void)
{
PhysicsWorld *world = new PhysicsWorld();
Expand All @@ -13,6 +46,15 @@ PhysicsWorld *PhysicsWorld::create(void)
return world;
}

PhysicsWorld *PhysicsWorld::create(float gravityX, float gravityY)
{
PhysicsWorld *world = new PhysicsWorld();
world->init();
world->setGravity(gravityX, gravityY);
world->autorelease();
return world;
}

PhysicsWorld::~PhysicsWorld(void)
{
unbindAllNodes();
Expand All @@ -27,11 +69,22 @@ bool PhysicsWorld::init(void)
return true;
}

cpSpace *PhysicsWorld::getSpace(void)
{
return m_space;
}

PhysicsDebugNode *PhysicsWorld::createDebugNode(void)
{
return PhysicsDebugNode::create(m_space);
}

const CCPoint PhysicsWorld::getGravity(void)
{
const cpVect gravity = cpSpaceGetGravity(m_space);
return CCPoint(gravity.x, gravity.y);
}

void PhysicsWorld::getGravity(float *gravityX, float *gravityY)
{
const cpVect gravity = cpSpaceGetGravity(m_space);
Expand All @@ -44,46 +97,97 @@ void PhysicsWorld::setGravity(float gravityX, float gravityY)
cpSpaceSetGravity(m_space, cpv(gravityX, gravityY));
}

float PhysicsWorld::calcMomentForCircle(float mass, float innerRadius, float outerRadius, float offsetX, float offsetY)
PhysicsBody *PhysicsWorld::addCircleShape(float mass, float radius, const CCPoint offset/*= CCPointZero*/)
{
return cpMomentForCircle(mass, innerRadius, outerRadius, cpv(offsetX, offsetY));
return addCircleShape(mass, radius, offset.x, offset.y);
}

PhysicsBody *PhysicsWorld::bindNodeToDefaultStaticBody(CCNode *node)
PhysicsBody *PhysicsWorld::addCircleShape(float mass, float radius, float offsetX/*= 0*/, float offsetY/*= 0*/)
{
CCAssert(m_bodies.find(node) == m_bodies.end(), "PhysicsWorld::bindNodeToDefaultStaticBody() - Node already in world");

if (!m_defaultStaticBody)
PhysicsBody *body;
if (mass <= 0)
{
m_defaultStaticBody = PhysicsBody::defaultStaticBody(this);
m_defaultStaticBody->retain();
body = PhysicsBody::createStaticBody(this);
}

node->retain();
m_defaultStaticBody->retain();
m_bodies[node] = m_defaultStaticBody;

return m_defaultStaticBody;
else
{
float moment = cpMomentForCircle(mass, 0, radius, cpv(offsetX, offsetY));
body = PhysicsBody::create(this, mass, moment);
}
body->addCircleShape(radius, offsetX, offsetY);
return body;
}

PhysicsBody *PhysicsWorld::bindNodeToNewStaticBody(CCNode *node)
PhysicsBody *PhysicsWorld::addBoxShape(float mass, float width, float height)
{
CCAssert(m_bodies.find(node) == m_bodies.end(), "PhysicsWorld::bindNodeToNewStaticBody() - Node already in world");
PhysicsBody *body = PhysicsBody::createStaticBody(this);
node->retain();
body->retain();
m_bodies[node] = body;
float moment = cpMomentForBox(mass, width, height);
PhysicsBody *body = PhysicsBody::create(this, mass, moment);
body->addBoxShape(width, height);
return body;
}

PhysicsBody *PhysicsWorld::bindNodeToNewBody(CCNode *node, float mass, float moment)
PhysicsBody *PhysicsWorld::addPolygonShape(float mass, CCPointArray *vertexes, const CCPoint offset/*= CCPointZero*/)
{
CCAssert(m_bodies.find(node) == m_bodies.end(), "PhysicsWorld::bindNodeToNewBody() - Node already in world");
return addPolygonShape(mass, vertexes, offset.x, offset.y);
}

PhysicsBody *PhysicsWorld::addPolygonShape(float mass, CCPointArray *vertexes, float offsetX/*= 0*/, float offsetY/*= 0*/)
{
cpVectArray *verts = cpVectArray::createFromCCPointArray(vertexes);
float moment = cpMomentForPoly(mass, verts->count(), verts->data(), cpv(offsetX, offsetY));
PhysicsBody *body = PhysicsBody::create(this, mass, moment);
body->addPolygonShape(verts->count(), verts->data(), offsetY, offsetY);
return body;
}

#if CC_LUA_ENGINE_ENABLED > 0
PhysicsBody *PhysicsWorld::addPolygonShape(float mass, int vertexes, float offsetX/*= 0*/, float offsetY/*= 0*/)
{
lua_State *L = CCLuaEngine::defaultEngine()->getLuaStack()->getLuaState();
if (!lua_istable(L, vertexes)) return NULL;

// count
int count = 0;
lua_pushnil(L);
while (lua_next(L, vertexes) != 0)
{
if (lua_istable(L, -1)) count++;
lua_pop(L, 1);
}

cpVect *verts = (cpVect*)malloc(sizeof(cpVect) * count);
lua_pushnil(L);
count = 0;
while (lua_next(L, vertexes) != 0)
{
if (!lua_istable(L, -1)) continue;
lua_rawgeti(L, -1, 1);
float x = lua_tonumber(L, -1);
lua_pop(L, 1);
lua_rawgeti(L, -1, 2);
float y = lua_tonumber(L, -1);
lua_pop(L, 1);
verts[count] = cpv(x, y);
count++;
lua_pop(L, 1);
}

float moment = cpMomentForPoly(mass, count, verts, cpv(offsetX, offsetY));
CCAssert(moment == moment, "PhysicsWorld::addPolygonShape() - invalid moment");
PhysicsBody *body = PhysicsBody::create(this, mass, moment);
body->addPolygonShape(count, verts, offsetY, offsetY);
return body;
}
#endif

void PhysicsWorld::bindNodeToBody(CCNode *node, PhysicsBody *body)
{
CCAssert(node != NULL, "PhysicsWorld::bindNodeToBody() - invalid node");
CCAssert(body != NULL, "PhysicsWorld::bindNodeToBody() - invalid body");
CCAssert(m_bodies.find(node) == m_bodies.end(), "PhysicsWorld::bindNodeToBody() - Node already in world");
node->retain();
body->retain();
m_bodies[node] = body;
return body;
}

void PhysicsWorld::unbindNode(CCNode *node)
Expand All @@ -105,6 +209,12 @@ void PhysicsWorld::unbindAllNodes(void)
m_bodies.clear();
}

PhysicsBody *PhysicsWorld::getBodyByNode(CCNode *node)
{
PhysicsWorldBodyMapIterator it = m_bodies.find(node);
return it != m_bodies.end() ? it->second : NULL;
}

void PhysicsWorld::start(void)
{
scheduleUpdate();
Expand All @@ -121,12 +231,12 @@ void PhysicsWorld::update(float dt)
{
cpBody *body = it->second->getBody();
const cpVect pos = cpBodyGetPos(body);

CCNode *node = it->first;
node->setRotation(-CC_RADIANS_TO_DEGREES(cpBodyGetAngle(body)));
node->setPosition(pos.x, pos.y);
}

cpSpaceStep(m_space, dt);
}

Expand Down
Loading

0 comments on commit 0bc4f81

Please sign in to comment.