3
3
#include " PhysicsBody.h"
4
4
#include " PhysicsDebugNode.h"
5
5
6
+ #if CC_LUA_ENGINE_ENABLED > 0
7
+ #include " CCLuaEngine.h"
8
+ #include " CCLuaStack.h"
9
+ #endif
10
+
6
11
using namespace cocos2d ;
7
12
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
+
8
41
PhysicsWorld *PhysicsWorld::create (void )
9
42
{
10
43
PhysicsWorld *world = new PhysicsWorld ();
@@ -13,6 +46,15 @@ PhysicsWorld *PhysicsWorld::create(void)
13
46
return world;
14
47
}
15
48
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
+
16
58
PhysicsWorld::~PhysicsWorld (void )
17
59
{
18
60
unbindAllNodes ();
@@ -27,11 +69,22 @@ bool PhysicsWorld::init(void)
27
69
return true ;
28
70
}
29
71
72
+ cpSpace *PhysicsWorld::getSpace (void )
73
+ {
74
+ return m_space;
75
+ }
76
+
30
77
PhysicsDebugNode *PhysicsWorld::createDebugNode (void )
31
78
{
32
79
return PhysicsDebugNode::create (m_space);
33
80
}
34
81
82
+ const CCPoint PhysicsWorld::getGravity (void )
83
+ {
84
+ const cpVect gravity = cpSpaceGetGravity (m_space);
85
+ return CCPoint (gravity.x , gravity.y );
86
+ }
87
+
35
88
void PhysicsWorld::getGravity (float *gravityX, float *gravityY)
36
89
{
37
90
const cpVect gravity = cpSpaceGetGravity (m_space);
@@ -44,46 +97,97 @@ void PhysicsWorld::setGravity(float gravityX, float gravityY)
44
97
cpSpaceSetGravity (m_space, cpv (gravityX, gravityY));
45
98
}
46
99
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 */ )
48
101
{
49
- return cpMomentForCircle (mass, innerRadius, outerRadius, cpv (offsetX, offsetY) );
102
+ return addCircleShape (mass, radius, offset. x , offset. y );
50
103
}
51
104
52
- PhysicsBody *PhysicsWorld::bindNodeToDefaultStaticBody (CCNode *node )
105
+ PhysicsBody *PhysicsWorld::addCircleShape ( float mass, float radius, float offsetX /* = 0 */ , float offsetY /* = 0 */ )
53
106
{
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 )
57
109
{
58
- m_defaultStaticBody = PhysicsBody::defaultStaticBody (this );
59
- m_defaultStaticBody->retain ();
110
+ body = PhysicsBody::createStaticBody (this );
60
111
}
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;
67
119
}
68
120
69
- PhysicsBody *PhysicsWorld::bindNodeToNewStaticBody (CCNode *node )
121
+ PhysicsBody *PhysicsWorld::addBoxShape ( float mass, float width, float height )
70
122
{
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);
76
126
return body;
77
127
}
78
128
79
- PhysicsBody *PhysicsWorld::bindNodeToNewBody (CCNode *node, float mass, float moment )
129
+ PhysicsBody *PhysicsWorld::addPolygonShape ( float mass, CCPointArray *vertexes, const CCPoint offset /* = CCPointZero */ )
80
130
{
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));
82
138
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" );
83
188
node->retain ();
84
189
body->retain ();
85
190
m_bodies[node] = body;
86
- return body;
87
191
}
88
192
89
193
void PhysicsWorld::unbindNode (CCNode *node)
@@ -105,6 +209,12 @@ void PhysicsWorld::unbindAllNodes(void)
105
209
m_bodies.clear ();
106
210
}
107
211
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
+
108
218
void PhysicsWorld::start (void )
109
219
{
110
220
scheduleUpdate ();
@@ -121,12 +231,12 @@ void PhysicsWorld::update(float dt)
121
231
{
122
232
cpBody *body = it->second ->getBody ();
123
233
const cpVect pos = cpBodyGetPos (body);
124
-
234
+
125
235
CCNode *node = it->first ;
126
236
node->setRotation (-CC_RADIANS_TO_DEGREES (cpBodyGetAngle (body)));
127
237
node->setPosition (pos.x , pos.y );
128
238
}
129
-
239
+
130
240
cpSpaceStep (m_space, dt);
131
241
}
132
242
0 commit comments