void FXLightning::render(){ float angle = atan2f(end.y - start.y , end.x - start.x); float x = cosf(angle - M_PI/2.0) * width; float y = sinf(angle - M_PI/2.0) * width; float rx = cosf(angle - M_PI/2.0) * spread; float ry = sinf(angle - M_PI/2.0) * spread; static int counter = 0; int n_samples = ccpDistance(start, end)/480 * K_NUMBER_OF_LIGHTNING_SAMPLES; if (n_samples > K_NUMBER_OF_LIGHTNING_SAMPLES) { n_samples = K_NUMBER_OF_LIGHTNING_SAMPLES; } for (int i=0; i < n_samples; i+=2) { CCPoint p = ccpLerp(start, end, i/(float)(n_samples-1)); float r = ((rand()%100) / 100.0 - 0.5) * 7; // float r = 20* noise1(counter*speed); float w = 0; if (i < n_samples*0.5) { w = i/(float)n_samples * 3; } else{ w = (n_samples-i-2)/(float)n_samples * 3; } w = clampf(w, 0, 1); if (tapper == 0) { vtx[ i+0 ].x = p.x + x + rx*r*w; vtx[ i+0 ].y = p.y + y + ry*r*w; vtx[ i+1 ].x = p.x - x + rx*r*w; vtx[ i+1 ].y = p.y - y + ry*r*w; } else{ vtx[ i+0 ].x = p.x + x*tapper*(1-i/(float)n_samples) + rx*r; vtx[ i+0 ].y = p.y + y*tapper*(1-i/(float)n_samples) + ry*r; vtx[ i+1 ].x = p.x - x*tapper*(1-i/(float)n_samples) + rx*r; vtx[ i+1 ].y = p.y - y*tapper*(1-i/(float)n_samples) + ry*r; } counter+=5; } pShaderProgram->use(); pShaderProgram->setUniformsForBuiltins(); ccGLBindTexture2D(texture->getName()); glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vtx); glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, uvs); glDrawArrays(GL_TRIANGLE_STRIP, 0,n_samples); //DEBUG // glDrawArrays(GL_LINE_STRIP, 0, n_samples); CC_INCREMENT_GL_DRAWS(1); }
void Player::HitBall() { for (size_t i = 0; i < _ballManager->GetNumberOfBalls(); i++) { Ball& ball = *_ballManager->GetBallAtIndex(i); // don't test if ball belongs to other player // not enough balls to justify it. if (!ball.IsContained()) { Vec2 ppos = this->convertToWorldSpace(Vec2()); Vec2 bpos = ball.getParent()->convertToWorldSpace(ball.getPosition()); Vec2 toBall = bpos - ppos; float r = 100; if (toBall.length() < r) { if (ball.getType() == BombBall::type) { Vec2 emptySpace; //Basically a placeholder because this will only be used in case of bomb balls, but hit requires a vec2 even if one is not used. ball.Hit(emptySpace); PlayerHitByBall(nullptr, &ball); ((Game_Scene*)(this->getParent()->getParent()))->SeeSaw(this, -4); } else if (ball.getType() == WalletBall::type) { Vec2 emptySpace; //Basically a placeholder because this will only be used in case of bomb balls, but hit requires a vec2 even if one is not used. ball.Hit(emptySpace); ((Game_Scene*)(this->getParent()->getParent()))->SeeSaw(this, 4); AudioHelper::PlayRandom("Moneyball", 2); } else { float difficulty = 0.1f; // 0=easy, 1=hard float dy = toBall.y / r; // -1 -> 1 dy = (dy > 0 ? 1 : -1) * pow(abs(cbrtf(dy)), (1.0f - difficulty)); // cubic curve, harder to get y just right dy = (dy + 1) / 2.0f; // 0 -> 1 for lerp Vec2 hitDir = ccpLerp(Vec2(Settings::horizontalSpeed, -250), Vec2(Settings::horizontalSpeed, 550), dy); // lerp between mim/max hit strength ball.Hit(hitDir); } AudioHelper::PlayRandom("bathitting", 3, 0.75 + rand_0_1() * 0.5); } } } }
void CCLayerPanZoom::update(float dt) { if(_dragInertia) { _dragInertiaCurrentTime += dt; float amount = clampf(_dragInertiaCurrentTime / _kDragInertiaDuration, 0, 1); CCPoint currentDragInertia = ccpLerp( _dragInertiaDir, ccp(0,0), amount ); move(currentDragInertia); if(_dragInertiaCurrentTime > _kDragInertiaDuration) { _dragInertia = false; _dragInertiaCurrentTime = 0; } } }
void BattleScene::moveCamera(float dt) {//auto move the camera to the heroes //log("moveCamera"); if (camera == nullptr) return; auto cameraPosition = camera->getPosition(); Vec2 focusPoint; if (GameMaster::getPlayer() != nullptr) focusPoint = GameMaster::getPlayer()->getPosition(); else focusPoint = getFocusPointOfHeros(); if (HeroManager.size() > 0) { auto temp = ccpLerp(cameraPosition, ccp(focusPoint.x + _cameraOffset.x, focusPoint.y + _cameraOffset.y), 2 * dt); auto position = Vec3(temp.x, temp.y, _cameraOffset.z); camera->setPosition3D(position); camera->lookAt(Vec3(position.x, focusPoint.y, 50.0), Vec3(0.0, 0.0, 1.0)); } }
/// // Update does the work of mapping the texture onto the triangles // It now doesn't occur the cost of free/alloc data every update cycle. // It also only changes the percentage point but no other points if they have not // been modified. // // It now deals with flipped texture. If you run into this problem, just use the // sprite property and enable the methods flipX, flipY. /// void CCProgressTimer::updateRadial() { if (!m_pSprite) { return; } float alpha = m_fPercentage / 100.f; float angle = 2.f*((float)M_PI) * ( m_bReverseDirection ? alpha : 1.0f - alpha); // We find the vector to do a hit detection based on the percentage // We know the first vector is the one @ 12 o'clock (top,mid) so we rotate // from that by the progress angle around the m_tMidpoint pivot CCPoint topMid = ccp(m_tMidpoint.x, 1.f); CCPoint percentagePt = ccpRotateByAngle(topMid, m_tMidpoint, angle); int index = 0; CCPoint hit = CCPoint::zero; if (alpha == 0.f) { // More efficient since we don't always need to check intersection // If the alpha is zero then the hit point is top mid and the index is 0. hit = topMid; index = 0; } else if (alpha == 1.f) { // More efficient since we don't always need to check intersection // If the alpha is one then the hit point is top mid and the index is 4. hit = topMid; index = 4; } else { // We run a for loop checking the edges of the texture to find the // intersection point // We loop through five points since the top is split in half float min_t = FLT_MAX; for (int i = 0; i <= kProgressTextureCoordsCount; ++i) { int pIndex = (i + (kProgressTextureCoordsCount - 1))%kProgressTextureCoordsCount; CCPoint edgePtA = boundaryTexCoord(i % kProgressTextureCoordsCount); CCPoint edgePtB = boundaryTexCoord(pIndex); // Remember that the top edge is split in half for the 12 o'clock position // Let's deal with that here by finding the correct endpoints if(i == 0){ edgePtB = ccpLerp(edgePtA, edgePtB, 1-m_tMidpoint.x); } else if(i == 4){ edgePtA = ccpLerp(edgePtA, edgePtB, 1-m_tMidpoint.x); } // s and t are returned by ccpLineIntersect float s = 0, t = 0; if(ccpLineIntersect(edgePtA, edgePtB, m_tMidpoint, percentagePt, &s, &t)) { // Since our hit test is on rays we have to deal with the top edge // being in split in half so we have to test as a segment if ((i == 0 || i == 4)) { // s represents the point between edgePtA--edgePtB if (!(0.f <= s && s <= 1.f)) { continue; } } // As long as our t isn't negative we are at least finding a // correct hitpoint from m_tMidpoint to percentagePt. if (t >= 0.f) { // Because the percentage line and all the texture edges are // rays we should only account for the shortest intersection if (t < min_t) { min_t = t; index = i; } } } } // Now that we have the minimum magnitude we can use that to find our intersection hit = ccpAdd(m_tMidpoint, ccpMult(ccpSub(percentagePt, m_tMidpoint),min_t)); } // The size of the vertex data is the index from the hitpoint // the 3 is for the m_tMidpoint, 12 o'clock point and hitpoint position. bool sameIndexCount = true; if(m_nVertexDataCount != index + 3){ sameIndexCount = false; CC_SAFE_FREE(m_pVertexData); m_nVertexDataCount = 0; } if(!m_pVertexData) { m_nVertexDataCount = index + 3; m_pVertexData = (ccV2F_C4B_T2F*)malloc(m_nVertexDataCount * sizeof(ccV2F_C4B_T2F)); CCAssert( m_pVertexData, "CCProgressTimer. Not enough memory"); } updateColor(); if (!sameIndexCount) { // First we populate the array with the m_tMidpoint, then all // vertices/texcoords/colors of the 12 'o clock start and edges and the hitpoint m_pVertexData[0].texCoords = textureCoordFromAlphaPoint(m_tMidpoint); m_pVertexData[0].vertices = vertexFromAlphaPoint(m_tMidpoint); m_pVertexData[1].texCoords = textureCoordFromAlphaPoint(topMid); m_pVertexData[1].vertices = vertexFromAlphaPoint(topMid); for(int i = 0; i < index; ++i){ CCPoint alphaPoint = boundaryTexCoord(i); m_pVertexData[i+2].texCoords = textureCoordFromAlphaPoint(alphaPoint); m_pVertexData[i+2].vertices = vertexFromAlphaPoint(alphaPoint); } } // hitpoint will go last m_pVertexData[m_nVertexDataCount - 1].texCoords = textureCoordFromAlphaPoint(hit); m_pVertexData[m_nVertexDataCount - 1].vertices = vertexFromAlphaPoint(hit); }