Skip to content

Commit 0bc4f81

Browse files
committed
move
1 parent fa31b55 commit 0bc4f81

File tree

17 files changed

+827
-108
lines changed

17 files changed

+827
-108
lines changed

lib/proj.ios/libquickcocos2dx.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@
275275
F4D752AF173887840094A93B /* PhysicsWorld.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4D752AD173887840094A93B /* PhysicsWorld.cpp */; };
276276
F4D752B217389F260094A93B /* PhysicsBody.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4D752B117389F260094A93B /* PhysicsBody.cpp */; };
277277
F4D752B51738C8BB0094A93B /* PhysicsWorld_luabinding.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4D752B31738C8BB0094A93B /* PhysicsWorld_luabinding.cpp */; };
278+
F4DFA53B173BDC2400973237 /* PhysicsDebugNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4DFA539173BDC2400973237 /* PhysicsDebugNode.cpp */; };
278279
F4E50FE016CF7AB200AA7E90 /* cocos2dx_extensions_luabinding.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4E50FDD16CF7AB200AA7E90 /* cocos2dx_extensions_luabinding.cpp */; };
279280
F4EF0A5D16D70C1E00B81E8C /* CCFileUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4EF0A5C16D70C1E00B81E8C /* CCFileUtils.cpp */; };
280281
F4F26D2F1727E73D002D3935 /* auxiliar.c in Sources */ = {isa = PBXBuildFile; fileRef = F4F26D0F1727E73D002D3935 /* auxiliar.c */; };
@@ -871,6 +872,8 @@
871872
F4D752D11738CA5A0094A93B /* cpSpace.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cpSpace.h; sourceTree = "<group>"; };
872873
F4D752D21738CA5A0094A93B /* cpSpatialIndex.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cpSpatialIndex.h; sourceTree = "<group>"; };
873874
F4D752D31738CA5A0094A93B /* cpVect.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cpVect.h; sourceTree = "<group>"; };
875+
F4DFA539173BDC2400973237 /* PhysicsDebugNode.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PhysicsDebugNode.cpp; sourceTree = "<group>"; };
876+
F4DFA53A173BDC2400973237 /* PhysicsDebugNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PhysicsDebugNode.h; sourceTree = "<group>"; };
874877
F4E50FDD16CF7AB200AA7E90 /* cocos2dx_extensions_luabinding.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cocos2dx_extensions_luabinding.cpp; sourceTree = "<group>"; };
875878
F4E50FDE16CF7AB200AA7E90 /* cocos2dx_extensions_luabinding.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cocos2dx_extensions_luabinding.h; sourceTree = "<group>"; };
876879
F4EF0A5C16D70C1E00B81E8C /* CCFileUtils.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCFileUtils.cpp; sourceTree = "<group>"; };
@@ -2178,6 +2181,8 @@
21782181
children = (
21792182
F4D752B117389F260094A93B /* PhysicsBody.cpp */,
21802183
F4D752B017389F1A0094A93B /* PhysicsBody.h */,
2184+
F4DFA539173BDC2400973237 /* PhysicsDebugNode.cpp */,
2185+
F4DFA53A173BDC2400973237 /* PhysicsDebugNode.h */,
21812186
F43D103F1738D50C00B189FF /* PhysicsShape.cpp */,
21822187
F43D10401738D50C00B189FF /* PhysicsShape.h */,
21832188
F4D752AD173887840094A93B /* PhysicsWorld.cpp */,
@@ -2648,6 +2653,7 @@
26482653
F4D752B217389F260094A93B /* PhysicsBody.cpp in Sources */,
26492654
F4D752B51738C8BB0094A93B /* PhysicsWorld_luabinding.cpp in Sources */,
26502655
F43D10411738D50D00B189FF /* PhysicsShape.cpp in Sources */,
2656+
F4DFA53B173BDC2400973237 /* PhysicsDebugNode.cpp in Sources */,
26512657
);
26522658
runOnlyForDeploymentPostprocessing = 0;
26532659
};

lib/proj.mac/libquickcocos2dx.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,12 +1523,12 @@
15231523
F4DE082916800061005DB2DB /* libquickcocos2dx */ = {
15241524
isa = PBXGroup;
15251525
children = (
1526-
F43D10441738DA2500B189FF /* third_party */,
15271526
F4DE084216800151005DB2DB /* cocos2d-x */,
15281527
F4DCAD28168C153000679DBE /* cocos2dx_extensions_luabinding */,
15291528
F425ABD016B7777A00A1C8DB /* cocos2dx_extra */,
15301529
F4B4EF3516C7E0AE00736FFE /* lua_extensions */,
15311530
F43991E216816AB600AE90D1 /* luajit2 */,
1531+
F43D10441738DA2500B189FF /* third_party */,
15321532
);
15331533
name = libquickcocos2dx;
15341534
path = lib;

lib/third_party/chipmunk/luabinding/PhysicsBody.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,39 @@ PhysicsShape *PhysicsBody::addBoxShape(float width, float height)
100100
return addShape(cpBoxShapeNew(m_body, width, height));
101101
}
102102

103+
PhysicsShape *PhysicsBody::addPolygonShape(CCPointArray *vertexs, float offsetX, float offsetY)
104+
{
105+
int numVertexs = vertexs->count();
106+
cpVect *cpVertexs = new cpVect[numVertexs];
107+
for (int i = 0; i < numVertexs; ++i)
108+
{
109+
const CCPoint pos = vertexs->get(i);
110+
cpVertexs[i] = cpv(pos.x, pos.y);
111+
}
112+
113+
PhysicsShape *shape = addShape(cpPolyShapeNew(m_body, numVertexs, cpVertexs, cpv(offsetX, offsetY)));
114+
delete []cpVertexs;
115+
return shape;
116+
}
117+
118+
PhysicsShape *PhysicsBody::addPolygonShape(int numVertexs, CCPoint *vertexs, float offsetX, float offsetY)
119+
{
120+
cpVect *cpVertexs = new cpVect[numVertexs];
121+
for (int i = 0; i < numVertexs; ++i)
122+
{
123+
cpVertexs[i] = cpv(vertexs[i].x, vertexs[i].y);
124+
}
125+
126+
PhysicsShape *shape = addShape(cpPolyShapeNew(m_body, numVertexs, cpVertexs, cpv(offsetX, offsetY)));
127+
delete []cpVertexs;
128+
return shape;
129+
}
130+
131+
PhysicsShape *PhysicsBody::addPolygonShape(int numVertexs, cpVect *vertexs, float offsetX, float offsetY)
132+
{
133+
return addShape(cpPolyShapeNew(m_body, numVertexs, vertexs, cpv(offsetX, offsetY)));
134+
}
135+
103136
void PhysicsBody::removeShapeAtIndex(int index)
104137
{
105138
CCAssert(index >= 0 && index < m_shapes->count(), "PhysicsBody::removeShapeAtIndex() - Invalid index");

lib/third_party/chipmunk/luabinding/PhysicsBody.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ class PhysicsBody : public CCObject
2828
void setPosition(float x, float y);
2929

3030
PhysicsShape *addSegmentShape(float lowerLeftX, float lowerLeftY, float lowerRightX, float lowerRightY, float thickness);
31-
PhysicsShape *addCircleShape(float radius, float offsetX, float offsetY);
31+
PhysicsShape *addCircleShape(float radius, float offsetX = 0, float offsetY = 0);
3232
PhysicsShape *addBoxShape(float width, float height);
33+
PhysicsShape *addPolygonShape(CCPointArray *vertexs, float offsetX = 0, float offsetY = 0);
34+
PhysicsShape *addPolygonShape(int numVertexs, CCPoint *vertexs, float offsetX = 0, float offsetY = 0);
35+
PhysicsShape *addPolygonShape(int numVertexs, cpVect *vertexs, float offsetX = 0, float offsetY = 0);
3336

3437
void removeShapeAtIndex(int index);
3538
void removeShape(PhysicsShape *shapeObject);

lib/third_party/chipmunk/luabinding/PhysicsWorld.cpp

Lines changed: 135 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,41 @@
33
#include "PhysicsBody.h"
44
#include "PhysicsDebugNode.h"
55

6+
#if CC_LUA_ENGINE_ENABLED > 0
7+
#include "CCLuaEngine.h"
8+
#include "CCLuaStack.h"
9+
#endif
10+
611
using namespace cocos2d;
712

13+
cpVectArray *cpVectArray::createFromCCPointArray(CCPointArray *points)
14+
{
15+
cpVectArray *array = new cpVectArray();
16+
array->initWithCCPointArray(points);
17+
array->autorelease();
18+
return array;
19+
}
20+
21+
cpVectArray::~cpVectArray(void)
22+
{
23+
free(m_verts);
24+
}
25+
26+
bool cpVectArray::initWithCCPointArray(CCPointArray *points)
27+
{
28+
CCAssert(points->count() > 0, "cpVectArray::initWithCCPointArray() - can't convert empty array");
29+
m_count = points->count();
30+
m_verts = (cpVect*)malloc(sizeof(cpVect) * m_count);
31+
for (int i = 0; i < m_count; ++i)
32+
{
33+
const CCPoint pos = points->get(i);
34+
m_verts[i] = cpv(pos.x, pos.y);
35+
}
36+
return true;
37+
}
38+
39+
// ----------------------------------------
40+
841
PhysicsWorld *PhysicsWorld::create(void)
942
{
1043
PhysicsWorld *world = new PhysicsWorld();
@@ -13,6 +46,15 @@ PhysicsWorld *PhysicsWorld::create(void)
1346
return world;
1447
}
1548

49+
PhysicsWorld *PhysicsWorld::create(float gravityX, float gravityY)
50+
{
51+
PhysicsWorld *world = new PhysicsWorld();
52+
world->init();
53+
world->setGravity(gravityX, gravityY);
54+
world->autorelease();
55+
return world;
56+
}
57+
1658
PhysicsWorld::~PhysicsWorld(void)
1759
{
1860
unbindAllNodes();
@@ -27,11 +69,22 @@ bool PhysicsWorld::init(void)
2769
return true;
2870
}
2971

72+
cpSpace *PhysicsWorld::getSpace(void)
73+
{
74+
return m_space;
75+
}
76+
3077
PhysicsDebugNode *PhysicsWorld::createDebugNode(void)
3178
{
3279
return PhysicsDebugNode::create(m_space);
3380
}
3481

82+
const CCPoint PhysicsWorld::getGravity(void)
83+
{
84+
const cpVect gravity = cpSpaceGetGravity(m_space);
85+
return CCPoint(gravity.x, gravity.y);
86+
}
87+
3588
void PhysicsWorld::getGravity(float *gravityX, float *gravityY)
3689
{
3790
const cpVect gravity = cpSpaceGetGravity(m_space);
@@ -44,46 +97,97 @@ void PhysicsWorld::setGravity(float gravityX, float gravityY)
4497
cpSpaceSetGravity(m_space, cpv(gravityX, gravityY));
4598
}
4699

47-
float PhysicsWorld::calcMomentForCircle(float mass, float innerRadius, float outerRadius, float offsetX, float offsetY)
100+
PhysicsBody *PhysicsWorld::addCircleShape(float mass, float radius, const CCPoint offset/*= CCPointZero*/)
48101
{
49-
return cpMomentForCircle(mass, innerRadius, outerRadius, cpv(offsetX, offsetY));
102+
return addCircleShape(mass, radius, offset.x, offset.y);
50103
}
51104

52-
PhysicsBody *PhysicsWorld::bindNodeToDefaultStaticBody(CCNode *node)
105+
PhysicsBody *PhysicsWorld::addCircleShape(float mass, float radius, float offsetX/*= 0*/, float offsetY/*= 0*/)
53106
{
54-
CCAssert(m_bodies.find(node) == m_bodies.end(), "PhysicsWorld::bindNodeToDefaultStaticBody() - Node already in world");
55-
56-
if (!m_defaultStaticBody)
107+
PhysicsBody *body;
108+
if (mass <= 0)
57109
{
58-
m_defaultStaticBody = PhysicsBody::defaultStaticBody(this);
59-
m_defaultStaticBody->retain();
110+
body = PhysicsBody::createStaticBody(this);
60111
}
61-
62-
node->retain();
63-
m_defaultStaticBody->retain();
64-
m_bodies[node] = m_defaultStaticBody;
65-
66-
return m_defaultStaticBody;
112+
else
113+
{
114+
float moment = cpMomentForCircle(mass, 0, radius, cpv(offsetX, offsetY));
115+
body = PhysicsBody::create(this, mass, moment);
116+
}
117+
body->addCircleShape(radius, offsetX, offsetY);
118+
return body;
67119
}
68120

69-
PhysicsBody *PhysicsWorld::bindNodeToNewStaticBody(CCNode *node)
121+
PhysicsBody *PhysicsWorld::addBoxShape(float mass, float width, float height)
70122
{
71-
CCAssert(m_bodies.find(node) == m_bodies.end(), "PhysicsWorld::bindNodeToNewStaticBody() - Node already in world");
72-
PhysicsBody *body = PhysicsBody::createStaticBody(this);
73-
node->retain();
74-
body->retain();
75-
m_bodies[node] = body;
123+
float moment = cpMomentForBox(mass, width, height);
124+
PhysicsBody *body = PhysicsBody::create(this, mass, moment);
125+
body->addBoxShape(width, height);
76126
return body;
77127
}
78128

79-
PhysicsBody *PhysicsWorld::bindNodeToNewBody(CCNode *node, float mass, float moment)
129+
PhysicsBody *PhysicsWorld::addPolygonShape(float mass, CCPointArray *vertexes, const CCPoint offset/*= CCPointZero*/)
80130
{
81-
CCAssert(m_bodies.find(node) == m_bodies.end(), "PhysicsWorld::bindNodeToNewBody() - Node already in world");
131+
return addPolygonShape(mass, vertexes, offset.x, offset.y);
132+
}
133+
134+
PhysicsBody *PhysicsWorld::addPolygonShape(float mass, CCPointArray *vertexes, float offsetX/*= 0*/, float offsetY/*= 0*/)
135+
{
136+
cpVectArray *verts = cpVectArray::createFromCCPointArray(vertexes);
137+
float moment = cpMomentForPoly(mass, verts->count(), verts->data(), cpv(offsetX, offsetY));
82138
PhysicsBody *body = PhysicsBody::create(this, mass, moment);
139+
body->addPolygonShape(verts->count(), verts->data(), offsetY, offsetY);
140+
return body;
141+
}
142+
143+
#if CC_LUA_ENGINE_ENABLED > 0
144+
PhysicsBody *PhysicsWorld::addPolygonShape(float mass, int vertexes, float offsetX/*= 0*/, float offsetY/*= 0*/)
145+
{
146+
lua_State *L = CCLuaEngine::defaultEngine()->getLuaStack()->getLuaState();
147+
if (!lua_istable(L, vertexes)) return NULL;
148+
149+
// count
150+
int count = 0;
151+
lua_pushnil(L);
152+
while (lua_next(L, vertexes) != 0)
153+
{
154+
if (lua_istable(L, -1)) count++;
155+
lua_pop(L, 1);
156+
}
157+
158+
cpVect *verts = (cpVect*)malloc(sizeof(cpVect) * count);
159+
lua_pushnil(L);
160+
count = 0;
161+
while (lua_next(L, vertexes) != 0)
162+
{
163+
if (!lua_istable(L, -1)) continue;
164+
lua_rawgeti(L, -1, 1);
165+
float x = lua_tonumber(L, -1);
166+
lua_pop(L, 1);
167+
lua_rawgeti(L, -1, 2);
168+
float y = lua_tonumber(L, -1);
169+
lua_pop(L, 1);
170+
verts[count] = cpv(x, y);
171+
count++;
172+
lua_pop(L, 1);
173+
}
174+
175+
float moment = cpMomentForPoly(mass, count, verts, cpv(offsetX, offsetY));
176+
CCAssert(moment == moment, "PhysicsWorld::addPolygonShape() - invalid moment");
177+
PhysicsBody *body = PhysicsBody::create(this, mass, moment);
178+
body->addPolygonShape(count, verts, offsetY, offsetY);
179+
return body;
180+
}
181+
#endif
182+
183+
void PhysicsWorld::bindNodeToBody(CCNode *node, PhysicsBody *body)
184+
{
185+
CCAssert(node != NULL, "PhysicsWorld::bindNodeToBody() - invalid node");
186+
CCAssert(body != NULL, "PhysicsWorld::bindNodeToBody() - invalid body");
187+
CCAssert(m_bodies.find(node) == m_bodies.end(), "PhysicsWorld::bindNodeToBody() - Node already in world");
83188
node->retain();
84189
body->retain();
85190
m_bodies[node] = body;
86-
return body;
87191
}
88192

89193
void PhysicsWorld::unbindNode(CCNode *node)
@@ -105,6 +209,12 @@ void PhysicsWorld::unbindAllNodes(void)
105209
m_bodies.clear();
106210
}
107211

212+
PhysicsBody *PhysicsWorld::getBodyByNode(CCNode *node)
213+
{
214+
PhysicsWorldBodyMapIterator it = m_bodies.find(node);
215+
return it != m_bodies.end() ? it->second : NULL;
216+
}
217+
108218
void PhysicsWorld::start(void)
109219
{
110220
scheduleUpdate();
@@ -121,12 +231,12 @@ void PhysicsWorld::update(float dt)
121231
{
122232
cpBody *body = it->second->getBody();
123233
const cpVect pos = cpBodyGetPos(body);
124-
234+
125235
CCNode *node = it->first;
126236
node->setRotation(-CC_RADIANS_TO_DEGREES(cpBodyGetAngle(body)));
127237
node->setPosition(pos.x, pos.y);
128238
}
129-
239+
130240
cpSpaceStep(m_space, dt);
131241
}
132242

0 commit comments

Comments
 (0)