void EvilQueenBlade::update(float dt){ if(oldPos.x == this->getPosition().x || oldPos.y == this->getPosition().y) return; float angle = atan2f(-this->getPosition().y + oldPos.y, this->getPosition().x - oldPos.x); if(angle == 0) return; this->setRotation(MATH_RAD_TO_DEG(angle)); oldPos = this->getPosition(); }
float OppAttack::poleAngle(Node* pole) { Node* firstMan = pole->getFirstChild(); Vector3 difference = firstMan->getTranslationWorld() - pole->getTranslationWorld(); difference.y = 0; difference.normalize(); return MATH_RAD_TO_DEG(atan2(difference.z, difference.x)); }
void InputLayer::stickHandle(Vec2 &pos, InputType inputType) { auto localPos = convertToNodeSpace(pos); switch (inputType) { case INPUT_BEGAN: { _prevPos = localPos; _imgStickBg->runAction(FadeIn::create(0.3)); _imgStick->runAction(FadeIn::create(0.3)); _pnlStick->setPosition(localPos); break; } case INPUT_MOVED: { auto moveVector = localPos - _prevPos; auto moveAngle = moveVector.getAngle(); auto moveDegree = MATH_RAD_TO_DEG(moveAngle); auto moveLength = moveVector.getLength(); //CCLOG("moveDegree : %f moveLength : %f", moveDegree, moveLength); if (moveLength > STICK_MAX_R) { auto rate = STICK_MAX_R / moveLength; moveVector = moveVector * rate; } auto oldPos = _imgStick->getPosition(); _imgStick->setPosition(moveVector); if (_target) { if (moveDegree >= -45 && moveDegree < 45) { _target->right(); } else if (moveDegree >= 45 && moveDegree < 135) { _target->up(); } else if (moveDegree >= -135 && moveDegree < -45) { _target->down(); } else { _target->left(); } } break; } case INPUT_ENDED: { _imgStickBg->runAction(FadeOut::create(0.3)); _imgStick->runAction(FadeOut::create(0.3)); _imgStick->setPosition(Vec2::ZERO); if (_target) { _target->none(); } break; } case INPUT_CANCELLED: { /* _imgStickBg->runAction(FadeOut::create(0.3)); _imgStick->runAction(FadeOut::create(0.3)); _imgStick->setPosition(Vec2::ZERO); if (_target) { _target->none(); } */ break; } default: break; } }
void SpaceshipGame::update(long elapsedTime) { // Calculate elapsed time in seconds float t = (float)elapsedTime / 1000.0; if (!_finished) { _time += t; // Play the background track if (_backgroundSound->getState() != AudioSource::PLAYING) _backgroundSound->play(); } else { // Stop the background track if (_backgroundSound->getState() != AudioSource::STOPPED) _backgroundSound->stop(); _throttle = 0.0f; } // Set initial force due to gravity _force.set(0, -GRAVITATIONAL_FORCE); // While we are pushing/touching the screen, apply a push force vector based on the distance from // the touch point to the center of the space ship. if (_pushing) { // Get the center point of the space ship in screen coordinates Vector3 shipCenterScreen; _scene->getActiveCamera()->project(getViewport(), _shipGroupNode->getBoundingSphere().center, &shipCenterScreen.x, &shipCenterScreen.y); // Compute a screen-space vector between the center point of the ship and the touch point. // We will use this vector to apply a "pushing" force to the space ship, similar to what // happens when you hold a magnet close to an object with opposite polarity. Vector2 pushForce((shipCenterScreen.x - _pushPoint.x), -(shipCenterScreen.y - _pushPoint.y)); // Transform the vector so that a smaller magnitude emits a larger force and applying the // maximum touch distance. float distance = (std::max)(TOUCH_DISTANCE_MAX - pushForce.length(), 0.0f); pushForce.normalize(); pushForce.scale(distance * FORCE_SCALE); _force.add(pushForce); // Compute a throttle value based on our force vector, minus gravity Vector2 throttleVector(_force.x, _force.y + GRAVITATIONAL_FORCE); _throttle += throttleVector.length() / FORCE_MAX * t; } else { // Gradually decrease the throttle if (_throttle > 0.0f) { _throttle *= 1.0f - t; } } // Clamp the throttle _throttle = CLAMP(_throttle, 0.0f, 1.0f); // Update acceleration (a = F/m) _acceleration.set(_force.x / MASS, _force.y / MASS); // Update velocity (v1 = v0 + at) _velocity.x += _acceleration.x * t; _velocity.y += _acceleration.y * t; // Clamp velocity to its maximum range _velocity.x = CLAMP(_velocity.x, -VELOCITY_MAX, VELOCITY_MAX); _velocity.y = CLAMP(_velocity.y, -VELOCITY_MAX, VELOCITY_MAX); // Move the spaceship based on its current velocity (x1 = x0 + vt) _shipGroupNode->translate(_velocity.x * t, _velocity.y * t, 0); // Check for collisions handleCollisions(t); // Update camera updateCamera(); // Reset ship rotation _shipGroupNode->setRotation(_initialShipRot); // Apply ship tilt if (_force.x != 0 && abs(_velocity.x) > 0.1f) { // Compute an angle based on the dot product between the force vector and the Y axis Vector2 fn; _force.normalize(&fn); float angle = MATH_RAD_TO_DEG(acos(Vector2::dot(Vector2(0, 1), fn))); if (_force.x > 0) angle = -angle; angle *= _throttle * t; _shipTilt += angle; _shipTilt = _shipTilt < -SHIP_TILT_MAX ? -SHIP_TILT_MAX : (_shipTilt > SHIP_TILT_MAX ? SHIP_TILT_MAX : _shipTilt); } else { // Interpolate tilt back towards zero when no force is applied _shipTilt = (_shipTilt + ((0 - _shipTilt) * t * 2.0f)); } _shipGroupNode->rotateZ(MATH_DEG_TO_RAD(_shipTilt)); if (_throttle > MATH_EPSILON) { // Apply ship spin _shipNode->rotateY(MATH_DEG_TO_RAD(SHIP_ROTATE_SPEED_MAX * t * _throttle)); // Play sound effect if (_spaceshipSound->getState() != AudioSource::PLAYING) _spaceshipSound->play(); // Set the pitch based on the throttle _spaceshipSound->setPitch(_throttle * SOUND_PITCH_SCALE); } else { // Stop sound effect _spaceshipSound->stop(); } // Modify ship glow effect based on the throttle _glowDiffuseParameter->setValue(Vector4(1, 1, 1, _throttle * ENGINE_POWER)); _shipSpecularParameter->setValue(SPECULAR - ((SPECULAR-2.0f) * _throttle)); }
virtual float getRotationZ() const override { return MATH_RAD_TO_DEG(_body->GetAngle()); }
void ParticlesGame::saveFile() { std::string filename; filename = FileSystem::displayFileDialog(FileSystem::SAVE, "Save Particle File", "Particle Files", "particle", "res"); if (filename.length() == 0) return; ParticleEmitter* e = _particleEmitter; // Extract just the particle name from the filename std::string dir = FileSystem::getDirectoryName(filename.c_str()); std::string ext = FileSystem::getExtension(filename.c_str()); std::string name = filename.substr(dir.length(), filename.length() - dir.length() - ext.length()); Texture* texture = e->getTexture(); std::string texturePath = texture->getPath(); std::string textureDir = FileSystem::getDirectoryName(texturePath.c_str()); texturePath = texturePath.substr(textureDir.length()); // Get camera rotation as axis-angle Vector3 cameraAxis; float cameraAngle = MATH_RAD_TO_DEG(_cameraParent->getRotation().toAxisAngle(&cameraAxis)); // Write out a properties file std::ostringstream s; s << "particle " << name << "\n" << "{\n" << " sprite\n" << " {\n" << " path = " << texturePath << "\n" << " width = " << e->getSpriteWidth() << "\n" << " height = " << e->getSpriteHeight() << "\n" << " blending = " << toString(e->getTextureBlending()) << "\n" << " animated = " << toString(e->isSpriteAnimated()) << "\n" << " looped = " << toString(e->isSpriteLooped()) << "\n" << " frameCount = " << e->getSpriteFrameCount() << "\n" << " frameRandomOffset = " << e->getSpriteFrameRandomOffset() << "\n" << " frameDuration = " << e->getSpriteFrameDuration() << "\n" << " }\n" << "\n" << " particleCountMax = " << e->getParticleCountMax() << "\n" << " emissionRate = " << e->getEmissionRate() << "\n" << " ellipsoid = " << toString(e->isEllipsoid()) << "\n" << " orbitPosition = " << toString(e->getOrbitPosition()) << "\n" << " orbitVelocity = " << toString(e->getOrbitVelocity()) << "\n" << " orbitAcceleration = " << toString(e->getOrbitAcceleration()) << "\n" << " sizeStartMin = " << e->getSizeStartMin() << "\n" << " sizeStartMax = " << e->getSizeStartMax() << "\n" << " sizeEndMin = " << e->getSizeEndMin() << "\n" << " sizeEndMax = " << e->getSizeEndMax() << "\n" << " energyMin = " << e->getEnergyMin() << "\n" << " energyMax = " << e->getEnergyMax() << "\n" << " colorStart = " << toString(e->getColorStart()) << "\n" << " colorStartVar = " << toString(e->getColorStartVariance()) << "\n" << " colorEnd = " << toString(e->getColorEnd()) << "\n" << " colorEndVar = " << toString(e->getColorEndVariance()) << "\n" << " position = " << toString(e->getPosition()) << "\n" << " positionVar = " << toString(e->getPositionVariance()) << "\n" << " velocity = " << toString(e->getVelocity()) << "\n" << " velocityVar = " << toString(e->getVelocityVariance()) << "\n" << " acceleration = " << toString(e->getAcceleration()) << "\n" << " accelerationVar = " << toString(e->getAccelerationVariance()) << "\n" << " rotationPerParticleSpeedMin = " << e->getRotationPerParticleSpeedMin() << "\n" << " rotationPerParticleSpeedMax = " << e->getRotationPerParticleSpeedMax() << "\n" << "\n" << " editor\n" << " {\n" << " cameraTranslation = " << toString(_cameraParent->getTranslation()) << "\n" << " cameraZoom = " << toString(_scene->getActiveCamera()->getNode()->getTranslation()) << "\n" << " cameraRotation = " << toString(cameraAxis) << ", " << cameraAngle << "\n" << " sizeMax = " << _startMax->getMax() << "\n" << " energyMax = " << _energyMax->getMax() << "\n" << " }\n" "}\n"; std::string text = s.str(); Stream* stream = FileSystem::open(filename.c_str(), FileSystem::WRITE); stream->write(text.c_str(), 1, text.length()); stream->close(); SAFE_DELETE(stream); }