void _Log::load_image(){ if( image_loaded == false && image_path.length() != 0 ) { Sprite.SetImage( VNImageManager.get_image( image_path ) ); Sprite.SetY( WINDOW_HEIGHT - Sprite.GetSize().y ); switch( align ){ case 1: Sprite.SetX( 0.f ); break; case 2: Sprite.SetX( 100.f ); break; case 3: Sprite.SetX( WINDOW_WIDTH/2 - Sprite.GetSize().x /2 ); break; case 4: Sprite.SetX( WINDOW_WIDTH - 100.f - Sprite.GetSize().x ); break; case 5: Sprite.SetX( WINDOW_WIDTH - Sprite.GetSize().x ); break; } image_loaded = true; } }
bool Bullet::detectCollision(sf::Vector2f p, const sf::Sprite& sprite) const { /* collide if the pixel is not transparent */ int img_width = sprite.GetSize().x, img_height = sprite.GetSize().y; int img_x = pos.x - p.x + img_width / 2, img_y = pos.y - p.y + img_height / 2; if (img_x < 0 || img_x >= img_width) return false; if (img_y < 0 || img_y >= img_height) return false; return sprite.GetPixel(img_x, img_y).a != 0; }
bool Collision::CircleTest(const sf::Sprite& Object1, const sf::Sprite& Object2) { //Simplest circle test possible //Distance between points <= sum of radius float Radius1 = (Object1.GetSize().x + Object1.GetSize().y) / 4; float Radius2 = (Object2.GetSize().x + Object2.GetSize().y) / 4; float xd = Object1.GetPosition().x - Object2.GetPosition().x; float yd = Object1.GetPosition().y - Object2.GetPosition().y; return sqrt(xd * xd + yd * yd) <= Radius1 + Radius2; }
void ImageManager::GetSpriteEnemy(sf::Sprite &sprite, int whichEnemy) { if(whichEnemy == ENEMY1) { sprite.SetImage(m_enemy1); sprite.SetSubRect(sf::IntRect(1, 1, sprite.GetSize().x-1, sprite.GetSize().y-1));//To do avec le charset } else std::cerr << "erreur fonction ImageManager.GetSpriteEnemy appelée avec whichEnemy = " << whichEnemy << std::endl; }
bool Mouse::isInside(sf::Sprite &sprite) { float x1, x2, y1, y2; x1 = sprite.GetPosition().x; x2 = x1 + sprite.GetSize().x; y1 = sprite.GetPosition().y; y2 = y1 + sprite.GetSize().y; if ((this->x < x2) && (this->x > x1)) { if ((this->y < y2) && (this->y > y1)) { return true; } } return false; }
bool sourisAuDessusBoutons(sf::Event &Event, sf::Sprite &Bouton) { //test la position actuelle du curseur ainsi que sa position future //si le curseur est au-dessus du sprite, alors on l'affiche ! if((Event.MouseMove.X < Bouton.GetPosition().x+Bouton.GetSize().x && Event.MouseMove.X>Bouton.GetPosition().x && Event.MouseMove.Y<Bouton.GetPosition().y+Bouton.GetSize().y && Event.MouseMove.Y>Bouton.GetPosition().y) || (Event.MouseButton.X < Bouton.GetPosition().x+Bouton.GetSize().x && Event.MouseButton.X>Bouton.GetPosition().x && Event.MouseButton.Y<Bouton.GetPosition().y+Bouton.GetSize().y && Event.MouseButton.Y>Bouton.GetPosition().y)) { return true; } else return false; }
bool Utility::contains(sf::Sprite sp, int x, int y){ float posX1 = sp.GetPosition().x - sp.GetSize().x/2; float posY1 = sp.GetPosition().y - sp.GetSize().y/2; //sf::Vector2f center = sp.GetCenter(); float posX2 = sp.GetPosition().x + sp.GetSize().x/2; float posY2 = sp.GetPosition().y + sp.GetSize().y/2; //sf::Vector2f //if(s if(x > posX1 && x < posX2 && y > posY1 && y < posY2) return true; return false; }
Bala::Bala(float _v, sf::Sprite& _cannon, sf::Image& imagen) { float cannonA, cannonX, cannonY, cannonH; //Cargo sprite sprite.SetImage(imagen); sprite.SetCenter(imagen.GetWidth()/2, imagen.GetHeight()/2); //Inicializo variables dt = 0.0f; cannonA = _cannon.GetRotation(); cannonX = _cannon.GetPosition().x; cannonY = _cannon.GetPosition().y; //Tomo el tamaño sobre el eje x como el largo del cañon //porque sin rotar la misma se encuentra en horizontal cannonH = _cannon.GetSize().x; //Descompongo la velocidad inicial vx = _v * (cos(cannonA/180*PI)); vy = _v * (sin(cannonA/180*PI)); //Posicion inicial bala según la posición del cañon //usando trigonometria xi = cannonX + ((cos(cannonA/180*PI) * cannonH)); yi = cannonY - ((sin(cannonA/180*PI) * cannonH)); sprite.SetPosition(xi, yi); }
void init(){ _backgroundImage.LoadFromFile("assets/wood-puzzle-floor.png"); _backgroundSprite.SetImage(_backgroundImage); _robotImage.LoadFromFile("assets/roomba.png"); _robotSprite.SetImage(_robotImage); _robotSprite.SetCenter(_robotSprite.GetSize().x/2, _robotSprite.GetSize().y/2); _fakeRobotSprite.SetImage(_robotImage); _fakeRobotSprite.SetCenter(_fakeRobotSprite.GetSize().x/2, _fakeRobotSprite.GetSize().y/2); _fakeRobotSprite.SetColor(sf::Color(100,100,255)); _landmarkImage.LoadFromFile("assets/landmark.png"); _landmarkSprite.SetImage(_landmarkImage); _landmarkSprite.SetCenter(_landmarkSprite.GetSize().x/2, _landmarkSprite.GetSize().y/2); _rangeImage.LoadFromFile("assets/range.png"); _rangeSprite.SetImage(_rangeImage); original_range_sprite_size_x = _rangeSprite.GetSize().x; original_range_sprite_size_y = _rangeSprite.GetSize().y; updateRangeSpriteScale(); _rangeSprite.SetCenter((original_range_sprite_size_x/2), (original_range_sprite_size_y/2)); _window.Create(sf::VideoMode(window_width,window_height,32), "Bearing simulator"); move_amount = 0; rotation_amount = 0; time_to_capture = false; walk = 0; rotation = 0; }
//returns true if (x,y) is inside sprite fiure bool contains(float x, float y, const sf::Sprite& sprite) { float minX = sprite.GetPosition().x; float minY = sprite.GetPosition().y; float maxX = sprite.GetPosition().x + sprite.GetSize().x; float maxY = sprite.GetPosition().y + sprite.GetSize().y; //return if x and y falls in between sprite's range return isBetween(x,minX, maxX) && isBetween(y,minY, maxY); }
int main() { img_r.LoadFromFile("..\\Imagenes\\rcircle.png"); img_b.LoadFromFile("..\\Imagenes\\bcircle.png"); sprite_b.SetImage(img_b); sprite_r.SetImage(img_r); sf::RenderWindow App(sf::VideoMode(800,600,32),"ventana"); const sf::Input& in = App.GetInput(); sprite_b.Scale(sprite_r.GetSize().x/sprite_b.GetSize().x,sprite_r.GetSize().y/sprite_b.GetSize().y); while(App.IsOpened()) { while(App.GetEvent(Event)) { if(Event.Type == sf::Event::Closed) App.Close(); if(Event.Type == sf::Event::MouseMoved){ // cursor.SetPosition(Event.MouseMove.X,Event.MouseMove.Y); } } if(in.IsKeyDown(sf::Key::Escape)) App.Close(); //cursor.SetPosition(in.GetMouseX(),in.GetMouseY()); App.Clear(); sprite_r.SetPosition(0,0); App.Draw(sprite_r); sprite_r.SetPosition(App.GetWidth()-img_r.GetWidth(),0); App.Draw(sprite_r); sprite_r.SetPosition(0,App.GetHeight()-img_r.GetHeight()); App.Draw(sprite_r); sprite_r.SetPosition(App.GetWidth()-img_r.GetWidth(),App.GetHeight()-img_r.GetHeight()); App.Draw(sprite_r); sprite_b.SetPosition((App.GetWidth()-sprite_b.GetSize().x)/2,(App.GetHeight()-sprite_b.GetSize().y)/2); App.Draw(sprite_b); App.Display(); } return 0; }
bool boxCollision(const sf::Sprite& a, const sf::Sprite& b) { sf::IntRect aRect(a.GetPosition().x - a.GetSize().x/2, a.GetPosition().y - a.GetSize().y/2, a.GetPosition().x + a.GetSize().x/2, a.GetPosition().y + a.GetSize().y/2); sf::IntRect bRect(b.GetPosition().x - b.GetSize().x/2, b.GetPosition().y - b.GetSize().y/2, b.GetPosition().x + b.GetSize().x/2, b.GetPosition().y + b.GetSize().y/2); return aRect.Intersects(bRect); }
sf::IntRect Collision::GetAABB(const sf::Sprite& Object) { //Get the top left corner of the sprite regardless of the sprite's center //This is in Global Coordinates so we can put the rectangle back into the right place sf::Vector2f pos = Object.TransformToGlobal(sf::Vector2f(0, 0)); //Store the size so we can calculate the other corners sf::Vector2f size = Object.GetSize(); float Angle = Object.GetRotation(); //Bail out early if the sprite isn't rotated if (Angle == 0.0f) { return sf::IntRect(static_cast<int> (pos.x), static_cast<int> (pos.y), static_cast<int> (pos.x + size.x), static_cast<int> (pos.y + size.y)); } //Calculate the other points as vectors from (0,0) //Imagine sf::Vector2f A(0,0); but its not necessary //as rotation is around this point. sf::Vector2f B(size.x, 0); sf::Vector2f C(size.x, size.y); sf::Vector2f D(0, size.y); //Rotate the points to match the sprite rotation B = RotatePoint(B, Angle); C = RotatePoint(C, Angle); D = RotatePoint(D, Angle); //Round off to int and set the four corners of our Rect int Left = static_cast<int> (MinValue(0.0f, B.x, C.x, D.x)); int Top = static_cast<int> (MinValue(0.0f, B.y, C.y, D.y)); int Right = static_cast<int> (MaxValue(0.0f, B.x, C.x, D.x)); int Bottom = static_cast<int> (MaxValue(0.0f, B.y, C.y, D.y)); //Create a Rect from out points and move it back to the correct position on the screen sf::IntRect AABB = sf::IntRect(Left, Top, Right, Bottom); AABB.Offset(static_cast<int> (pos.x), static_cast<int> (pos.y)); return AABB; }
int main() { img_r.LoadFromFile("..\\Imagenes\\rcircle.png"); img_b.LoadFromFile("..\\Imagenes\\bcircle.png"); sprite_b.SetImage(img_b); sprite_r[0].SetImage(img_r); sprite_r[1].SetImage(img_r); sprite_r[2].SetImage(img_r); sprite_r[3].SetImage(img_r); sf::RenderWindow App(sf::VideoMode(800,600,32),"ventana"); const sf::Input& in = App.GetInput(); sprite_b.Scale(sprite_r[0].GetSize().x/sprite_b.GetSize().x,sprite_r[0].GetSize().y/sprite_b.GetSize().y); sprite_r[0].SetPosition(0,0); sprite_r[1].SetPosition(App.GetWidth()-img_r.GetWidth(),0); sprite_r[2].SetPosition(0,App.GetHeight()-img_r.GetHeight()); sprite_r[3].SetPosition(App.GetWidth()-img_r.GetWidth(),App.GetHeight()-img_r.GetHeight()); sprite_b.SetPosition((App.GetWidth()-sprite_b.GetSize().x)/2,(App.GetHeight()-sprite_b.GetSize().y)/2); int index = -1; int pos_x,pos_y; while(App.IsOpened()) { while(App.GetEvent(Event)) { if(Event.Type == sf::Event::Closed) App.Close(); if(index == -1 && Event.Type == sf::Event::MouseButtonPressed) { int x = Event.MouseButton.X; int y = Event.MouseButton.Y; int sx,sy; int w,h; for(int i=0;i<4;i++) { sx = sprite_r[i].GetPosition().x; sy = sprite_r[i].GetPosition().y; h = sprite_r[i].GetSize().y; w = sprite_r[i].GetSize().x; if(sx < x && sx + w > x && sy < y && sy + h > y) { index = i; pos_x = x; pos_y = y; break; } } } if(index > -1 && Event.Type == sf::Event::MouseButtonReleased) { index = -1; } } if(in.IsKeyDown(sf::Key::Escape)) App.Close(); if(index > -1) { int off_x = in.GetMouseX() - pos_x; int off_y = in.GetMouseY() - pos_y; pos_x = in.GetMouseX(); pos_y = in.GetMouseY(); sprite_r[index].Move(off_x,off_y); } App.Clear(); App.Draw(sprite_r[3]); App.Draw(sprite_r[2]); App.Draw(sprite_r[1]); App.Draw(sprite_r[0]); App.Draw(sprite_b); App.Display(); } return 0; }
bool Collision::collide(sf::Sprite& obj, float dx, float dy, CollisionResult& result) { //assert(_map->tileFromPixel(obj.GetPosition().x, obj.GetPosition().y)->isWalkable()); //assert(_map->tileFromPixel(obj.GetPosition().x + obj.GetSize().x, obj.GetPosition().y)->isWalkable()); std::cout << "posx: " << obj.GetPosition().x << std::endl; std::cout << "posy: " << obj.GetPosition().y << std::endl; std::cout << "dx: " << dx << std::endl; std::cout << "dy: " << dy << std::endl; result.left = false; result.right = false; result.floor = false; result.ceil = false; result.dx = dx; result.dy = dy; if(dx != 0.f) { //if moving horizontal bool left = dx < 0; float forwardX; if(left) forwardX = obj.GetPosition().x; else forwardX = obj.GetPosition().x + obj.GetSize().x; float newX = forwardX + dx; float topY = obj.GetPosition().y + constants::COLLISION_MARGIN; float botY = obj.GetPosition().y + obj.GetSize().y - constants::COLLISION_MARGIN; Tile* topTile = _map->tileFromPixel(newX, topY); Tile* botTile = _map->tileFromPixel(newX, botY); if(!topTile->isWalkable() || !botTile->isWalkable()) { result.left = left; result.right = !left; float limit; if(left) limit = topTile->GetPosition().x + topTile->GetSize().x; else limit = topTile->GetPosition().x; result.dx = limit - forwardX; } std::cout << "res.dx=" << result.dx << std::endl; assert(left || result.dx <= dx); assert(!left || result.dx >= dx); //assert(result.dx * dx >= 0); //same direction } float objPosX = obj.GetPosition().x + result.dx; if(dy != 0.f) { //if moving vertically bool down = dy > 0; float forwardY; if(down) forwardY = obj.GetPosition().y + obj.GetSize().y; else forwardY = obj.GetPosition().y; float newY = forwardY + dy; Tile* leftTileY = _map->tileFromPixel(objPosX + constants::COLLISION_MARGIN, newY); Tile* rightTileY = _map->tileFromPixel(objPosX + obj.GetSize().x - constants::COLLISION_MARGIN, newY); if(!leftTileY->isWalkable() || !rightTileY->isWalkable()) { result.floor = down; result.ceil = !down; float limit; if(down) limit = leftTileY->GetPosition().y;// - constants::COLLISION_MARGIN; else limit = leftTileY->GetPosition().y + leftTileY->GetSize().y;// + constants::COLLISION_MARGIN; result.dy = limit - forwardY; } std::cout << "res.dy=" << result.dy << std::endl; assert(down || result.dy >= dy); assert(!down || result.dy <= dy); //assert(result.dy * dy >= 0); //same direction } return result.floor || result.ceil || result.left || result.right; }
bool Table::isPointOverSprite(const sf::Vector2f Position, sf::Sprite &Sprite){ return (Position.x < Sprite.GetPosition().x + Sprite.GetSize().x) && (Sprite.GetPosition().x < Position.x) && (Position.y < Sprite.GetPosition().y + Sprite.GetSize().y) && (Sprite.GetPosition().y < Position.y); }
bool Collision::BoundingBoxTest(const sf::Sprite& Object1, const sf::Sprite& Object2) { sf::Vector2f A, B, C, BL, TR; sf::Vector2f HalfSize1 = Object1.GetSize(); sf::Vector2f HalfSize2 = Object2.GetSize(); //For somereason the Vector2d divide by operator //was misbehaving //Doing it manually HalfSize1.x /= 2; HalfSize1.y /= 2; HalfSize2.x /= 2; HalfSize2.y /= 2; //Get the Angle we're working on float Angle = Object1.GetRotation() - Object2.GetRotation(); float CosA = cos(Angle * RADIANS_PER_DEGREE); float SinA = sin(Angle * RADIANS_PER_DEGREE); float t, x, a, dx, ext1, ext2; //Normalise the Center of Object2 so its axis aligned an represented in //relation to Object 1 C = Object2.GetPosition(); C -= Object1.GetPosition(); C = RotatePoint(C, Object2.GetRotation()); //Get the Corners BL = TR = C; BL -= HalfSize2; TR += HalfSize2; //Calculate the vertices of the rotate Rect A.x = -HalfSize1.y*SinA; B.x = A.x; t = HalfSize1.x*CosA; A.x += t; B.x -= t; A.y = HalfSize1.y*CosA; B.y = A.y; t = HalfSize1.x*SinA; A.y += t; B.y -= t; t = SinA * CosA; // verify that A is vertical min/max, B is horizontal min/max if (t < 0) { t = A.x; A.x = B.x; B.x = t; t = A.y; A.y = B.y; B.y = t; } // verify that B is horizontal minimum (leftest-vertex) if (SinA < 0) { B.x = -B.x; B.y = -B.y; } // if rr2(ma) isn't in the horizontal range of // colliding with rr1(r), collision is impossible if (B.x > TR.x || B.x > -BL.x) return false; // if rr1(r) is axis-aligned, vertical min/max are easy to get if (t == 0) { ext1 = A.y; ext2 = -ext1; }// else, find vertical min/max in the range [BL.x, TR.x] else { x = BL.x - A.x; a = TR.x - A.x; ext1 = A.y; // if the first vertical min/max isn't in (BL.x, TR.x), then // find the vertical min/max on BL.x or on TR.x if (a * x > 0) { dx = A.x; if (x < 0) { dx -= B.x; ext1 -= B.y; x = a; } else { dx += B.x; ext1 += B.y; } ext1 *= x; ext1 /= dx; ext1 += A.y; } x = BL.x + A.x; a = TR.x + A.x; ext2 = -A.y; // if the second vertical min/max isn't in (BL.x, TR.x), then // find the local vertical min/max on BL.x or on TR.x if (a * x > 0) { dx = -A.x; if (x < 0) { dx -= B.x; ext2 -= B.y; x = a; } else { dx += B.x; ext2 += B.y; } ext2 *= x; ext2 /= dx; ext2 -= A.y; } } // check whether rr2(ma) is in the vertical range of colliding with rr1(r) // (for the horizontal range of rr2) return !((ext1 < BL.y && ext2 < BL.y) || (ext1 > TR.y && ext2 > TR.y)); }
void caseResolution(sf::RenderWindow &App, sf::Event &Event, sf::Sprite &Case1, sf::Sprite &Case2, sf::Sprite &Case3, sf::Sprite &Checkeur, int ¶m) { if ((Event.Type == sf::Event::MouseButtonPressed) && (Event.MouseButton.X > Case1.GetPosition().x) && (Event.MouseButton.X < Case1.GetPosition().x+Case1.GetSize().x) && (Event.MouseButton.Y > Case1.GetPosition().y) && (Event.MouseButton.Y < Case1.GetPosition().y+Case1.GetSize().y)) { Checkeur.SetX(Case1.GetPosition().x); Checkeur.SetY(Case1.GetPosition().y); } else if ((Event.Type == sf::Event::MouseButtonReleased) && (Event.MouseButton.X > Case2.GetPosition().x) && (Event.MouseButton.X < Case2.GetPosition().x+Case2.GetSize().x) && (Event.MouseButton.Y > Case2.GetPosition().y) && (Event.MouseButton.Y < Case2.GetPosition().y+Case2.GetSize().y)) { Checkeur.SetX(Case2.GetPosition().x); Checkeur.SetY(Case2.GetPosition().y); } else if ((Event.Type == sf::Event::MouseButtonReleased) && (Event.MouseButton.X > Case3.GetPosition().x) && (Event.MouseButton.X < Case3.GetPosition().x+Case3.GetSize().x) && (Event.MouseButton.Y > Case3.GetPosition().y) && (Event.MouseButton.Y < Case3.GetPosition().y+Case3.GetSize().y)) { Checkeur.SetX(Case3.GetPosition().x); Checkeur.SetY(Case3.GetPosition().y); } param=1; }
bool cliqueBouton(sf::Event &Event, sf::Sprite &BoutonJ) { if ((Event.Type == sf::Event::MouseButtonPressed) && (Event.MouseButton.X > BoutonJ.GetPosition().x) && (Event.MouseButton.X < BoutonJ.GetPosition().x+BoutonJ.GetSize().x) && (Event.MouseButton.Y > BoutonJ.GetPosition().y) && (Event.MouseButton.Y < BoutonJ.GetPosition().y+BoutonJ.GetSize().y)) return true; else return false; }