bool Ball::isCollided(Paddle &paddle) { const sf::Vector2f polarPos = toPolar(getPosition() - Settings::getScreenCenter()); const float ballRad = ((sf::CircleShape*)shape)->getRadius(); const float closeRad = 0.01 * Settings::getZoneRadius(); if ((polarPos.x + ballRad) > (paddle.getRadius() - closeRad)) { const float ballRadAngle = toPolar(sf::Vector2f(-ballRad, polarPos.x)).y; const float closeAngle = 1.0; const float paddleCenter = paddle.getCenterAngle(); if ( ((polarPos.y + ballRadAngle) > (paddleCenter - paddle.getAngularLength() - closeAngle)) && ((polarPos.y - ballRadAngle) < (paddleCenter + paddle.getAngularLength() + closeAngle)) ) { // ball is close to paddle, so bother to do checks const int NUM_POINTS = ((sf::CircleShape*)shape)->getPointCount(); for (int i = 0; i<NUM_POINTS; i++) { // check for every point on the circle shape, in global coords sf::Vector2f localPoint = ((sf::CircleShape*)shape)->getPoint(i); sf::Transform transform = shape->getTransform(); sf::Vector2f globalPoint = transform.transformPoint(localPoint); if (paddle.isWithin(globalPoint)) { return true; } } } } return false; }
void PuzzleApp::update() { if (_hands[LEFT]->state != Hand::NORMAL && _hands[RIGHT]->state != Hand::NORMAL) { Vec2f diff = _hands[RIGHT]->pos - _hands[LEFT]->pos; Vec2f polar = toPolar(diff); console()<<polar.x<<" "<<polar.y<<endl; float theta = polar.y;//[0, PI_two) clockwise float sign = 0.0f; if (theta > Pi) theta -= Pi; if (theta < Pi_half) { sign = theta*0.5f; } else if (theta > Pi_half) { theta = Pi - theta; sign = -theta*0.5f; } _rotate = sign; float loBound = 150; float hiBound = getWindowWidth(); _scale = lmap(polar.x, loBound, hiBound, minScale, maxScale); _scale = constrain(_scale, minScale, maxScale); } StateMachine::update(); }
void Ball::bounce(float offset, sf::Vector2f polarCollisionPoint) { // move ball outside paddle, to where it would be if // bounce happened immediately sf::Vector2f polarPos = toPolar(getPosition() - Settings::getScreenCenter()); const float ballRad = ((sf::CircleShape*)shape)->getRadius(); const float paddleRad = polarCollisionPoint.x; sf::Vector2f bouncedPos( 2*paddleRad - polarPos.x - 2 * ballRad, polarPos.y); shape->setPosition(toCartesian(bouncedPos) + Settings::getScreenCenter()); // invert direction, then modify according to skew // factor and offset sf::Vector2f polarVel = toPolar(velocity); float angleOfIncidence = polarCollisionPoint.y - polarVel.y; if (angleOfIncidence > 90) { angleOfIncidence = angleOfIncidence - 180; } else if (angleOfIncidence < -90) { angleOfIncidence = angleOfIncidence + 180; } float reflectionAngle = 180 - 2*angleOfIncidence; float skewReflectionAngle = reflectionAngle + skewFactor * offset; if ((reflectionAngle <=180 &&skewReflectionAngle < reflectionAngle/2) || (reflectionAngle > 180 && (180 -skewReflectionAngle) > (180 -reflectionAngle/2)) ) { skewReflectionAngle = reflectionAngle/2; } // reflect and skew polarVel.y -= skewReflectionAngle; mutex.lock(); velocity = toCartesian(polarVel); mutex.unlock(); }
bool Ball::hasScored() { sf::Vector2i center (Settings::getScreenResolution()/2); mutex.lock(); sf::Vector2f pos = shape->getPosition(); mutex.unlock(); int courtRadius = Settings::getZoneRadius(); float ballRadius = toPolar(sf::Vector2f(pos.x - center.x, pos.y - center.y)).x; return ballRadius > courtRadius; }
bool Sprite::isPointInside( const Vec2f& pt ) { Vec2f polar = toPolar(pt - _center); polar.y -= toRadians(_degree); Vec2f local = fromPolar(polar); if (local.x > -_size.x/2 && local.x < _size.x/2 && local.y > -_size.y/2 && local.y < _size.y/2) return true; else return false; }
double StationKeep::getRange(const IMC::EstimatedState* state) const { double lat = state->lat; double lon = state->lon; Coordinates::toWGS84(*state, lat, lon); double x; double y; WGS84::displacement(lat, lon, 0, m_lat, m_lon, 0, &x, &y); // dummy variable double bearing; double range; toPolar(x, y, &bearing, &range); return range; }