bool CsgDisk::intersects(const Vec2f &vertice) const { if(!intersectsBBox(vertice)) return false; Vec2f transformedVertice = T_Inverted() * vertice; return transformedVertice.distance(Origin()) <= 0.5f; }
void Ball::draw( const gl::VboMesh &mesh, bool useMotionBlur ) { // store the current modelview matrix gl::pushModelView(); if(useMotionBlur) { // determine the number of balls that make up the motion blur trail (minimum of 3, maximum of 30) float trailsize = math<float>::clamp( math<float>::floor(mPrevPosition.distance(mPosition)), 3.0f, 30.0f ); float segments = trailsize - 1.0f; // draw ball with motion blur (using additive blending) gl::color( mColor / trailsize ); Vec2f offset(0.0f, 0.0f); for(size_t i=0;i<trailsize;++i) { Vec2f difference = mPrevPosition.lerp( i / segments, mPosition ) - offset; offset += difference; gl::translate( difference ); gl::draw( mesh ); } } else { // draw ball without motion blur gl::color( mColor ); gl::translate( mPosition ); gl::draw( mesh ); } // restore the modelview matrix gl::popModelView(); // mHasBeenDrawn = true; }
void Attractor::clicked( const Vec2f& mouse ) { float d = mouse.distance( location ); if (d < mass) { dragging = true; dragOffset = location - mouse; } }
void Ball::draw( const gl::VboMesh &mesh, bool useMotionBlur ) { float trailsize = math<float>::clamp( math<float>::floor(mPrevPosition.distance(mPosition)), 3.0f, 30.0f ); float segments = trailsize - 1.0f; gl::pushModelView(); if(useMotionBlur) { // draw ball with motion blur (using additive blending) gl::color( mColor / trailsize ); Vec2f offset(0.0f, 0.0f); for(size_t i=0;i<trailsize;++i) { Vec2f difference = mPrevPosition.lerp( i / segments, mPosition ) - offset; offset += difference; gl::translate( difference ); gl::draw( mesh ); } } else { // draw ball without motion blur gl::color( mColor ); gl::translate( mPosition ); gl::draw( mesh ); } gl::popModelView(); // mHasBeenDrawn = true; }
//* This only mostly works float Camera::getScreenRadius( const Sphere &sphere, float screenWidth, float screenHeight ) const { Vec2f screenCenter( worldToScreen( sphere.getCenter(), screenWidth, screenHeight ) ); Vec3f orthog = mViewDirection.getOrthogonal().normalized(); Vec2f screenPerimeter = worldToScreen( sphere.getCenter() + sphere.getRadius() * orthog, screenWidth, screenHeight ); return screenPerimeter.distance( screenCenter ); }
void PhotoBoothApp::touchesBegan( TouchEvent event ){ TouchEvent::Touch touch = event.getTouches().front(); Vec2f cameraButtonTargetPos = Vec2f(mCameraButtonPos.value()); float touchX = touch.getX() / DISPLAY_SCALE; float touchY = touch.getY() / DISPLAY_SCALE; switch(mCurrentState) { case STATE_PREVIEW: // see if the camera icon has been tapped (touch coordinates are reversed for landscape mode) cameraButtonTargetPos.x += mCameraButtonTexture.getWidth() / 2.0f; cameraButtonTargetPos.y += mCameraButtonTexture.getHeight() / 2.0f; if( cameraButtonTargetPos.distance( Vec2f(touchX, touchY) ) < (mCameraButtonTexture.getWidth() * 2) ) { mCountDownStartTime = getElapsedSeconds(); mCurrentState = STATE_COUNT_DOWN; } break; case STATE_COUNT_DOWN: // stub.. break; case STATE_ACCEPT: if(touchY > 1280) { // only look for touches near the bottom of the screen. // just split the screen in half, no need to do precise hit detection for save/cancel buttons.. if(touchX > width / 2){ ip::flipVertical( &mCameraSurface ); cinder::cocoa::SafeUiImage img = cocoa::createUiImage( mCameraSurface ); // Call into objective C to do upload via cocoa FileSender::sendFile(img); timeline().apply( &mPreviewTexturePos, Vec2f(0, -height ), 1.0f, EaseInCubic() ); }else{ timeline().apply( &mPreviewTexturePos, Vec2f(0, height ), 1.0f, EaseInBack() ); } mCurrentState = STATE_PREVIEW; timeline().apply( &mDarkBgAlpha, 0.0f, 1.0f, EaseInCubic() ); // Hide buttons timeline().apply( &mDiscardPos, Vec2f(100, height + 100 ), 1.0f, EaseInCubic() ); timeline().apply( &mSavePos, Vec2f(width-700, height + 100 ), 1.0f, EaseInCubic() ); } break; } }
void Attractor::hover( const Vec2f& mouse ) { float d = mouse.distance( location ); if (d < mass) { rollover = true; } else { rollover = false; } }
bool isInCircle(Circle* circ, Vec2f click) { int distance = click.distance(circ->pos_); if(distance<circ->radius_) { return true; } return false; }
void GLTrianglesApp::mouseDown(MouseEvent event) { // Compute normalized mouse position: mMouseAdjustedPos = static_cast<Vec2f>( event.getPos() ) / static_cast<Vec2f>( getWindowSize() ); // Compute mouse distance from triangle strip vertices: size_t tCountA = mTriangleStrip.size(); float tBestDistA = 1e6; int tBestA = -1; // for(size_t i = 0; i < tCountA; i++) { float tDistA = mMouseAdjustedPos.distance( mTriangleStrip[ i ] ); if( tDistA < tBestDistA ) { tBestDistA = tDistA; tBestA = i; } } // Compute mouse distance from triangle vertices: size_t tCountB = mTriangles.size(); float tBestDistB = 1e6; int tBestB = -1; // for(size_t i = 0; i < tCountB; i++) { float tDistB = mMouseAdjustedPos.distance( mTriangles[ i ] ); if( tDistB < tBestDistB ) { tBestDistB = tDistB; tBestB = i; } } // Check whether mouse is closer to triangle strip or triangles // and set member flag/index accordingly: if( tBestDistA < tBestDistB ) { mBestIsA = true; mBestIdx = tBestA; } else { mBestIsA = false; mBestIdx = tBestB; } }
// Check if a point is within the ball at the end of the chain // If so, set dragged = true; void Chain::contains( Vec2f pos ) { // float d = dist(x,y,tail.x,tail.y); float d = pos.distance( mTail->getPosition() ); if( d < mRadius ) { mOffset.x = mTail->getPosition().x - pos.x; mOffset.y = mTail->getPosition().y - pos.y; mTail->makeFixed(); mDragged = true; } }
int Plaaster::shoot(Vec3f pos) { int count=0; for (int i=0;i< triangles.size();i++) { if (triangles[i]->center.x > pos.x -70 && triangles[i]->center.x < pos.x +70 ) { if (triangles[i]->center.z > pos.y -70 && triangles[i]->center.z < pos.y +70 ) { Vec2f a = Vec2f(triangles[i]->center.x ,triangles[i]->center.z); float dist =a.distance(Vec2f(pos.x,pos.y )); Vec2f dir =a-Vec2f(pos.x,pos.y ); dir.normalize(); //if (dist < 70) { count ++; PlaasterParticle *p = triangles[i]->particle; p->reset(100-dist ,dir); particles.push_back(p); triangles.erase(triangles.begin()+i ); // cinder::app::console()<< "delete"; } i--; } } } //cinder::app::console()<< count <<endl; ; updateMainMesh(); return count; }
// A method that calculates a steering vector towards a target Vec2f Boid::steer(Vec2f target) { Vec2f steer; // The steering vector Vec2f desired = target - loc; // A vector pointing from the location to the target float d = target.distance(loc); // Distance from the target is the magnitude of the vector // If the distance is greater than 0, calc steering (otherwise return zero vector) if (d > 0) { desired /= d; // Normalize desired desired *= maxspeed; // Steering = Desired minus Velocity steer = desired - vel; steer.x = clamp(steer.x, -maxforce, maxforce); // Limit to maximum steering force steer.y = clamp(steer.y, -maxforce, maxforce); } return steer; }
bool Ball::isCollidingWith(BallRef other) { // this is a simplification: there is a change we will miss the collision return (mPosition.distance( other->mPosition ) < (2 * RADIUS)); }
void ResizerGameApp::draw() { // clear out the window with black gl::clear( Color( .1, .1, .1 ) ); gl::enableAlphaBlending(); float tSecs = getElapsedSeconds(); int tFrames = getElapsedFrames(); int tWinW = getWindowWidth(); int tWinH = getWindowHeight(); int tMaxEnemies = 30; float tClosest = 1000; float tEnemySpeed = constrain(float(tFrames) / 1000.0 + 1.0, 1.0, 8.0); float tEnemyCurve = abs(sin(tFrames * .001)) * 270; float tEnemyDistance = 70; tEnemyDistance -= constrain(tFrames * .01, 20.0, 70.0); float tEnemyCurve2 = abs(cos(tFrames * .001)) * 270; float tEnemyDistance2 = 90; tEnemyDistance2 -= constrain(tFrames * .01, 15.0, 90.0); int tPlayerY = constrain(tWinW - 300, 0, tWinH); int tPlayerX = 70; Vec2f player = Vec2f(tPlayerX, tPlayerY); float health = 10; // draw the score Vec2f tSize = Vec2f( tWinW, 100 ); Font tFont = Font( "Arial", 12 ); string txt = "Score: " + std::to_string(tFrames); TextBox tbox = TextBox().alignment( TextBox::RIGHT ).font( tFont ).size( Vec2i( tSize.x, TextBox::GROW ) ).text( txt ); gl::color( .8, .8, .8 ); gl::Texture tTextTexture = gl::Texture( tbox.render() ); if( tTextTexture ) gl::draw( tTextTexture ); cout << "Score: " << tFrames << "\n"; // draw enemies gl::color(.8, .1, .1); for (int i = 0; i < tMaxEnemies; ++i){ float tX = ( 600 - ( tFrames % 600 ) * tEnemySpeed ) - ( sin(i * .5) * tEnemyCurve ) + 300; float tY = sin( tSecs ) * 20 + i * tEnemyDistance; float tX2 = tX - 50; float tY2 = sin( tSecs + .5) * 20 + i * tEnemyDistance; gl::drawVector( Vec3f(tX, tY, 0), Vec3f(tX2, tY2, 0), 10, 5 ); float tDist = min( player.distance( Vec2f(tX2, tY2)), player.distance( Vec2f(tX, tY) ) ); if ( tDist < tClosest ) tClosest = tDist; } // wave2 if (tFrames > 2000) { for (int i = 0; i < tMaxEnemies; ++i){ float tX = ( 700 - ( tFrames % 700 ) * tEnemySpeed ) - ( sin(i * .5) * tEnemyCurve2 ) + 300; float tY = cos( tSecs ) * 20 + i * tEnemyDistance2; float tX2 = tX - 50; float tY2 = cos( tSecs + .5) * 20 + i * tEnemyDistance2; tX += 300; tX2 += 300; gl::drawVector( Vec3f(tX, tY, 0), Vec3f(tX2, tY2, 0), 10, 5 ); float tDist = min( player.distance( Vec2f(tX2, tY2)), player.distance( Vec2f(tX, tY) ) ); if ( tDist < tClosest ) tClosest = tDist; } } // player color gl::color( .1, .8, .1 ); // animate player if (tFrames > 100) { health += sin(tSecs) * 3; } // flash yellow if almost hit if ( tClosest < health + 5 ) { gl::color( 1, 1, 0 ); } // quit if hit if ( tClosest <= health) { quit(); } // draw the player gl::drawSolidCircle(player, health); }
float Boid::dist(Vec2f v1,Vec2f v2) { return v1.distance(v2); }
void GLBoxSelector::processMouse(QMouseEvent *me) { #define GRAB_POS_INVALID() (grabPos.x < 0.0f) #define GRAB_POS_INVALIDATE() (grabPos.x = grabPos.y = -1.f) const bool leftButIsDown = me->buttons()&Qt::LeftButton; const float thresh = wc2gl(Vec2i(10,viewport.v4)).magnitude(); Vec2f c1(box.x,box.y), c2(box.v3,box.y), c3(box.v3,box.v4), c4(box.x,box.v4); Vec2f pos = wc2gl(Vec2i(me->x(),me->y())); const bool movingBox = leftButIsDown && !GRAB_POS_INVALID() && glw->cursor().shape() == Qt::ClosedHandCursor; if (!leftButIsDown) GRAB_POS_INVALIDATE(), grabCorners = 0; glw->unsetCursor(); if (!movingBox && !grabCorners) { if (pos.distance(c2) <= thresh) // bottom right glw->setCursor(Qt::SizeFDiagCursor), grabCorners = 1<<2; else if (pos.distance(c4) <= thresh) // top left glw->setCursor(Qt::SizeFDiagCursor), grabCorners = 1<<4; else if (pos.distance(c1) <= thresh) // bottom left glw->setCursor(Qt::SizeBDiagCursor), grabCorners = 1<<1; else if (pos.distance(c3) <= thresh) // top right corner glw->setCursor(Qt::SizeBDiagCursor), grabCorners = 1<<3; else if (pos.x >= c1.x && pos.x <= c2.x) { if (fabsf(pos.y-c1.y) <= thresh) // bottom side of box glw->setCursor(Qt::SizeVerCursor), grabCorners = (1<<1)|(1<<2); else if (fabsf(pos.y-c3.y) <= thresh) // top side of box glw->setCursor(Qt::SizeVerCursor), grabCorners = (1<<3)|(1<<4); } if (!grabCorners && pos.y >= c1.y && pos.y <= c3.y) { if (fabsf(pos.x-c1.x) <= thresh) // left side of box glw->setCursor(Qt::SizeHorCursor), grabCorners = (1<<1)|(1<<4); else if (fabsf(pos.x-c3.x) <= thresh) // right side of box glw->setCursor(Qt::SizeHorCursor), grabCorners = (1<<2)|(1<<3); } if (!grabCorners && !(pos.x <= c1.x) && !(pos.x >= c3.x) && !(pos.y <= c1.y) && !(pos.y >= c3.y)) { glw->setCursor(leftButIsDown ? Qt::ClosedHandCursor : Qt::OpenHandCursor); if (leftButIsDown) grabPos = pos; } if (!leftButIsDown) grabCorners = 0; } else if (movingBox && !grabCorners) { // movingBox! glw->setCursor(Qt::ClosedHandCursor); if (leftButIsDown && !GRAB_POS_INVALID()) { Vec2f movvec = pos-grabPos; grabPos = grabPos + movvec; box.v1 += movvec.x; box.v2 += movvec.y; box.v3 += movvec.x; box.v4 += movvec.y; } if(box.v1 < 0.f) box.v3 += -box.v1, box.v1=0.f; else if (box.v3 > 1.f) box.v1 -= box.v3-1.f, box.v3 = 1.f; else if (box.v2 < 0.f) box.v4 += -box.v2, box.v2 = 0.f; else if (box.v4 > 1.f) box.v2 -= box.v4-1.f, box.v4 = 1.f; } else if (!movingBox && grabCorners) { const bool movc1 = (grabCorners&(1<<1)), movc2 = (grabCorners&(1<<2)), movc3 = (grabCorners&(1<<3)), movc4 = (grabCorners&(1<<4)); if (movc1 && movc2) { // dragging bottom side of box glw->setCursor(Qt::SizeVerCursor); c1.y = c2.y = pos.y; if (c1.y > 1.f) c1.y = c2.y = 1.f; if (c1.y < 0.f) c1.y = c2.y = 0.f; if (c1.y >= c3.y) grabCorners = (1<<3)|(1<<4); } else if (movc4 && movc3) { // top line of box is dragging glw->setCursor(Qt::SizeVerCursor); c4.y = c3.y = pos.y; if (c4.y > 1.f) c4.y = c3.y = 1.f; if (c4.y < 0.f) c4.y = c3.y = 0.f; if (c4.y <= c1.y) grabCorners = (1<<1)|(1<<2); } else if (movc2 && movc3) { // right side of box is dragging glw->setCursor(Qt::SizeHorCursor); c2.x = c3.x = pos.x; if (c3.x > 1.f) c3.x = c2.x = 1.f; if (c3.x < 0.f) c3.x = c2.x = 0.f; if (c3.x <= c1.x) grabCorners = (1<<1)|(1<<4); } else if (movc1 && movc4) { // left side of box is dragging glw->setCursor(Qt::SizeHorCursor); c1.x = c4.x = pos.x; if (c1.x > 1.f) c1.x = c4.x = 1.f; if (c1.x < 0.f) c1.x = c4.x = 0.f; if (c1.x >= c2.x) grabCorners = (1<<2)|(1<<3); } else if (movc1) { glw->setCursor(Qt::SizeBDiagCursor); c1 = pos; if (c1.x < 0) c1.x = 0; if (c1.y < 0) c1.y = 0; if (c1.x > 1.f) c1.x = 1.f; if (c1.y > 1.f) c1.y = 1.f; c4.x = c1.x; c2.y = c1.y; if (c1.x >= c3.x && c1.y >= c3.y) grabCorners = (1<<3); else if (c1.x >= c2.x) grabCorners = (1<<2); else if (c1.y >= c4.y) grabCorners = (1<<4); } else if (movc3) { glw->setCursor(Qt::SizeBDiagCursor); c3 = pos; if (c3.x < 0) c3.x = 0; if (c3.y < 0) c3.y = 0; if (c3.x > 1.f) c3.x = 1.f; if (c3.y > 1.f) c3.y = 1.f; c4.y = c3.y; c2.x = c3.x; if (c1.x >= c3.x && c1.y >= c3.y) grabCorners = (1<<1); else if (c3.x <= c4.x) grabCorners = (1<<4); else if (c3.y <= c2.y) grabCorners = (1<<2); } else if (movc2) { glw->setCursor(Qt::SizeFDiagCursor); c2 = pos; if (c2.x < 0) c2.x = 0; if (c2.y < 0) c2.y = 0; if (c2.x > 1.f) c2.x = 1.f; if (c2.y > 1.f) c2.y = 1.f; c1.y = c2.y; c3.x = c2.x; if (c2.x >= c4.x && c2.y >= c4.y) grabCorners = (1<<4); else if (c1.x >= c2.x) grabCorners = (1<<1); else if (c3.y <= c2.y) grabCorners = (1<<3); } else if (movc4) { glw->setCursor(Qt::SizeFDiagCursor); c4 = pos; if (c4.x < 0) c4.x = 0; if (c4.y < 0) c4.y = 0; if (c4.x > 1.f) c4.x = 1.f; if (c4.y > 1.f) c4.y = 1.f; c1.x = c4.x; c3.y = c4.y; if (c2.x >= c4.x && c2.y >= c4.y) grabCorners = (1<<2); else if (c3.x <= c4.x) grabCorners = (1<<3); else if (c1.y >= c4.y) grabCorners = (1<<1); } box = Vec4f ( MIN(MIN(c1.x,c2.x),MIN(c3.x,c4.x)), MIN(MIN(c1.y,c2.y),MIN(c3.y,c4.y)), MAX(MAX(c1.x,c2.x),MAX(c3.x,c4.x)), MAX(MAX(c1.y,c2.y),MAX(c3.y,c4.y)) ); } chkBoxSanity(); }