bool PixelPerfectTest(const sf::Sprite& Object1, const sf::Sprite& Object2, sf::Uint8 AlphaLimit) { sf::FloatRect Intersection; if (Object1.getGlobalBounds().intersects(Object2.getGlobalBounds(), Intersection)) { sf::IntRect O1SubRect = Object1.getTextureRect(); sf::IntRect O2SubRect = Object2.getTextureRect(); sf::Uint8* mask1 = Bitmasks.GetMask(Object1.getTexture()); sf::Uint8* mask2 = Bitmasks.GetMask(Object2.getTexture()); // Loop through our pixels for (int i = Intersection.left; i < Intersection.left+Intersection.width; i++) { for (int j = Intersection.top; j < Intersection.top+Intersection.height; j++) { sf::Vector2f o1v = Object1.getInverseTransform().transformPoint(i, j); sf::Vector2f o2v = Object2.getInverseTransform().transformPoint(i, j); // Make sure pixels fall within the sprite's subrect if (o1v.x > 0 && o1v.y > 0 && o2v.x > 0 && o2v.y > 0 && o1v.x < O1SubRect.width && o1v.y < O1SubRect.height && o2v.x < O2SubRect.width && o2v.y < O2SubRect.height) { if (Bitmasks.GetPixel(mask1, Object1.getTexture(), (int)(o1v.x)+O1SubRect.left, (int)(o1v.y)+O1SubRect.top) > AlphaLimit && Bitmasks.GetPixel(mask2, Object2.getTexture(), (int)(o2v.x)+O2SubRect.left, (int)(o2v.y)+O2SubRect.top) > AlphaLimit) return true; } } } } return false; }
void afficheConstructible (sf::RenderWindow &App, sf::Sprite &SpriteTower ,sf::Shape &PolygonPorte,Constructible* maCons,int cote, int offSetX, int offSetY ) { // affichage du sprite de la construction int i = maCons->getPosition()->getI(); int j = maCons->getPosition()->getJ(); drawHexagoneIJ ( i,j, SpriteTower, cote, offSetX, offSetY); //SpriteTower.SetCenter(SpriteTower.GetSize().x/2,SpriteTower.GetSize().y/2); sf::Vector2f positionAvant = SpriteTower.getPosition(); SpriteTower.setPosition(positionAvant.x-SpriteTower.getTextureRect().height,positionAvant.y-SpriteTower.getTextureRect().width); App.draw(SpriteTower); // affichage de la porté std::set<Case *> cibles = maCons->getCaseAPorteDEffet(); std::set<Case *>::iterator its; for ( its= cibles.begin() ; its != cibles.end() ; ++its ) { drawHexagoneIJ ( (*its)->getI() , (*its)->getJ(), PolygonPorte, cote, offSetX, offSetY); App.draw(PolygonPorte); } // les point de vue ( mon dieu c'est moche le code graphique ) double centreX = SpriteTower.getTextureRect().width /2 ;// SpriteTower.getTexture().getSize().x/2; double centreY = SpriteTower.getTextureRect().height/2 ; /*sf::Shape pv = sf::Shape::Line (0,0, 0+maCons->getPV()/2 ,0,5,sf::Color::Black); drawHexagoneIJ ( i,j, pv, cote, offSetX, offSetY); pv.Move(0-centreX,0-centreY); App.draw(pv);*/ }
void Animation::SetSprite(const sf::Sprite& sprite, const int frameCount) { m_frameWidth = sprite.getTextureRect().width/frameCount; m_frameHeight = sprite.getTextureRect().height; m_sprite = sprite; m_frameCount = frameCount; Reset(); }
KeyObject::KeyObject(float x, float y, sf::Sprite sprite, sf::Color outline, sf::String word, key_callback_t callback, bool active, bool visible) : GameObject(x, y, (float)sprite.getTexture()->getSize().x, (float)sprite.getTexture()->getSize().y), outline_(outline), word_(word), active_(active), focused_(false), visible_(visible), callback_(callback), state_(DEFAULT) { this->rect_.width = sprite.getTextureRect().width; this->rect_.height = sprite.getTextureRect().height; SetSprite(sprite); }
sf::Vector2f getCenter(const sf::Sprite& sprite) { sf::Vector2f center = sprite.getPosition(); center.x += sprite.getTextureRect().width / 2.f; center.y += sprite.getTextureRect().height / 2.f; return center; }
void _Log::load_image(){ if( image_loaded == false && image_path.length() != 0 ) { texture_1.loadFromFile( image_path ); Sprite.setTexture( texture_1, false ); sf::IntRect spr_data = Sprite.getTextureRect(); float x,y = WINDOW_HEIGHT - spr_data.height; switch( align ){ case 1: x = 0.f; break; case 2: x = WINDOW_WIDTH * 0.25 - spr_data.width /2; break; case 3: x = WINDOW_WIDTH * 0.5 - spr_data.width /2; break; case 4: x = WINDOW_WIDTH * 0.75 - spr_data.width /2; break; case 5: x = WINDOW_WIDTH - spr_data.width; break; } Sprite.setPosition( x, y ); cout << x << " " << y << endl; image_loaded = true; } }
bool testForStanding(sf::Sprite sp2) { if ((sp2.getGlobalBounds().left+sp2.getGlobalBounds().width > sprite.getPosition().x) && (sp2.getGlobalBounds().left < sprite.getPosition().x+sprite.getTextureRect().width*sprite.getScale().x)) { return true; } return false; }
sf::Vector2f Collision2D::GetSize(const sf::Sprite& sprite) { // Return the size of the sprite adjusting for scale const sf::IntRect iOriginalSize = sprite.getTextureRect(); const sf::Vector2f fScale = sprite.getScale(); return sf::Vector2f(iOriginalSize.width * fScale.x, iOriginalSize.height * fScale.y); }
bool ImageButton(const sf::Sprite& sprite, const sf::Vector2f& size, const int framePadding, const sf::Color& bgColor, const sf::Color& tintColor) { const sf::Texture* texturePtr = sprite.getTexture(); if (!texturePtr) { return false; } return ::imageButtonImpl(*texturePtr, static_cast<sf::FloatRect>(sprite.getTextureRect()), size, framePadding, bgColor, tintColor); }
void resize(sf::Sprite& sprite, float width, float height) { int local_width = sprite.getTextureRect().width; int local_height = sprite.getTextureRect().height; if ((local_width > 0) && (local_height > 0)) sprite.setScale(width / local_width, height / local_height); }
void Graphics::draw(sf::Vector2f position, sf::Vector2f hitbox, sf::Sprite sprite) { sprite.setOrigin(0, hitbox.y); sf::Vector2f roundedPosition = sf::Vector2f(position.x - 0.5, position.y - 0.5); //Translate position translatePosition(roundedPosition, window_); //Set Position sprite.setPosition(roundedPosition); //Scale if necessary sprite.scale(hitbox.x / sprite.getTextureRect().width, hitbox.y / sprite.getTextureRect().height); window_->draw(sprite); return; }
SpriteAnimator::SpriteAnimator(sf::Sprite &sprite) : Animator() , invertXOffset(false) , invertYOffset(false) , sprite(sprite) , sourceRectangle(sprite.getTextureRect()) { }
OrientedBoundingBox (const sf::Sprite& Object) // Calculate the four points of the OBB from a transformed (scaled, rotated...) sprite { sf::Transform trans = Object.getTransform(); sf::IntRect local = Object.getTextureRect(); Points[0] = trans.transformPoint(0.f, 0.f); Points[1] = trans.transformPoint(local.width, 0.f); Points[2] = trans.transformPoint(local.width, local.height); Points[3] = trans.transformPoint(0.f, local.height); }
void Image(const sf::Sprite& sprite, const sf::Vector2f& size, const sf::Color& tintColor, const sf::Color& borderColor) { const sf::Texture* texturePtr = sprite.getTexture(); // sprite without texture cannot be drawn if (!texturePtr) { return; } Image(*texturePtr, size, static_cast<sf::FloatRect>(sprite.getTextureRect()), tintColor, borderColor); }
// The flip function works well TODO MOVE THIS SHIT void Player::Flip(sf::Sprite& sprite) { if (Player::Direction==-1) { sprite.setTextureRect( sf::IntRect( sprite.getTextureRect().width, //TODO left+width? 0, -sprite.getTextureRect().width, sprite.getTextureRect().height ) ); } else { sprite.setTextureRect(Player::TextureRect); } }
void KeyObject::SetSprite(sf::Sprite sprite) { sprite_ = sprite; sf::RenderTexture temp; temp.create(sprite.getTextureRect().width, sprite.getTextureRect().height); temp.draw(sprite); sf::Image outline_image = temp.getTexture().copyToImage(); outline_image.flipVertically(); sf::Vector2u size = outline_image.getSize(); sf::Image new_image; new_image.create(size.x, size.y, sf::Color::Transparent); int i, j, min_i, max_i, min_j, max_j, local_i, local_j; int radius = 7; for (i = 0; i < size.x; ++i) { for (j = 0; j < size.y; ++j) { min_i = std::max(0, i - radius); max_i = std::min(size.x, (unsigned) i + radius); min_j = std::max(0, j - radius); max_j = std::min(size.y, (unsigned) j + radius); float strength = 0; for (local_i = min_i; local_i < max_i; ++local_i) { for (local_j = min_j; local_j < max_j; ++local_j) { if (outline_image.getPixel(local_i, local_j).a > 50) { float distSquared = std::pow(i - local_i, 2.f) + std::pow(j - local_j, 2.f); strength += 1.f / std::pow(distSquared, 1.5f); } } } if (strength > 0) { outline_.a = std::min(255.f, 255.f * 1.5f * strength); new_image.setPixel(i, j, outline_); } } } texture_outline_.loadFromImage(new_image); sprite_outline_.setTexture(texture_outline_, true); }
void Sprite2VertexArray(const sf::Sprite & sprite,sf::VertexArray & vertices) { sf::IntRect texBox = sprite.getTextureRect(); sf::FloatRect box = sprite.getGlobalBounds(); vertices.append(sf::Vertex(sf::Vector2f(box.left, box.top), sf::Vector2f((float)texBox.left, (float)texBox.top))); vertices.append(sf::Vertex(sf::Vector2f(box.left, box.top + box.height), sf::Vector2f((float)texBox.left, (float)(texBox.top + texBox.height)))); vertices.append(sf::Vertex(sf::Vector2f(box.left + box.width, box.top + box.height), sf::Vector2f((float)(texBox.left + texBox.width), (float)(texBox.top + texBox.height)))); vertices.append(sf::Vertex(sf::Vector2f(box.left + box.width, box.top), sf::Vector2f((float)(texBox.left + texBox.width), (float)texBox.top))); }
void Animation::get_sprite(int frame, sf::Sprite& sprite) const { if ( d->tileset ) { d->tileset->get_tile(d->start_tile+frame,sprite); sf::IntRect ir = sprite.getTextureRect(); ir.left += d->offset.x; ir.top += d->offset.y; sprite.setTextureRect(ir); } }
//otestad (har inga vettiga cirklar) att testa med) bool CollisionManager::Circle_CircleCollision(const sf::Sprite& sprite1, const sf::Sprite& sprite2) { // avståndet mellan cirklarnas mittpunkter sf::Vector2f distance = sf::Vector2f(sprite1.getGlobalBounds().left + sprite1.getGlobalBounds().width / 2.0f, sprite1.getGlobalBounds().top + sprite1.getGlobalBounds().height / 2.0f) - sf::Vector2f(sprite2.getGlobalBounds().left + sprite2.getGlobalBounds().width / 2.0f, sprite2.getGlobalBounds().top + sprite2.getGlobalBounds().height / 2.0f); float magnitudeOfDistanceSquared = distance.x * distance.x + distance.y * distance.y; // beräkna radien på båda objekten float radius1 = (sprite1.getTextureRect().width * sprite1.getScale().x + sprite1.getTextureRect().height * sprite1.getScale().y)/4; float radius2 = (sprite2.getTextureRect().width * sprite2.getScale().x + sprite2.getTextureRect().height * sprite2.getScale().y)/4; float maximumCollidingDistanceBetweenBoundings = (radius1 + radius1) * (radius2 + radius2); return (magnitudeOfDistanceSquared <= maximumCollidingDistanceBetweenBoundings); }
bool DebugHud::Button(sf::RenderWindow* window, sf::Sprite buttonSprite) { //get width/height int width = buttonSprite.getTextureRect().width; int height = buttonSprite.getTextureRect().height; float x = buttonSprite.getPosition().x ; float y = buttonSprite.getPosition().y ; //text mouse sf::Vector2i mousePos = sf::Mouse::getPosition(*window); if (mousePos.x < x) return false; if (mousePos.y < y) return false; if (mousePos.x > x + width) return false; if (mousePos.y > y + height) return false; buttonSprites[3].setPosition(x - width /4,y - height / 2); window->draw(buttonSprites[3]); return sf::Mouse::isButtonPressed(sf::Mouse::Button::Left); }
void Animation::create(const sf::Sprite& spritesheet, const sf::IntRect spriteArea, sf::Time duration) { // We get the first's frame rect _frameRect = spriteArea; // The number of sprites there is on this area _numberFrames = spritesheet.getTextureRect().width / spriteArea.width; // Setting the texture to own it _sprite.setTexture(*spritesheet.getTexture()); _sprite.setTextureRect(spriteArea); // the duration of the animation _duration = duration; // we can say the animation is ready _isLoaded = true; }
static std::vector<sf::Sprite> split4(sf::Sprite sprite) { std::vector<sf::Sprite> splits = std::vector<sf::Sprite>(0); const sf::Texture* texture = sprite.getTexture(); sf::IntRect baseRect = sprite.getTextureRect(); sf::Vector2f position = sprite.getPosition(); splits.push_back(sf::Sprite(*texture, sf::IntRect(baseRect.left,baseRect.top,baseRect.width/2,baseRect.height/2))); splits[0].setPosition(position.x,position.y); splits[0].setColor(sprite.getColor()); splits.push_back(sf::Sprite(*texture, sf::IntRect(baseRect.left+(baseRect.width/2), baseRect.top, baseRect.width/2, baseRect.height/2))); splits[1].setPosition(position.x+baseRect.width/2,position.y); splits[1].setColor(sprite.getColor()); splits.push_back(sf::Sprite(*texture, sf::IntRect(baseRect.left, baseRect.top+(baseRect.height/2), baseRect.width/2 , baseRect.height/2))); splits[2].setPosition(position.x,position.y+baseRect.height/2); splits[2].setColor(sprite.getColor()); splits.push_back(sf::Sprite(*texture, sf::IntRect(baseRect.left+(baseRect.width/2), baseRect.top+(baseRect.height/2), baseRect.width/2 , baseRect.height/2))); splits[3].setPosition(position.x+baseRect.width/2,position.y+baseRect.height/2); splits[3].setColor(sprite.getColor()); return splits; }
sf::Vector2f GetSpriteSize (const sf::Sprite& Object) { sf::IntRect OriginalSize = Object.getTextureRect(); sf::Vector2f Scale = Object.getScale(); return sf::Vector2f (OriginalSize.width*Scale.x, OriginalSize.height*Scale.y); }
/** Affiche le monstre la ou il doit etre sur la case, avec le bon angle et la bonne position affiche également les points de vie du moob */ void afficheMonstre (sf::RenderWindow &App, sf::Sprite &SpriteMoob ,Monstre* monMoob,int cote, int offSetX, int offSetY , std::list<Tour*> &listTour) { using namespace std; Case* caseMoob = monMoob->CaseCourante(); drawHexagoneIJ ( caseMoob->getI() , caseMoob->getJ(), SpriteMoob, cote, offSetX, offSetY); //sf::Shape pv = sf::RectangleShape (0,0, 0+monMoob->getPV()/2 ,0,5,sf::Color::Black); sf::RectangleShape pv (sf::Vector2f(5,monMoob->getPV()/2)); pv.setFillColor(sf::Color::Black); /// Moche drawHexagoneIJ ( caseMoob->getI() , caseMoob->getJ(), pv, cote, offSetX, offSetY); // on centre le moob int etape; Direction dtion = monMoob->quelleDirection(etape); // on bouge en fonction de la direction double moobOffSetX=0,moobOffSetY=0,moobAvancement; if ( etape == 0) { moobAvancement = ((1000-monMoob->getAvancement())/1000)*cote*(sqrt(3)); } else { moobAvancement = ((500-monMoob->getAvancement())/1000)*cote*(sqrt(3)); } double angle = 0; switch (dtion) { case Est : { moobOffSetX = moobAvancement; moobOffSetY =0; angle = -90; if ( etape == 0) { moobOffSetX -= (sqrt(3)/2) *cote; } break;} case NordEst : { moobOffSetX = moobAvancement / 2 ; moobOffSetY = -moobAvancement * (sqrt(3)/2) ; if ( etape == 0) { moobOffSetX -= (sqrt(3)/2) *cote /2; moobOffSetY += (sqrt(3)/2) *cote *(sqrt(3)/2); } angle = -30; break;} case NordOuest : { moobOffSetX = -moobAvancement / 2 ; moobOffSetY = -moobAvancement * (sqrt(3)/2) ; if ( etape == 0) { moobOffSetX += (sqrt(3)/2) *cote /2; moobOffSetY += (sqrt(3)/2) *cote *(sqrt(3)/2); } angle = 30; break;} case Ouest : { moobOffSetX = - moobAvancement; moobOffSetY = 0; if ( etape == 0) { moobOffSetX += (sqrt(3)/2) *cote; } angle = 90; break;} case SudOuest : { angle = 160; moobOffSetX = -moobAvancement / 2 ; moobOffSetY = moobAvancement * (sqrt(3)/2) ; if ( etape == 0) { moobOffSetX += (sqrt(3)/2) *cote /2; moobOffSetY -= (sqrt(3)/2) *cote *(sqrt(3)/2); } break;} case SudEst: { angle = -160; moobOffSetX = moobAvancement / 2 ; moobOffSetY = moobAvancement * (sqrt(3)/2) ; if ( etape == 0) { moobOffSetX -= (sqrt(3)/2) *cote /2; moobOffSetY -= (sqrt(3)/2) *cote *(sqrt(3)/2); } break;} } SpriteMoob.move(moobOffSetX,moobOffSetY); //double centreX = SpriteMoob.getSize().x/2; // double centreY = SpriteMoob.getSize().y/2 ; double centreX = SpriteMoob.getTextureRect().width /2 ;// SpriteTower.getTexture().getSize().x/2; double centreY = SpriteMoob.getTextureRect().height/2 ; SpriteMoob.setPosition(centreX,centreY); SpriteMoob.rotate(angle); pv. move(moobOffSetX-centreX,moobOffSetY-centreY); App.draw(pv); App.draw(SpriteMoob); //la gestion de l'attaque n'est pas encore definie if ( monMoob != NULL ) { using namespace std; Case* laCase = monMoob->CaseCourante(); if (laCase != NULL ) { std::list<Tour*>::iterator itt = listTour.begin(); for ( itt; itt != listTour.end() ; ++itt) { if ( (*itt)->getCible() == monMoob) { sf::Vector2<int> ij; sf::Vector2<double>xy; ij.x = (*itt)->getPosition()->getI(); ij.y = (*itt)->getPosition()->getJ(); xy = IJtoXY (ij, cote, offSetX, offSetY); // sf::Shape Line = sf::Shape::Line(SpriteMoob.GetPosition().x,SpriteMoob.GetPosition().y,xy.x,xy.y, 2, sf::Color::Red); // App.Draw(Line); } } } } }
bool CollisionManager::Circle_WallCollision(const sf::Sprite& sprite) { float radius = (sprite.getTextureRect().width * sprite.getScale().x + sprite.getTextureRect().height * sprite.getScale().y)/6; sf::Vector2f coordinates = sf::Vector2f(sprite.getGlobalBounds().left + sprite.getGlobalBounds().width / 2.0f, sprite.getGlobalBounds().top + sprite.getGlobalBounds().height / 2.0f); //hämta antalet väggar att kolla for (int i = 0; i < m_wallManager->GetCount(); i++) { //hämta antalet punkter i en convex form for (int j = 0; j < m_wallManager->GetWall(i)->getPointCount(); j++) { //hämta startpunkten sf::Vector2f A = m_wallManager->GetWall(i)->getPosition() + m_wallManager->GetWall(i)->getPoint(j); //hämta slutpunkten sf::Vector2f B; if (j==0) { B = m_wallManager->GetWall(i)->getPosition() + m_wallManager->GetWall(i)->getPoint(m_wallManager->GetWall(i)->getPointCount()-1); } else { B = m_wallManager->GetWall(i)->getPosition() + m_wallManager->GetWall(i)->getPoint(j-1); } sf::Vector2f C = coordinates; //räkna ut avstånden mellan punkterna sf::Vector2f f = A - C; sf::Vector2f t = B - C; sf::Vector2f l = t - f; float a = l.x * l.x + l.y * l.y ; float b = 2*(l.x * f.x + l.y * f.y) ; float c = f.x * f.x + f.y * f.y - radius*radius ; float discriminant = b*b-4*a*c; if( discriminant < 0 ) { continue; } else { discriminant = sqrt( discriminant ); // cirkeln kan penetreras på två olika punkter, ingångspunkten och utgångspunkten float t1 = (-b - discriminant)/(2*a); float t2 = (-b + discriminant)/(2*a); // om t1 och t2 är mellan 0 och 1 har linjen gått igenom cirkeln // om t1 eller t2 är mellan 0 och 1 har linjen gått in i men inte igenom cirkeln if( t1 >= 0 && t1 <= 1 ) { return true ; } if( t2 >= 0 && t2 <= 1 ) { return true ; } //om t1 och t2 är större än 1 eller mindre än 0 har linjen missat cirkeln helt } } } //har den gått igenom alla former och kommit till slutet har ingen kollision sket mellan några punkter return false; }
MainMenu() : system(TextureManager::instance().getTexture("assets/rticle.png")) { pointer.setTexture(TextureManager::instance().getTexture("assets/two_pointer.png")); //pointer.setPosition(1183, 427); //system = new thor::ParticleSystem(); emitter.setEmissionRate(0.5f); emitter.setParticleLifetime( sf::seconds(40) ); emitter.setParticleRotation( thor::Distributions::uniform(0.f, 360.f) ); emitter.setParticlePosition( sf::Vector2f(1260,740) ); emitter.setParticleScale(sf::Vector2f(0.1,0.1)); system.addEmitter(thor::refEmitter(emitter)); thor::ColorGradient gradient; gradient[0.0f] = sf::Color::Magenta; gradient[0.1f] = sf::Color::Red; gradient[0.2f] = sf::Color::Blue; gradient[0.3f] = sf::Color::Cyan; gradient[0.4f] = sf::Color::Green; gradient[0.5f] = sf::Color::Red; gradient[0.6f] = sf::Color::Magenta; gradient[0.7f] = sf::Color::Cyan; gradient[0.8f] = sf::Color::Red; gradient[0.9f] = sf::Color::Blue; gradient[1.0f] = sf::Color::Red; thor::ColorAnimation colorizer(gradient); thor::FadeAnimation fader(0.1f, 0.1f); system.addAffector( thor::ScaleAffector(sf::Vector2f(0.1f,0.1))); system.addAffector( thor::AnimationAffector(fader) ); system.addAffector( thor::AnimationAffector(colorizer) ); sprite = AnimatedSprite(sf::seconds(0.3)); v.setTexture(TextureManager::instance().getTexture("assets/one_upper.png")); back2.setTexture(TextureManager::instance().getTexture("assets/two_menu.png")); logo.setTexture(TextureManager::instance().getTexture("assets/one_logo.png")); press.setTexture(TextureManager::instance().getTexture("assets/one_anybutton.png")); anim.setSpriteSheet(TextureManager::instance().getTexture("assets/one_animooted.jpg")); for (int i = 0; i < 5; i++) { anim.addFrame(sf::IntRect(0, 800 * i, WIDTH, HEIGHT)); } sprite.setAnimation(anim); sprite.setLooped(true); sprite.play(); logo.setColor(sf::Color(255,255,255,0)); press.setColor(sf::Color(255,255,255,0)); pointer.setColor(sf::Color(255,255,255,0)); press.setPosition(WIDTH/2 - press.getTextureRect().width/2, HEIGHT/2 - press.getTextureRect().height/2 + 100); pressFading = true; newLogoY = 800; oTweener.addTween(&CDBTweener::TWEQ_BACK, CDBTweener::TWEA_INOUT, 2.f, &newLogoY, 0.0f); first = true; pressFactor = 0.2; transition = false; check = false; sm.init(sf::Vector2f(WIDTH, HEIGHT)); rt.create(WIDTH, HEIGHT); MusicManager::instance().playMusicFast("low"); selection = 1; };
sf::Vector2f size(const sf::Sprite& sprite) { const sf::IntRect& rect = sprite.getTextureRect(); return sf::Vector2f(rect.width * sprite.getScale().x, rect.height * sprite.getScale().y); }
bool CollisionManager::Circle_FurnitureCollision(const sf::Sprite& player, const Furniture& furniture) { if(furniture.GetNoise() == 0) { return false; } float radius = (player.getTextureRect().width * player.getScale().x + player.getTextureRect().height * player.getScale().y)/6; sf::Vector2f coordinates = sf::Vector2f(player.getGlobalBounds().left + player.getGlobalBounds().width / 2.0f, player.getGlobalBounds().top + player.getGlobalBounds().height / 2.0f); //hämta antalet punkter i en convex form for (int j = 0; j < furniture.getPointCount(); j++) { float m_degree = furniture.getRotation(); //hämta startpunkten sf::Vector2f A_point = furniture.getPoint(j) - furniture.getOrigin(); //hämta slutpunkten sf::Vector2f B_point; if (j==0) { B_point = furniture.getPoint(furniture.getPointCount()-1) - furniture.getOrigin(); } else { B_point = furniture.getPoint(j-1) - furniture.getOrigin(); } sf::Vector2f newCirclePosition; newCirclePosition.x = B_point.x * cosf(m_degree * (M_PI / 180)) - B_point.y * sinf(m_degree * (M_PI / 180)); newCirclePosition.y = B_point.x * sinf(m_degree * (M_PI / 180)) + B_point.y * cosf(m_degree * (M_PI / 180)); B_point = newCirclePosition; newCirclePosition.x = A_point.x * cosf(m_degree * (M_PI / 180)) - A_point.y * sinf(m_degree * (M_PI / 180)); newCirclePosition.y = A_point.x * sinf(m_degree * (M_PI / 180)) + A_point.y * cosf(m_degree * (M_PI / 180)); A_point = newCirclePosition; sf::Vector2f A = furniture.getPosition() + A_point; sf::Vector2f B = furniture.getPosition() + B_point; sf::Vector2f C = coordinates; //räkna ut avstånden mellan punkterna sf::Vector2f f = A - C; sf::Vector2f t = B - C; sf::Vector2f l = t - f; float a = l.x * l.x + l.y * l.y ; float b = 2*(l.x * f.x + l.y * f.y) ; float c = f.x * f.x + f.y * f.y - radius*radius ; float discriminant = b*b-4*a*c; if( discriminant < 0 ) { continue; } else { discriminant = sqrt( discriminant ); // cirkeln kan penetreras på två olika punkter, ingångspunkten och utgångspunkten float t1 = (-b - discriminant)/(2*a); float t2 = (-b + discriminant)/(2*a); // om t1 och t2 är mellan 0 och 1 har linjen gått igenom cirkeln // om t1 eller t2 är mellan 0 och 1 har linjen gått in i men inte igenom cirkeln if( t1 >= 0 && t1 <= 1 ) { return true ; } if( t2 >= 0 && t2 <= 1 ) { return true ; } //om t1 och t2 är större än 1 eller mindre än 0 har linjen missat cirkeln helt } } //har den gått igenom alla former och kommit till slutet har ingen kollision sket mellan några punkter return false; }