bool PPCollisionWR(Sprite Sprite1,Sprite Sprite2,float AlphaLimit = 50.f) { float xmin1 = Sprite1.GetPosition().x - Sprite1.GetCenter().x; float ymin1 = Sprite1.GetPosition().y - Sprite1.GetCenter().y; float xmin2 = Sprite2.GetPosition().x - Sprite2.GetCenter().x; float ymin2 = Sprite2.GetPosition().y - Sprite2.GetCenter().y; float xmax1 = xmin1 + Sprite1.GetSize().x; float ymax1 = ymin1 + Sprite1.GetSize().y; float xmax2 = xmin2 + Sprite2.GetSize().x; float ymax2 = ymin2 + Sprite2.GetSize().y; float xmin = max(xmin1, xmin2); float ymin = max(ymin1, ymin2); float xmax = min(xmax1, xmax2); float ymax = min(ymax1, ymax2); //if(xmax <= xmin || ymax <= ymin) { return false; } for(int y = int(ymin); y < ymax; ++y) { for(int x = int(xmin); x < xmax; ++x) { float x1 = x - xmin1, y1 = y - ymin1; float x2 = x - xmin2, y2 = y - ymin2; Vector2f Point1 = RotatePoint(Vector2f(x1,y1), Sprite1.GetCenter(), -Sprite1.GetRotation()); Vector2f Point2 = RotatePoint(Vector2f(x2,y2), Sprite2.GetCenter(), -Sprite2.GetRotation()); if(Point1.x > 0 && Point1.y > 0 && Point2.x > 0 && Point2.y > 0 && Point1.x < Sprite1.GetSize().x && Point1.y < Sprite1.GetSize().y && Point2.x < Sprite2.GetSize().x && Point2.y < Sprite2.GetSize().y) { Color color1 = Sprite1.GetPixel(int(Point1.x),int(Point1.y)); Color color2 = Sprite2.GetPixel(int(Point2.x),int(Point2.y)); if (color1.a >= AlphaLimit && color2.a >= AlphaLimit) { return true; } } } } return false; }
void Sprites::MoveSprites(float timeStep) { Graphics* graphics = GetSubsystem<Graphics>(); float width = (float)graphics->GetWidth(); float height = (float)graphics->GetHeight(); // Go through all sprites for (unsigned i = 0; i < sprites_.Size(); ++i) { Sprite* sprite = sprites_[i]; // Rotate float newRot = sprite->GetRotation() + timeStep * 30.0f; sprite->SetRotation(newRot); // Move, wrap around rendering window edges Vector2 newPos = sprite->GetPosition() + sprite->GetVar(VAR_VELOCITY).GetVector2() * timeStep; if (newPos.x_ < 0.0f) newPos.x_ += width; if (newPos.x_ >= width) newPos.x_ -= width; if (newPos.y_ < 0.0f) newPos.y_ += height; if (newPos.y_ >= height) newPos.y_ -= height; sprite->SetPosition(newPos); } }
bool PPCollision(Sprite Sprite1,Sprite Sprite2,float AlphaLimit = 50.f) { float xmin1 = Sprite1.GetPosition().x - Sprite1.GetCenter().x; float ymin1 = Sprite1.GetPosition().y - Sprite1.GetCenter().y; float xmin2 = Sprite2.GetPosition().x - Sprite2.GetCenter().x; float ymin2 = Sprite2.GetPosition().y - Sprite2.GetCenter().y; float xmax1 = xmin1 + Sprite1.GetSize().x; float ymax1 = ymin1 + Sprite1.GetSize().y; float xmax2 = xmin2 + Sprite2.GetSize().x; float ymax2 = ymin2 + Sprite2.GetSize().y; float xmin = max(xmin1, xmin2); float ymin = max(ymin1, ymin2); float xmax = min(xmax1, xmax2); float ymax = min(ymax1, ymax2); if(xmax <= xmin || ymax <= ymin) { return false; } for(int y = int(ymin); y < ymax; y += 5) { for(int x = int(xmin); x < xmax; x += 5) { float x1 = x - xmin1, y1 = y - ymin1; float x2 = x - xmin2, y2 = y - ymin2; if(x1 > 0 && y1 > 0 && x2 > 0 && y2 > 0 && x1 < Sprite1.GetSize().x && y1 < Sprite1.GetSize().y && x2 < Sprite2.GetSize().x && y2 < Sprite2.GetSize().y) { Color color1 = Sprite1.GetPixel(int(x1),int(y1)); Color color2 = Sprite2.GetPixel(int(x2),int(y2)); if (color1.a >= AlphaLimit && color2.a >= AlphaLimit) { return true; } } } } return false; }
bool PPCollision(Vector2f point, Sprite & s, float AlphaLimit = 20.f) { point -= s.GetPosition() - s.GetCenter(); if(point.x >= 0 && point.x < s.GetSize().x && point.y >= 0 && point.y < s.GetSize().y && s.GetPixel((int)point.x, (int)point.y).a > AlphaLimit) return true; else return false; }
bool ownCollision(Sprite& s1, Sprite& s2, int n) { if(s1.GetPosition().x < s2.GetPosition().x+s2.GetSize().x && s1.GetPosition().x+s1.GetSize().x > s2.GetPosition().x && s1.GetPosition().y < s2.GetPosition().y+s2.GetSize().y && s1.GetPosition().y+s1.GetSize().y+n > s2.GetPosition().y) return true; else return false; }
bool SimpleCollision(Sprite s1, Sprite s2) { if(s1.GetPosition().x - s1.GetCenter().x < s2.GetPosition().x + s2.GetSize().x - s2.GetCenter().x && s1.GetPosition().x + s1.GetSize().x - s1.GetCenter().x > s2.GetPosition().x - s2.GetCenter().x && s1.GetPosition().y - s1.GetCenter().y < s2.GetPosition().y + s2.GetSize().y - s2.GetCenter().y && s1.GetPosition().y + s1.GetSize().y - s1.GetCenter().y > s2.GetPosition().y - s2.GetCenter().y) return true; else return false; }
bool EngineInput::hoversprite( Sprite &s ) { F32 x, y, w, h; x = s.GetPosition().x; y = s.GetPosition().y; w = x + s.GetSize().x; h = y + s.GetSize().y; if( x <= mouse.x && mouse.x <= w ) { if( y < mouse.y && mouse.y <= h ) { return true; } } return false; }
int lua_Sprite_SetPosition(lua_State* state) { Sprite* sprite = (Sprite*)lua_touserdata(state, 1); if (sprite == NULL) { return luaL_typerror(state, 1, "Sprite"); } if(lua_isnumber(state, 2)) { double x = (double) luaL_optnumber(state, 2, sprite->GetPosition().x); double y = (double) luaL_optnumber(state, 3, sprite->GetPosition().y); sprite->SetPosition(x, y); } else { Vector* vector = (Vector*)lua_touserdata(state, 2); if (vector == NULL) { return luaL_typerror(state, 2, "Vector"); } sprite->SetPosition(*vector); } return 0; }
int main() { glfwInit(); // Initialisoidaan GLFW // GLFW perusasetukset glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); // asetetaan OpenGL- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); // versio 2.1 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE); // määritellään OpenGL profiili käytettäväksi glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // ikkunan kokoa ei voi muuttaa // luodaan ikkuna GLFWwindow GLFWwindow* window = glfwCreateWindow(width, height, "EPELIROOTTORI", nullptr, nullptr); glfwMakeContextCurrent(window); glfwSetKeyCallback(window, key_callback); glewExperimental = GL_TRUE; glewInit(); // Initialisoidaan GLEW glViewport(0, 0, width, height); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Renderer renderer = { glm::vec2(width, height) }; Renderer renderer2 = { glm::vec2(width * 2, height * 2) }; Renderer renderer3 = { glm::vec2(width * 4, height * 4) }; AnimationManager anima; TextManager tex; tex.LoadFont("..//data/fonts/arial.ttf"); tex.SetText("0"); tex.SetCharacterSize(30); tex.SetPosition(glm::vec2(10, 0)); tex.SetColor(glm::vec3(0.0, 0.0, 1.0)); TextManager tex2; tex2.LoadFont("..//data/fonts/arial.ttf"); tex2.SetText("0"); tex2.SetCharacterSize(30); tex2.SetPosition(glm::vec2(width - tex2.GetGlobalBounds().x, 0)); tex2.SetColor(glm::vec3(0.0, 1.0, 0.0)); TextManager tex3; tex3.LoadFont("..//data/fonts/Arctik5.ttf"); tex3.SetText("Pong"); tex3.SetCharacterSize(30); tex3.SetPosition(glm::vec2(width / 2.5, 0)); tex3.SetColor(glm::vec3(0.0, 0.0, 0.0)); Sprite typhlosion; typhlosion.SetAnimation("../data/animations/Typhlosion2.png", glm::vec2(234, 250), 0.2); typhlosion.SetPosition(glm::vec2((width - typhlosion.GetBounds().x) / 2, (height - typhlosion.GetBounds().y) / 2)); typhlosion.SetScale(glm::vec2(0.5, 0.5)); Sprite kisse; kisse.SetTexture("../data/textures/kisse.png"); kisse.SetColor(glm::vec3(0.0, 0.0, 1.0)); kisse.SetPosition(glm::vec2(0, (height - kisse.GetBounds().y) / 2)); Sprite kisse2; kisse2.SetTexture("../data/textures/kisse.png"); kisse2.SetColor(glm::vec3(0.0, 1.0, 0.0)); kisse2.SetPosition(glm::vec2(width - kisse.GetBounds().x, (height - kisse.GetBounds().y) / 2)); Sprite animation; animation.SetAnimation("../data/animations/gems.png", "../data/animations/anim_gems.xml"); animation.SetPosition(glm::vec2((width / 2), 250)); animation.SetScale(glm::vec2(2, 2)); renderer2.camera->SetPosition(glm::vec2(1, 1)); renderer3.camera->SetPosition(glm::vec2(1.5, 1.5)); glm::vec2 dir = { 2.0, 2.0 }, dir2 = { 0.0, 1.0 }, dir3 = { 0.0, -1.0 }; int score1 = 0, score2 = 0; glm::vec3 color = { 0.01, 0.01, 0.01 }; Polygon polygon(300, 200, 1, 0, 350, 350, 1.0f, 0.4f, 0.3f, "../data/textures/planet.png"); Polygon polygon2(4, 100, 1, 0, 850, 350, 1.0f, 0.4f, 0.3f, "../data/textures/kisse.png"); Timer time; time.Start(); while (!glfwWindowShouldClose(window)) { glfwPollEvents(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(0.0f, 0.2f, 0.2f, 1.0f); // Typhlosion koskettaa vasenta tai oikeaa seinää if (typhlosion.GetPosition().x > width) { typhlosion.SetPosition(glm::vec2((width - typhlosion.GetBounds().x) / 2, (height - typhlosion.GetBounds().y) / 2)); dir.x = -2.0; score1++; tex.SetText(std::to_string(score1)); } if (typhlosion.GetPosition().x + typhlosion.GetGlobalBounds().x < 0) { typhlosion.SetPosition(glm::vec2((width - typhlosion.GetBounds().x) / 2, (height - typhlosion.GetBounds().y) / 2)); dir.x = 2.0; score2++; tex2.SetText(std::to_string(score2)); tex2.SetPosition(glm::vec2(width - tex2.GetGlobalBounds().x, 0)); } // Typhlosion kimpoaa katosta ja lattiasta if (typhlosion.GetPosition().y + typhlosion.GetGlobalBounds().y > height) { dir.y = -2.0; } if (typhlosion.GetPosition().y < 0) { dir.y = 2.0; } // Typhlosion törmää kisseien sivuihin // kisse1 if (kisse.GetPosition().y + kisse.GetGlobalBounds().y > typhlosion.GetPosition().y && kisse.GetPosition().y + kisse.GetGlobalBounds().y - 5 < typhlosion.GetPosition().y && kisse.GetPosition().x < typhlosion.GetPosition().x && kisse.GetPosition().x + kisse.GetGlobalBounds().x > typhlosion.GetPosition().x || kisse.GetPosition().y + kisse.GetGlobalBounds().y > typhlosion.GetPosition().y && kisse.GetPosition().y + kisse.GetGlobalBounds().y - 5 < typhlosion.GetPosition().y && kisse.GetPosition().x < typhlosion.GetPosition().x + typhlosion.GetGlobalBounds().x && kisse.GetPosition().x + kisse.GetGlobalBounds().x > typhlosion.GetPosition().x + typhlosion.GetGlobalBounds().x) { dir.y = 2.0; } else if (kisse.GetPosition().y < typhlosion.GetPosition().y + typhlosion.GetGlobalBounds().y && kisse.GetPosition().y + 5 > typhlosion.GetPosition().y + typhlosion.GetGlobalBounds().y && kisse.GetPosition().x < typhlosion.GetPosition().x && kisse.GetPosition().x + kisse.GetGlobalBounds().x > typhlosion.GetPosition().x || kisse.GetPosition().y < typhlosion.GetPosition().y + typhlosion.GetGlobalBounds().y && kisse.GetPosition().y + 5 > typhlosion.GetPosition().y + typhlosion.GetGlobalBounds().y && kisse.GetPosition().x < typhlosion.GetPosition().x + typhlosion.GetGlobalBounds().x && kisse.GetPosition().x + kisse.GetGlobalBounds().x > typhlosion.GetPosition().x + typhlosion.GetGlobalBounds().x) { dir.y = -2.0; } else if (kisse.GetPosition().x + kisse.GetGlobalBounds().x > typhlosion.GetPosition().x && kisse.GetPosition().y < typhlosion.GetPosition().y && kisse.GetPosition().y + kisse.GetGlobalBounds().y > typhlosion.GetPosition().y || kisse.GetPosition().x + kisse.GetGlobalBounds().x > typhlosion.GetPosition().x && kisse.GetPosition().y < typhlosion.GetPosition().y + typhlosion.GetGlobalBounds().y && kisse.GetPosition().y + kisse.GetGlobalBounds().y > typhlosion.GetPosition().y + typhlosion.GetGlobalBounds().y) { dir.x = 2.0; } // Typhlosion törmää kisseien sivuihin // kisse2 if (kisse2.GetPosition().y + kisse2.GetGlobalBounds().y > typhlosion.GetPosition().y && kisse2.GetPosition().y + kisse2.GetGlobalBounds().y - 5 < typhlosion.GetPosition().y && kisse2.GetPosition().x < typhlosion.GetPosition().x && kisse2.GetPosition().x + kisse2.GetGlobalBounds().x > typhlosion.GetPosition().x || kisse2.GetPosition().y + kisse2.GetGlobalBounds().y > typhlosion.GetPosition().y && kisse2.GetPosition().y + kisse2.GetGlobalBounds().y - 5 < typhlosion.GetPosition().y && kisse2.GetPosition().x < typhlosion.GetPosition().x + typhlosion.GetGlobalBounds().x && kisse2.GetPosition().x + kisse2.GetGlobalBounds().x > typhlosion.GetPosition().x + typhlosion.GetGlobalBounds().x) { dir.y = 2.0; } else if (kisse2.GetPosition().y < typhlosion.GetPosition().y + typhlosion.GetGlobalBounds().y && kisse2.GetPosition().y + 5 > typhlosion.GetPosition().y + typhlosion.GetGlobalBounds().y && kisse2.GetPosition().x < typhlosion.GetPosition().x && kisse2.GetPosition().x + kisse2.GetGlobalBounds().x > typhlosion.GetPosition().x || kisse2.GetPosition().y < typhlosion.GetPosition().y + typhlosion.GetGlobalBounds().y && kisse2.GetPosition().y + 5 > typhlosion.GetPosition().y + typhlosion.GetGlobalBounds().y && kisse2.GetPosition().x < typhlosion.GetPosition().x + typhlosion.GetGlobalBounds().x && kisse2.GetPosition().x + kisse2.GetGlobalBounds().x > typhlosion.GetPosition().x + typhlosion.GetGlobalBounds().x) { dir.y = -2.0; } else if (kisse2.GetPosition().x < typhlosion.GetPosition().x + typhlosion.GetGlobalBounds().x && kisse2.GetPosition().y < typhlosion.GetPosition().y && kisse2.GetPosition().y + kisse2.GetGlobalBounds().y > typhlosion.GetPosition().y || kisse2.GetPosition().x < typhlosion.GetPosition().x + typhlosion.GetGlobalBounds().x && kisse2.GetPosition().y < typhlosion.GetPosition().y + typhlosion.GetGlobalBounds().y && kisse2.GetPosition().y + kisse2.GetGlobalBounds().y > typhlosion.GetPosition().y + typhlosion.GetGlobalBounds().y) { dir.x = -2.0; } // kisse1 liikkumiset if (kisse.GetPosition().y < 0) { dir2 = { 0.0, 1.0 }; } if (kisse.GetPosition().y + kisse.GetGlobalBounds().y > height) { dir2 = { 0.0, -1.0 }; } // kisse2 liikkumiset if (kisse2.GetPosition().y < 0) { dir3 = { 0.0, 1.0 }; } if (kisse2.GetPosition().y + kisse2.GetGlobalBounds().y > height) { dir3 = { 0.0, -1.0 }; } if (tex3.GetColor().x < 0) { color = glm::vec3(0.01, 0.01, 0.01); } if (tex3.GetColor().x > 1) { color = glm::vec3(-0.01, -0.01, -0.01); } tex3.SetColor(tex3.GetColor() + color); polygon2.Rotate(1); polygon.DemoEffect(1); typhlosion.Rotate(1); typhlosion.Move(dir); kisse.Move(dir2); kisse2.Move(dir3); renderer.Draw(polygon); renderer.Draw(polygon2); renderer.Draw(typhlosion); renderer.Draw(kisse); renderer.Draw(kisse2); renderer.Draw(tex); renderer.Draw(tex2); renderer.Draw(tex3); renderer.Draw(animation); animation.GetAnimationManager()->UpdateAnimation(); typhlosion.GetAnimationManager()->UpdateAnimation(); glDisable(GL_TEXTURE_2D); glfwSwapBuffers(window); } }
Vector2f GetPosition() { return oSprite.GetPosition(); }
bool Colision::colisionSimple(Sprite s1, Sprite s2) { ///Obtengo el tamaño y la posicion de los sprites a comparar Vector2f tam_s1 = s1.GetSize(); Vector2f tam_s2 = s2.GetSize(); Vector2f pos_s1 = s1.GetPosition(); Vector2f pos_s2 = s2.GetPosition(); ///Comparo si la esquina superior izquierda se S1 estra dentro del area ocupada por s2 if( ((pos_s1.x >= pos_s2.x) and (pos_s1.x <= pos_s2.x + tam_s2.x)) and ((pos_s1.y >= pos_s2.y) and (pos_s1.y <= pos_s2.y + tam_s2.y)) ) { return true; } ///Comparo si la esquina superior dercha de S1 esta dentro del area ocupada por S2 if( ((pos_s1.x + tam_s1.x >= pos_s2.x) and (pos_s1.x + tam_s1.x <= pos_s2.x + tam_s2.x)) and ((pos_s1.y >= pos_s2.y) and (pos_s1.y <= pos_s2.y + tam_s2.y)) ) { return true; } ///Comparo si la esquina inferior derecha de S1 esta dentro del area ocupada por S2 if( ((pos_s1.x + tam_s1.x >= pos_s2.x) and (pos_s1.x + tam_s1.x <= pos_s2.x + tam_s2.x)) and ((pos_s1.y + tam_s2.y>= pos_s2.y) and (pos_s1.y + tam_s2.y <= pos_s2.y + tam_s2.y)) ) { return true; } ///Comparo si la esquina inferior izquierda de S1 esta dentro del area ocupada por S2 if( ((pos_s1.x >= pos_s2.x) and (pos_s1.x <= pos_s2.x + tam_s2.x)) and ((pos_s1.y + tam_s2.y>= pos_s2.y) and (pos_s1.y + tam_s2.y <= pos_s2.y + tam_s2.y)) ) { return true; } ///s2 con s1 ///Comparo si la esquina superior izquierda se S1 estra dentro del area ocupada por s2 if( ((pos_s2.x >= pos_s1.x) and (pos_s2.x <= pos_s1.x + tam_s1.x)) and ((pos_s2.y >= pos_s1.y) and (pos_s2.y <= pos_s1.y + tam_s1.y)) ) { return true; } ///Comparo si la esquina superior dercha de S1 esta dentro del area ocupada por S2 if( ((pos_s2.x + tam_s2.x >= pos_s1.x) and (pos_s2.x + tam_s2.x <= pos_s1.x + tam_s1.x)) and ((pos_s2.y >= pos_s1.y) and (pos_s2.y <= pos_s1.y + tam_s1.y)) ) { return true; } ///Comparo si la esquina inferior derecha de S1 esta dentro del area ocupada por S2 if( ((pos_s2.x + tam_s2.x >= pos_s1.x) and (pos_s2.x + tam_s2.x <= pos_s1.x + tam_s1.x)) and ((pos_s2.y + tam_s1.y>= pos_s1.y) and (pos_s2.y + tam_s1.y <= pos_s1.y + tam_s1.y)) ) { return true; } ///Comparo si la esquina inferior izquierda de S1 esta dentro del area ocupada por S2 if( ((pos_s2.x >= pos_s1.x) and (pos_s2.x <= pos_s1.x + tam_s1.x)) and ((pos_s2.y + tam_s1.y>= pos_s1.y) and (pos_s2.y + tam_s1.y <= pos_s1.y + tam_s1.y)) ) { return true; } return false; }
//void RenderManager::RenderObjectAt(Entity* object, const a2de::Vector2D& screen_position) { // if(object == nullptr) return; // a2de::Vector2D old_pos(object->GetBody()->GetPosition()); // object->GetBody()->SetPosition(screen_position); // object->Render(al_get_backbuffer(_display_context)); // object->GetBody()->SetPosition(old_pos); //} // void RenderManager::RenderObjectAt(Sprite& sprite, const a2de::Vector2D& screen_position) { a2de::Vector2D old_pos(sprite.GetPosition()); sprite.SetPosition(screen_position); sprite.Draw(al_get_backbuffer(_display_context)); sprite.SetPosition(old_pos); }
Vector2f LowerRight(Sprite & sprite) { return sprite.GetPosition() + sprite.GetSize() - sprite.GetCenter(); }
Vector2f UpperLeft(Sprite & sprite) { return sprite.GetPosition() - sprite.GetCenter(); }