CCPhysicsSprite.cpp
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
*
* 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 "CCPhysicsSprite.h"
#include "base/CCDirector.h"
#include "base/CCEventDispatcher.h"
#if (CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION)
#if (CC_ENABLE_CHIPMUNK_INTEGRATION && CC_ENABLE_BOX2D_INTEGRATION)
#error "Either Chipmunk or Box2d should be enabled, but not both at the same time"
#endif
#if CC_ENABLE_CHIPMUNK_INTEGRATION
#include "chipmunk/chipmunk.h"
#elif CC_ENABLE_BOX2D_INTEGRATION
#include "Box2D/Box2D.h"
#endif
NS_CC_EXT_BEGIN
PhysicsSprite::PhysicsSprite()
: _ignoreBodyRotation(false)
, _CPBody(nullptr)
, _pB2Body(nullptr)
, _PTMRatio(0.0f)
, _syncTransform(nullptr)
{}
PhysicsSprite* PhysicsSprite::create()
{
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
if (pRet && pRet->init())
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
PhysicsSprite* PhysicsSprite::createWithTexture(Texture2D *pTexture)
{
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
if (pRet && pRet->initWithTexture(pTexture))
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
PhysicsSprite* PhysicsSprite::createWithTexture(Texture2D *pTexture, const Rect& rect)
{
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
if (pRet && pRet->initWithTexture(pTexture, rect))
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
PhysicsSprite* PhysicsSprite::createWithSpriteFrame(SpriteFrame *pSpriteFrame)
{
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
if (pRet && pRet->initWithSpriteFrame(pSpriteFrame))
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
PhysicsSprite* PhysicsSprite::createWithSpriteFrameName(const char *pszSpriteFrameName)
{
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
if (pRet && pRet->initWithSpriteFrameName(pszSpriteFrameName))
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
PhysicsSprite* PhysicsSprite::create(const char *pszFileName)
{
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
if (pRet && pRet->initWithFile(pszFileName))
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
PhysicsSprite* PhysicsSprite::create(const char *pszFileName, const Rect& rect)
{
PhysicsSprite* pRet = new (std::nothrow) PhysicsSprite();
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 getNodeToParentTransform won't be called.
bool PhysicsSprite::isDirty() const
{
return true;
}
bool PhysicsSprite::isIgnoreBodyRotation() const
{
return _ignoreBodyRotation;
}
void PhysicsSprite::setIgnoreBodyRotation(bool bIgnoreBodyRotation)
{
_ignoreBodyRotation = bIgnoreBodyRotation;
}
// Override the setters and getters to always reflect the body's properties.
const Vec2& PhysicsSprite::getPosition() const
{
return getPosFromPhysics();
}
void PhysicsSprite::getPosition(float* x, float* y) const
{
if (x == nullptr || y == nullptr) {
return;
}
const Vec2& pos = getPosFromPhysics();
*x = pos.x;
*y = pos.y;
}
float PhysicsSprite::getPositionX() const
{
return getPosFromPhysics().x;
}
float PhysicsSprite::getPositionY() const
{
return getPosFromPhysics().y;
}
Vec3 PhysicsSprite::getPosition3D() const
{
Vec2 pos = getPosFromPhysics();
return Vec3(pos.x, pos.y, 0);
}
//
// Chipmunk only
//
cpBody* PhysicsSprite::getCPBody() const
{
#if CC_ENABLE_CHIPMUNK_INTEGRATION
return _CPBody;
#else
CCASSERT(false, "Can't call chipmunk methods when Chipmunk is disabled");
return nullptr;
#endif
}
void PhysicsSprite::setCPBody(cpBody *pBody)
{
#if CC_ENABLE_CHIPMUNK_INTEGRATION
_CPBody = pBody;
#else
CCASSERT(false, "Can't call chipmunk methods when Chipmunk is disabled");
#endif
}
b2Body* PhysicsSprite::getB2Body() const
{
#if CC_ENABLE_BOX2D_INTEGRATION
return _pB2Body;
#else
CCASSERT(false, "Can't call box2d methods when Box2d is disabled");
return nullptr;
#endif
}
void PhysicsSprite::setB2Body(b2Body *pBody)
{
#if CC_ENABLE_BOX2D_INTEGRATION
_pB2Body = pBody;
#else
CC_UNUSED_PARAM(pBody);
CCASSERT(false, "Can't call box2d methods when Box2d is disabled");
#endif
}
float PhysicsSprite::getPTMRatio() const
{
#if CC_ENABLE_BOX2D_INTEGRATION
return _PTMRatio;
#else
CCASSERT(false, "Can't call box2d methods when Box2d is disabled");
return 0;
#endif
}
void PhysicsSprite::setPTMRatio(float fRatio)
{
#if CC_ENABLE_BOX2D_INTEGRATION
_PTMRatio = fRatio;
#else
CC_UNUSED_PARAM(fRatio);
CCASSERT(false, "Can't call box2d methods when Box2d is disabled");
#endif
}
//
// Common to Box2d and Chipmunk
//
const Vec2& PhysicsSprite::getPosFromPhysics() const
{
static Vec2 s_physicPosion;
#if CC_ENABLE_CHIPMUNK_INTEGRATION
cpVect cpPos = cpBodyGetPosition(_CPBody);
s_physicPosion = Vec2(cpPos.x, cpPos.y);
#elif CC_ENABLE_BOX2D_INTEGRATION
b2Vec2 pos = _pB2Body->GetPosition();
float x = pos.x * _PTMRatio;
float y = pos.y * _PTMRatio;
s_physicPosion.set(x,y);
#endif
return s_physicPosion;
}
void PhysicsSprite::setPosition(float x, float y)
{
#if CC_ENABLE_CHIPMUNK_INTEGRATION
cpVect cpPos = cpv(x, y);
cpBodySetPosition(_CPBody, cpPos);
#elif CC_ENABLE_BOX2D_INTEGRATION
float angle = _pB2Body->GetAngle();
_pB2Body->SetTransform(b2Vec2(x / _PTMRatio, y / _PTMRatio), angle);
#endif
}
void PhysicsSprite::setPosition(const Vec2 &pos)
{
setPosition(pos.x, pos.y);
}
void PhysicsSprite::setPositionX(float x)
{
setPosition(x, getPositionY());
}
void PhysicsSprite::setPositionY(float y)
{
setPosition(getPositionX(), y);
}
void PhysicsSprite::setPosition3D(const Vec3& position)
{
setPosition(position.x, position.y);
}
float PhysicsSprite::getRotation() const
{
#if CC_ENABLE_CHIPMUNK_INTEGRATION
return (_ignoreBodyRotation ? Sprite::getRotation() : -CC_RADIANS_TO_DEGREES(cpBodyGetAngle(_CPBody)));
#elif CC_ENABLE_BOX2D_INTEGRATION
return (_ignoreBodyRotation ? Sprite::getRotation() :
CC_RADIANS_TO_DEGREES(_pB2Body->GetAngle()));
#else
return 0.0f;
#endif
}
void PhysicsSprite::setRotation(float fRotation)
{
if (_ignoreBodyRotation)
{
Sprite::setRotation(fRotation);
}
#if CC_ENABLE_CHIPMUNK_INTEGRATION
else
{
cpBodySetAngle(_CPBody, -CC_DEGREES_TO_RADIANS(fRotation));
}
#elif CC_ENABLE_BOX2D_INTEGRATION
else
{
b2Vec2 p = _pB2Body->GetPosition();
float radians = CC_DEGREES_TO_RADIANS(fRotation);
_pB2Body->SetTransform(p, radians);
}
#endif
}
void PhysicsSprite::syncPhysicsTransform() const
{
// 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 CC_ENABLE_CHIPMUNK_INTEGRATION
cpVect rot = (_ignoreBodyRotation ? cpvforangle(-CC_DEGREES_TO_RADIANS(_rotationX)) : cpBodyGetRotation(_CPBody));
float x = cpBodyGetPosition(_CPBody).x + rot.x * -_anchorPointInPoints.x * _scaleX - rot.y * -_anchorPointInPoints.y * _scaleY;
float y = cpBodyGetPosition(_CPBody).y + rot.y * -_anchorPointInPoints.x * _scaleX + rot.x * -_anchorPointInPoints.y * _scaleY;
if (_ignoreAnchorPointForPosition)
{
x += _anchorPointInPoints.x;
y += _anchorPointInPoints.y;
}
float mat[] = { (float)rot.x * _scaleX, (float)rot.y * _scaleX, 0, 0,
(float)-rot.y * _scaleY, (float)rot.x * _scaleY, 0, 0,
0, 0, 1, 0,
x, y, 0, 1};
_transform.set(mat);
#elif CC_ENABLE_BOX2D_INTEGRATION
b2Vec2 pos = _pB2Body->GetPosition();
float x = pos.x * _PTMRatio;
float y = pos.y * _PTMRatio;
if (_ignoreAnchorPointForPosition)
{
x += _anchorPointInPoints.x;
y += _anchorPointInPoints.y;
}
// Make matrix
float radians = _pB2Body->GetAngle();
float c = cosf(radians);
float s = sinf(radians);
if (!_anchorPointInPoints.isZero())
{
x += ((c * -_anchorPointInPoints.x * _scaleX) + (-s * -_anchorPointInPoints.y * _scaleY));
y += ((s * -_anchorPointInPoints.x * _scaleX) + (c * -_anchorPointInPoints.y * _scaleY));
}
// Rot, Translate Matrix
float mat[] = { (float)c * _scaleX, (float)s * _scaleX, 0, 0,
(float)-s * _scaleY, (float)c * _scaleY, 0, 0,
0, 0, 1, 0,
x, y, 0, 1};
_transform.set(mat);
#endif
}
void PhysicsSprite::onEnter()
{
Node::onEnter();
_syncTransform = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_AFTER_UPDATE, std::bind(&PhysicsSprite::afterUpdate, this, std::placeholders::_1));
_syncTransform->retain();
}
void PhysicsSprite::onExit()
{
if (_syncTransform != nullptr)
{
Director::getInstance()->getEventDispatcher()->removeEventListener(_syncTransform);
_syncTransform->release();
}
Node::onExit();
}
void PhysicsSprite::afterUpdate(EventCustom* /*event*/)
{
syncPhysicsTransform();
_transformDirty = false;
_transformUpdated = true;
setDirtyRecursively(true);
}
NS_CC_EXT_END
#endif // CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION