-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvrope.cpp
293 lines (251 loc) · 11 KB
/
vrope.cpp
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
/*
MIT License.
Copyright (c) 2012 Creeng Ltd.
Copyright (c) 2012 Flightless Ltd.
Copyright (c) 2010 Clever Hamster Games.
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.
*/
//
// vrope.cpp
//
// Created by patrick on 14/10/2010.
// Modified by Flightless www.flightless.co.nz 20/4/2012
// Ported to cocos2d-x by Creeng www.creeng.com 12/7/2012
//
#include "vrope.h"
using namespace cocos2d;
VRope::VRope(b2Body* body1, b2Body* body2, CCSpriteBatchNode* ropeBatchNode)
: bodyA(body1), bodyB(body2), spriteSheet(ropeBatchNode), jointAB(0)
{
CCPoint pointA = ccp(bodyA->GetPosition().x*PTM_RATIO,bodyA->GetPosition().y*PTM_RATIO);
CCPoint pointB = ccp(bodyB->GetPosition().x*PTM_RATIO,bodyB->GetPosition().y*PTM_RATIO);
createRope(pointA, pointB);
}
// Flightless, init rope using a joint between two bodies
VRope::VRope(b2Joint* joint, CCSpriteBatchNode* ropeBatchNode)
: jointAB(joint), spriteSheet(ropeBatchNode), bodyA(0), bodyB(0)
{
CCPoint pointA = ccp(jointAB->GetAnchorA().x*PTM_RATIO,jointAB->GetAnchorA().y*PTM_RATIO);
CCPoint pointB = ccp(jointAB->GetAnchorB().x*PTM_RATIO,jointAB->GetAnchorB().y*PTM_RATIO);
createRope(pointA, pointB);
}
VRope::VRope(const CCPoint& pointA, const CCPoint& pointB, CCSpriteBatchNode* ropeBatchNode)
: spriteSheet(ropeBatchNode), bodyA(0), bodyB(0), jointAB(0)
{
createRope(pointA, pointB);
}
VRope::~VRope()
{
for(int i=0; i<numPoints; ++i)
{
delete vPoints[i];
if (i!=numPoints-1) {
delete vSticks[i];
}
}
}
void VRope::update(float dt)
{
CCPoint pointA, pointB;
if (bodyA) {
pointA = ccp(bodyA->GetPosition().x*PTM_RATIO,bodyA->GetPosition().y*PTM_RATIO);
pointB = ccp(bodyB->GetPosition().x*PTM_RATIO,bodyB->GetPosition().y*PTM_RATIO);
} else {
pointA = ccp(jointAB->GetAnchorA().x*PTM_RATIO,jointAB->GetAnchorA().y*PTM_RATIO);
pointB = ccp(jointAB->GetAnchorB().x*PTM_RATIO,jointAB->GetAnchorB().y*PTM_RATIO);
}
updateWithPoints(pointA, pointB, dt);
}
// Flightless, update rope by pre-integrating the gravity each step (optimised for changing gravity)
// nb. uses current global point gravity, should probably be moved to a gravity for each rope
void VRope::updateWithPreIntegratedGravity(float dt)
{
CCPoint pointA, pointB;
if (bodyA) {
pointA = ccp(bodyA->GetPosition().x*PTM_RATIO,bodyA->GetPosition().y*PTM_RATIO);
pointB = ccp(bodyB->GetPosition().x*PTM_RATIO,bodyB->GetPosition().y*PTM_RATIO);
} else {
pointA = ccp(jointAB->GetAnchorA().x*PTM_RATIO,jointAB->GetAnchorA().y*PTM_RATIO);
pointB = ccp(jointAB->GetAnchorB().x*PTM_RATIO,jointAB->GetAnchorB().y*PTM_RATIO);
}
// pre-integrate current gravity
CCPoint gravity = ccpMult(VPoint::getGravity(), dt);
// update points with pre-integrated gravity
updateWithPoints(pointA, pointB, gravity.x, gravity.y);
}
// Flightless, update rope by pre-integrating the gravity each step (optimised for changing gravity)
void VRope::updateWithPreIntegratedGravity(float dt, float gravityX, float gravityY)
{
CCPoint pointA, pointB;
if (bodyA) {
pointA = ccp(bodyA->GetPosition().x*PTM_RATIO,bodyA->GetPosition().y*PTM_RATIO);
pointB = ccp(bodyB->GetPosition().x*PTM_RATIO,bodyB->GetPosition().y*PTM_RATIO);
} else {
pointA = ccp(jointAB->GetAnchorA().x*PTM_RATIO,jointAB->GetAnchorA().y*PTM_RATIO);
pointB = ccp(jointAB->GetAnchorB().x*PTM_RATIO,jointAB->GetAnchorB().y*PTM_RATIO);
}
// update points with pre-integrated gravity
updateWithPoints(pointA, pointB, gravityX*dt, gravityY*dt);
}
// Flightless, update rope by pre-integrating the gravity each step (optimised for changing gravity)
// nb. this uses a gravity with origin (0,0) and an average of bodyA and bodyB positions to determine which way is 'down' for each rope.
void VRope::updateWithPreIntegratedOriginGravity(float dt)
{
CCPoint pointA, pointB;
if (bodyA) {
pointA = ccp(bodyA->GetPosition().x*PTM_RATIO,bodyA->GetPosition().y*PTM_RATIO);
pointB = ccp(bodyB->GetPosition().x*PTM_RATIO,bodyB->GetPosition().y*PTM_RATIO);
} else {
pointA = ccp(jointAB->GetAnchorA().x*PTM_RATIO,jointAB->GetAnchorA().y*PTM_RATIO);
pointB = ccp(jointAB->GetAnchorB().x*PTM_RATIO,jointAB->GetAnchorB().y*PTM_RATIO);
}
// pre-integrate gravity, based on average position of bodies
CCPoint gravityAtPoint = ccp(-0.5f*(pointA.x+pointB.x), -0.5f*(pointA.y+pointB.y));
gravityAtPoint = ccpMult(ccpNormalize(gravityAtPoint), -10.0f*dt); // nb. vrope uses negative gravity!
// update points with pre-integrated gravity
updateWithPoints(pointA, pointB, gravityAtPoint.x, gravityAtPoint.y);
}
void VRope::reset()
{
CCPoint pointA, pointB;
if (bodyA) {
pointA = ccp(bodyA->GetPosition().x*PTM_RATIO,bodyA->GetPosition().y*PTM_RATIO);
pointB = ccp(bodyB->GetPosition().x*PTM_RATIO,bodyB->GetPosition().y*PTM_RATIO);
} else {
pointA = ccp(jointAB->GetAnchorA().x*PTM_RATIO,jointAB->GetAnchorA().y*PTM_RATIO);
pointB = ccp(jointAB->GetAnchorB().x*PTM_RATIO,jointAB->GetAnchorB().y*PTM_RATIO);
}
resetWithPoints(pointA, pointB);
}
void VRope::createRope(const CCPoint& pointA, const CCPoint& pointB)
{
float distance = ccpDistance(pointA,pointB);
int segmentFactor = 20; // 16; //12; //increase value to have less segments per rope, decrease to have more segments
numPoints = (int) distance/segmentFactor;
CCPoint diffVector = ccpSub(pointB,pointA);
float multiplier = distance / (numPoints-1);
antiSagHack = 0.1f; //HACK: scale down rope points to cheat sag. set to 0 to disable, max suggested value 0.1
for(int i=0;i<numPoints;i++) {
CCPoint tmpVector = ccpAdd(pointA, ccpMult(ccpNormalize(diffVector), multiplier*i*(1-antiSagHack)));
VPoint *tmpPoint = new VPoint();
tmpPoint->setPos(tmpVector.x, tmpVector.y);
vPoints.push_back(tmpPoint);
}
for(int i=0;i<numPoints-1;i++) {
VStick* tmpStick = new VStick(vPoints[i], vPoints[i+1]);
vSticks.push_back(tmpStick);
}
if(spriteSheet) {
for(int i=0;i<numPoints-1;i++) {
VPoint* point1 = vSticks[i]->getPointA();
VPoint* point2 = vSticks[i]->getPointB();
CCPoint stickVector = ccpSub(ccp(point1->x,point1->y),ccp(point2->x,point2->y));
float stickAngle = ccpToAngle(stickVector);
float f = spriteSheet->getTextureAtlas()->getTexture()->getPixelsHigh() / CC_CONTENT_SCALE_FACTOR();
CCRect r = CCRectMake(0, 0, multiplier, f);
CCSprite* tmpSprite = CCSprite::spriteWithTexture(spriteSheet->getTexture(), r);
ccTexParams params = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
tmpSprite->getTexture()->setTexParameters(¶ms);
tmpSprite->setPosition(ccpMidpoint(ccp(point1->x, point1->y), ccp(point2->x, point2->y)));
tmpSprite->setRotation(-1 * CC_RADIANS_TO_DEGREES(stickAngle));
spriteSheet->addChild(tmpSprite);
ropeSprites.push_back(tmpSprite);
}
}
}
void VRope::resetWithPoints(const CCPoint &pointA, const CCPoint& pointB)
{
float distance = ccpDistance(pointA, pointB);
CCPoint diffVector = ccpSub(pointB, pointA);
float multiplier = distance / (numPoints - 1);
for(int i=0;i<numPoints;i++) {
CCPoint tmpVector = ccpAdd(pointA, ccpMult(ccpNormalize(diffVector), multiplier*i*(1-antiSagHack)));
VPoint *tmpPoint = vPoints[i];
tmpPoint->setPos(tmpVector.x, tmpVector.y);
}
}
void VRope::updateWithPoints(const CCPoint& pointA, const CCPoint& pointB, float dt)
{
//manually set position for first and last point of rope
vPoints[0]->setPos(pointA.x, pointA.y);
vPoints[numPoints -1]->setPos(pointB.x, pointB.y);
//update points, apply gravity
for(int i=1;i<numPoints-1;i++) {
vPoints[i]->applyGravity(dt);
vPoints[i]->update();
}
//contract sticks
int iterations = 4;
for(int j=0;j<iterations;j++) {
for(int i=0;i<numPoints-1;i++) {
vSticks[i]->contract();
}
}
}
void VRope::updateWithPoints(const CCPoint& pointA, const CCPoint& pointB, float gxdt, float gydt)
{
//manually set position for first and last point of rope
vPoints[0]->setPos(pointA.x, pointA.y);
vPoints[numPoints-1]->setPos(pointB.x, pointB.y);
//update points, apply pre-integrated gravity
for(int i=1;i<numPoints-1;i++) {
vPoints[i]->applyGravityxdt(gxdt, gydt);
vPoints[i]->update();
}
//contract sticks
int iterations = 4;
for(int j=0;j<iterations;j++) {
for(int i=0;i<numPoints-1;i++) {
vSticks[i]->contract();
}
}
}
void VRope::debugDraw()
{
//Depending on scenario, you might need to have different Disable/Enable of Client States
//glDisableClientState(GL_TEXTURE_2D);
//glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//glDisableClientState(GL_COLOR_ARRAY);
//set color and line width for ccDrawLine
ccDrawColor4F(0.0f, 0.0f, 1.0f, 1.0f);
glLineWidth(5.0f);
for(int i=0;i<numPoints-1;i++) {
//"debug" draw
VPoint *pointA = vSticks[i]->getPointA();
VPoint *pointB = vSticks[i]->getPointB();
ccDrawPoint(ccp(pointA->x,pointA->y));
ccDrawPoint(ccp(pointB->x,pointB->y));
//ccDrawLine(ccp(pointA.x,pointA.y),ccp(pointB.x,pointB.y));
}
//restore to white and default thickness
ccDrawColor4F(1.0f,1.0f,1.0f,1.0f);
glLineWidth(1);
//glEnableClientState(GL_TEXTURE_2D);
//glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//glEnableClientState(GL_COLOR_ARRAY);
}
void VRope::updateSprites()
{
if(spriteSheet) {
for(int i=0;i<numPoints-1;i++) {
VPoint *point1 = vSticks[i]->getPointA();
VPoint *point2 = vSticks[i]->getPointB();
CCPoint point1_ = ccp(point1->x,point1->y);
CCPoint point2_ = ccp(point2->x,point2->y);
float stickAngle = ccpToAngle(ccpSub(point1_,point2_));
CCSprite *tmpSprite = ropeSprites[i];
tmpSprite->setPosition(ccpMidpoint(point1_,point2_));
tmpSprite->setRotation(-CC_RADIANS_TO_DEGREES(stickAngle));
}
}
}
void VRope::removeSprites()
{
for(int i=0;i<numPoints-1;i++) {
CCSprite *tmpSprite = ropeSprites[i];
spriteSheet->removeChild(tmpSprite, true);
}
ropeSprites.clear();
}