Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

112 Neutral

About babaliaris

  • Rank

Personal Information


  • Github
  • Steam

Recent Profile Visitors

733 profile views
  1. Well i found out Here what's the problem and how to solve it (Something about world coordinates and object coordinates) but i can't understand how ti works. Can you show me some examples in code on how you implement this??? Scaling Matrix: m_Impl->scale = glm::mat4(1.0f); m_Impl->scale = glm::scale(m_Impl->scale, glm::vec3(width, height, 0)); Verticies: //Verticies. float verticies[] = { //Positions. //Texture Coordinates. 1.0f, 1.0f, 0.0f, 0.0f, 2.0f, 1.0f, 1.0f, 0.0f, 2.0f, 2.0f, 1.0f, 1.0f, 1.0f, 2.0f, 0.0f, 1.0f }; Rendering: //Projection Matrix. glm::mat4 proj = glm::ortho(0.0f, (float)window->GetWidth(), 0.0f, (float)window->GetHeight(), -1.0f, 1.0f); //Set the uniform. material->program->setUniformMat4f("u_MVP", proj * model); //model is the scale matrix from the previous code. //Draw. glDrawElements(GL_TRIANGLES, material->ibo->GetCount(), GL_UNSIGNED_INT, NULL); Shader: #shader vertex #version 330 core layout(location = 0) in vec4 aPos; layout(location = 1) in vec2 aTexCoord; out vec2 texCoord; uniform mat4 u_MVP; void main() { gl_Position = u_MVP*aPos; texCoord = aTexCoord; } #shader fragment #version 330 core out vec4 colors; in vec2 texCoord; uniform sampler2D u_Texture; void main() { colors = texture(u_Texture, texCoord); } Before Scaling (It's down there on the bottom left corner as a dot). After Scaling Problem: Why does the position also changes?? If you see my Verticies, the first position starts at 1.0f, 1.0f , so when i'm scaling it should stay at that position
  2. OMG MAN I LOVE YOU!!!!!!!!!!!!! I wasn't binding the shaders inside the Sprite::Render() Method So i was actually setting the mvp only on the shader that was binded previously inside the renderer!!! Problem solved!! I literally can't thank you enough! This will prevent a lot of frustration in the future. This forum never led me down Thank you again! This is what i had to change in my Sprite::Render Method: //Render. void Sprite::Render(Window * window) { //I HAD TO DO THAT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! m_Impl->program->Bind(); //Create the projection Matrix based on the current window width and height. glm::mat4 proj = glm::ortho(0.0f, (float)window->GetWidth(), 0.0f, (float)window->GetHeight(), -1.0f, 1.0f); //SO I CAN DO THAT ON THE RIGHT SHADER!!!!!!!!!!!!!!!!!!! //Set the MVP Uniform. m_Impl->program->setUniformMat4f("u_MVP", proj * m_Impl->model); std::cout << "Inside " << m_Impl->tag.c_str() << " Render() has model pointer: " << &m_Impl->model << std::endl; //Run All The Brains (Scripts) of this game object (sprite). for (unsigned int i = 0; i < m_Impl->brains.size(); i++) { //Get Current Brain. Brain *brain = m_Impl->brains[i]; //Call the start function only once! if (brain->GetStart()) { brain->SetStart(false); brain->Start(); } //Call the update function every frame. brain->Update(); } //Render. window->GetRenderer()->Draw(m_Impl->vao, m_Impl->ibo, m_Impl->texture, m_Impl->program); }
  3. As you can see everything works as i expected. The model matrix of the player is changing every time, and also i double checked the model matrices of the other two sprites (they remain Identity Matrices). Also the add brain is adding the brain to the correct sprite (Player). First Call Of Player's Renderer. Second Call Of Player's Renderer. Third Call Of Player's Renderer.
  4. I also noticed this (Check out the image i attached): Player's VAO, VBO and program (shader) have exactly the same id. Is this reasonably? Does openGL assign different ids for each of these components, so a vao and a vbo can have the same id for example? Also i checked the ids of the other two sprite object and each sprite object has a unique id for vao, vbo, ibo and program and also when i'm binding all these inside the Renderer i'm sure i'm binding the correct ones for the correct corresponding sprite object. And at the end we will probably find a bug so stupid and we all are going to laugh ūüėõ
  5. I have news! I debugged the application and i noticed the followings: The Model Matrix of the Player is being updating as i expected. The Model Matrices of the other two sprite objects are not being updated as i expected (They remain as identity matrices) So the problem is not there. What else do you think might cause this??? Could be the Renderer? But inside the renderer i'm binding the vao, ibo, texture and shader of the current sprite object, and each sprite has it's own unique vao, vbo, ibo, and shader and since i saw that indeed the Player's model Matrix is being updating correctly, i can't understand why it's not the Player's Vertices which are being transformed. It's like the MVP Matrix does not affect the Player's vertices but only the last object which i instantiate. According to what i said above this is impossible!!!
  6. Ok I'll check it. The problem is that I'm creating the game engine in a different project and compiling it as a dynamic library. Then I'm linking it with the actual game project in where I have the Main.cpp file. If I put a break point into Main, it won't jump inside methods which are coming from the dynamic library. Is there something I can do about that, or I must create the whole game as one project which compiles into an executable????
  7. You are right. When I change the order of the sprite objects instantiation, I see another sprite moving. But I can't understand why they could exchange data with each other. If you see the sprite constructor, it creates a new Implementation object, so each sprite object should have it's own.
  8. I updated my Post and i added all the Classes. I think i'm sure i'm calling move on the right sprite object.
  9. Hello Everyone! I'm learning openGL, and currently i'm making a simple 2D game engine to test what I've learn so far. In order to not say to much, i made a video in which i'm showing you the behavior of the rendering. Video: What i was expecting to happen, was the player moving around. When i render only the player, he moves as i would expect. When i add a second Sprite object, instead of the Player, this new sprite object is moving and finally if i add a third Sprite object the third one is moving. And the weird think is that i'm transforming the Vertices of the Player so why the transformation is being applied somewhere else? Take a look at my code: Sprite Class (You mostly need to see the Constructor, the Render Method and the Move Method) #include "Brain.h" #include <glm/gtc/matrix_transform.hpp> #include <vector> struct Sprite::Implementation { //Position. struct pos pos; //Tag. std::string tag; //Texture. Texture *texture; //Model matrix. glm::mat4 model; //Vertex Array Object. VertexArray *vao; //Vertex Buffer Object. VertexBuffer *vbo; //Layout. VertexBufferLayout *layout; //Index Buffer Object. IndexBuffer *ibo; //Shader. Shader *program; //Brains. std::vector<Brain *> brains; //Deconstructor. ~Implementation(); }; Sprite::Sprite(std::string image_path, std::string tag, float x, float y) { //Create Pointer To Implementaion. m_Impl = new Implementation(); //Set the Position of the Sprite object. m_Impl->pos.x = x; m_Impl->pos.y = y; //Set the tag. m_Impl->tag = tag; //Create The Texture. m_Impl->texture = new Texture(image_path); //Initialize the model Matrix. m_Impl->model = glm::mat4(1.0f); //Get the Width and the Height of the Texture. int width = m_Impl->texture->GetWidth(); int height = m_Impl->texture->GetHeight(); //Create the Verticies. float verticies[] = { //Positions //Texture Coordinates. x, y, 0.0f, 0.0f, x + width, y, 1.0f, 0.0f, x + width, y + height, 1.0f, 1.0f, x, y + height, 0.0f, 1.0f }; //Create the Indicies. unsigned int indicies[] = { 0, 1, 2, 2, 3, 0 }; //Create Vertex Array. m_Impl->vao = new VertexArray(); //Create the Vertex Buffer. m_Impl->vbo = new VertexBuffer((void *)verticies, sizeof(verticies)); //Create The Layout. m_Impl->layout = new VertexBufferLayout(); m_Impl->layout->PushFloat(2); m_Impl->layout->PushFloat(2); m_Impl->vao->AddBuffer(m_Impl->vbo, m_Impl->layout); //Create the Index Buffer. m_Impl->ibo = new IndexBuffer(indicies, 6); //Create the new shader. m_Impl->program = new Shader("Shaders/SpriteShader.shader"); } //Render. void Sprite::Render(Window * window) { //Create the projection Matrix based on the current window width and height. glm::mat4 proj = glm::ortho(0.0f, (float)window->GetWidth(), 0.0f, (float)window->GetHeight(), -1.0f, 1.0f); //Set the MVP Uniform. m_Impl->program->setUniformMat4f("u_MVP", proj * m_Impl->model); //Run All The Brains (Scripts) of this game object (sprite). for (unsigned int i = 0; i < m_Impl->brains.size(); i++) { //Get Current Brain. Brain *brain = m_Impl->brains[i]; //Call the start function only once! if (brain->GetStart()) { brain->SetStart(false); brain->Start(); } //Call the update function every frame. brain->Update(); } //Render. window->GetRenderer()->Draw(m_Impl->vao, m_Impl->ibo, m_Impl->texture, m_Impl->program); } void Sprite::Move(float speed, bool left, bool right, bool up, bool down) { if (left) { m_Impl->pos.x -= speed; m_Impl->model = glm::translate(m_Impl->model, glm::vec3(-speed, 0, 0)); } if (right) { m_Impl->pos.x += speed; m_Impl->model = glm::translate(m_Impl->model, glm::vec3(speed, 0, 0)); } if (up) { m_Impl->pos.y += speed; m_Impl->model = glm::translate(m_Impl->model, glm::vec3(0, speed, 0)); } if (down) { m_Impl->pos.y -= speed; m_Impl->model = glm::translate(m_Impl->model, glm::vec3(0, -speed, 0)); } } void Sprite::AddBrain(Brain * brain) { //Push back the brain object. m_Impl->brains.push_back(brain); } pos *Sprite::GetPos() { return &m_Impl->pos; } std::string Sprite::GetTag() { return m_Impl->tag; } int Sprite::GetWidth() { return m_Impl->texture->GetWidth(); } int Sprite::GetHeight() { return m_Impl->texture->GetHeight(); } Sprite::~Sprite() { delete m_Impl; } //Implementation Deconstructor. Sprite::Implementation::~Implementation() { delete texture; delete vao; delete vbo; delete layout; delete ibo; delete program; } Renderer Class #include "Renderer.h" #include "Error.h" Renderer::Renderer() { } Renderer::~Renderer() { } void Renderer::Draw(VertexArray * vao, IndexBuffer * ibo, Texture *texture, Shader * program) { vao->Bind(); ibo->Bind(); program->Bind(); if (texture != NULL) texture->Bind(); GLCall(glDrawElements(GL_TRIANGLES, ibo->GetCount(), GL_UNSIGNED_INT, NULL)); } void Renderer::Clear(float r, float g, float b) { GLCall(glClearColor(r, g, b, 1.0)); GLCall(glClear(GL_COLOR_BUFFER_BIT)); } void Renderer::Update(GLFWwindow *window) { /* Swap front and back buffers */ glfwSwapBuffers(window); /* Poll for and process events */ glfwPollEvents(); } Shader Code #shader vertex #version 330 core layout(location = 0) in vec4 aPos; layout(location = 1) in vec2 aTexCoord; out vec2 t_TexCoord; uniform mat4 u_MVP; void main() { gl_Position = u_MVP * aPos; t_TexCoord = aTexCoord; } #shader fragment #version 330 core out vec4 aColor; in vec2 t_TexCoord; uniform sampler2D u_Texture; void main() { aColor = texture(u_Texture, t_TexCoord); } Also i'm pretty sure that every time i'm hitting the up, down, left and right arrows on the keyboard, i'm changing the model Matrix of the Player and not the others. Window Class: #include "Window.h" #include <GL/glew.h> #include <GLFW/glfw3.h> #include "Error.h" #include "Renderer.h" #include "Scene.h" #include "Input.h" //Global Variables. int screen_width, screen_height; //On Window Resize. void OnWindowResize(GLFWwindow *window, int width, int height); //Implementation Structure. struct Window::Implementation { //GLFW Window. GLFWwindow *GLFW_window; //Renderer. Renderer *renderer; //Delta Time. double delta_time; //Frames Per Second. int fps; //Scene. Scene *scnene; //Input. Input *input; //Deconstructor. ~Implementation(); }; //Window Constructor. Window::Window(std::string title, int width, int height) { //Initializing width and height. screen_width = width; screen_height = height; //Create Pointer To Implementation. m_Impl = new Implementation(); //Try initializing GLFW. if (!glfwInit()) { std::cout << "GLFW could not be initialized!" << std::endl; std::cout << "Press ENTER to exit..." << std::endl; std::cin.get(); exit(-1); } //Setting up OpenGL Version 3.3 Core Profile. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); /* Create a windowed mode window and its OpenGL context */ m_Impl->GLFW_window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL); if (!m_Impl->GLFW_window) { std::cout << "GLFW could not create a window!" << std::endl; std::cout << "Press ENTER to exit..." << std::endl; std::cin.get(); glfwTerminate(); exit(-1); } /* Make the window's context current */ glfwMakeContextCurrent(m_Impl->GLFW_window); //Initialize GLEW. if(glewInit() != GLEW_OK) { std::cout << "GLEW could not be initialized!" << std::endl; std::cout << "Press ENTER to exit..." << std::endl; std::cin.get(); glfwTerminate(); exit(-1); } //Enabling Blending. GLCall(glEnable(GL_BLEND)); GLCall(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); //Setting the ViewPort. GLCall(glViewport(0, 0, width, height)); //**********Initializing Implementation**********// m_Impl->renderer = new Renderer(); m_Impl->delta_time = 0.0; m_Impl->fps = 0; m_Impl->input = new Input(this); //**********Initializing Implementation**********// //Set Frame Buffer Size Callback. glfwSetFramebufferSizeCallback(m_Impl->GLFW_window, OnWindowResize); } //Window Deconstructor. Window::~Window() { delete m_Impl; } //Window Main Loop. void Window::MainLoop() { //Time Variables. double start_time = 0, end_time = 0, old_time = 0, total_time = 0; //Frames Counter. int frames = 0; /* Loop until the user closes the window */ while (!glfwWindowShouldClose(m_Impl->GLFW_window)) { old_time = start_time; //Total time of previous frame. start_time = glfwGetTime(); //Current frame start time. //Calculate the Delta Time. m_Impl->delta_time = start_time - old_time; //Get Frames Per Second. if (total_time >= 1) { m_Impl->fps = frames; total_time = 0; frames = 0; } //Clearing The Screen. m_Impl->renderer->Clear(0, 0, 0); //Render The Scene. if (m_Impl->scnene != NULL) m_Impl->scnene->Render(this); //Updating the Screen. m_Impl->renderer->Update(m_Impl->GLFW_window); //Increasing frames counter. frames++; //End Time. end_time = glfwGetTime(); //Total time after the frame completed. total_time += end_time - start_time; } //Terminate GLFW. glfwTerminate(); } //Load Scene. void Window::LoadScene(Scene * scene) { //Set the scene. m_Impl->scnene = scene; } //Get Delta Time. double Window::GetDeltaTime() { return m_Impl->delta_time; } //Get FPS. int Window::GetFPS() { return m_Impl->fps; } //Get Width. int Window::GetWidth() { return screen_width; } //Get Height. int Window::GetHeight() { return screen_height; } //Get Input. Input * Window::GetInput() { return m_Impl->input; } Renderer * Window::GetRenderer() { return m_Impl->renderer; } GLFWwindow * Window::GetGLFWindow() { return m_Impl->GLFW_window; } //Implementation Deconstructor. Window::Implementation::~Implementation() { delete renderer; delete input; } //OnWindowResize void OnWindowResize(GLFWwindow *window, int width, int height) { screen_width = width; screen_height = height; //Updating the ViewPort. GLCall(glViewport(0, 0, width, height)); } Brain Class #include "Brain.h" #include "Sprite.h" #include "Window.h" struct Brain::Implementation { //Just A Flag. bool started; //Window Pointer. Window *window; //Sprite Pointer. Sprite *sprite; }; Brain::Brain(Window *window, Sprite *sprite) { //Create Pointer To Implementation. m_Impl = new Implementation(); //Initialize Implementation. m_Impl->started = true; m_Impl->window = window; m_Impl->sprite = sprite; } Brain::~Brain() { //Delete Pointer To Implementation. delete m_Impl; } void Brain::Start() { } void Brain::Update() { } Window * Brain::GetWindow() { return m_Impl->window; } Sprite * Brain::GetSprite() { return m_Impl->sprite; } bool Brain::GetStart() { return m_Impl->started; } void Brain::SetStart(bool value) { m_Impl->started = value; } Script Class (Its a Brain Subclass!!!) #include "Script.h" Script::Script(Window *window, Sprite *sprite) : Brain(window, sprite) { } Script::~Script() { } void Script::Start() { std::cout << "Game Started!" << std::endl; } void Script::Update() { Input *input = this->GetWindow()->GetInput(); Sprite *sp = this->GetSprite(); //Move this sprite. this->GetSprite()->Move(200 * this->GetWindow()->GetDeltaTime(), input->GetKeyDown("left"), input->GetKeyDown("right"), input->GetKeyDown("up"), input->GetKeyDown("down")); std::cout << sp->GetTag().c_str() << ".x = " << sp->GetPos()->x << ", " << sp->GetTag().c_str() << ".y = " << sp->GetPos()->y << std::endl; } Main: #include "SpaceShooterEngine.h" #include "Script.h" int main() { Window w("title", 600,600); Scene *scene = new Scene(); Sprite *player = new Sprite("Resources/Images/player.png", "Player", 100,100); Sprite *other = new Sprite("Resources/Images/cherno.png", "Other", 400, 100); Sprite *other2 = new Sprite("Resources/Images/cherno.png", "Other", 300, 400); Brain *brain = new Script(&w, player); player->AddBrain(brain); scene->AddSprite(player); scene->AddSprite(other); scene->AddSprite(other2); w.LoadScene(scene); w.MainLoop(); return 0; } I literally can't find what is wrong. If you need more code, ask me to post it. I will also attach all the source files. Brain.cpp Error.cpp IndexBuffer.cpp Input.cpp Renderer.cpp Scene.cpp Shader.cpp Sprite.cpp Texture.cpp VertexArray.cpp VertexBuffer.cpp VertexBufferLayout.cpp Window.cpp Brain.h Error.h IndexBuffer.h Input.h Renderer.h Scene.h Shader.h SpaceShooterEngine.h Sprite.h Texture.h VertexArray.h VertexBuffer.h VertexBufferLayout.h Window.h
  10. babaliaris

    Spatial Hashing.

    I was thinking to make a hash table which each bucket will point to a quadtree. So instead of having only one big quadtree, i can have multiple. I think this will be a lot faster than having a single tree. Red lines represents the spatial hash table while the blue ones quadtrees. and of course all this will be applied during scene loading. Is this going to be better than having a single quadtree?
  11. babaliaris

    Spatial Hashing.

    There you stepped from (2097152*2097152) = 4,398,046,511,104 to (16777216 * 2) = 33,554,432, and you didn't wonder where the remaining 4,398,012,956,672 went? As a first suggestion, use powers of 2 everywhere, and don't bother expanding them to decimal numbers, as it only confuses matters. You are correct in 2^11 being the first value > 1920), so the number of entries in 1 one direction is 2^32 / 2^11 = 2^21   ( 2^a / 2^b = 2^(a-b) ) The full table has 2 such dimensions, giving you 2^21 * 2^21 = 2^42 cells. (that's 2^12 * 2^30 cells = 4096 GB if a cell takes 1 byte.) If you want to squeeze that in 32MB (2^25 byte), you get 2^42 / 2^25 = 2^17 cells in each single byte, or for 8 byte memory-bucket you get 2^(17+3) = 2^20 cells in a memory-bucket.   To be honest, I am lost here. Perhaps instead of throwing numbers around, take a step back, and describe some properties of the objects in your world first, so we can give some guidance about good and bad solutions? Some properties that come to mind: - World-size (given, 2^32 in both x and y direction). - Total number of objects in the world - Accuracy of positioning (integer positions, or fractions)? - How are objects spread? (evenly, clustered, everything in [0,1), something else? ) Under the assumption that not all objects are equally relevant all the time (eg stuff on the screen or "nearby" may be more relevant, so maybe only keep those in the table?) - How large is the "relevant" part of the world? - How many objects of the above are such a part? - How are they spread? - How close to each other are they?   Unfortunately i can't answer these questions. All of this depends on the programmer. My game engine is not designed to work for a specified game but to give functionalities in order other programmers be able to create 2d games very easy. But one of the problems i think can be sovled. I know that my full world is a square with length 2^32, but of of course the game programmer won't use all of this space. So, after he creates the scene when the game runs i can calculate which of these objects has the less x coordinate the greater x coordinate the y less coordinate and the y greater coordinate or even better i can force the programmer to tell me exactly the borders of his world. And using this coordinates i can create another square with length = less x coordinate + greater x coordinate (and make sure its power of two) to surround his world. So now i can use the new length in order to restrict the space and create a hash table only for that space. But i also must make sure that on runtime, nothing can go farther of this square (all the objects inlcuding new spawn ones) because if an objects has (x,y) coordinates greater than this square it will cause the hash function to return out of range keys. How does this sounds?
  12. babaliaris

    Spatial Hashing.

      Yes, your math is correct and setting  42949672.96 = cell_size will make your lookup function work. However given how large your cells are this seems pretty large. I'm not sure how big sprites will be in your game, but the article recommended that the space of a cell be roughly twice as large as an average moving object in your game. Maybe a better course of action would be to determine how big you want your cells and use that equation above to find out how many you will need. Of course if you go for more cells you will have a larger array in memory, so you'll need to determine where the sweet spot is for performance. Larger spacial array and less objects to process per cell or a smaller spacial array with more objects per cell.  Another cool idea for performance tweaks would be to make sure your cell count is a power of 2 (which would make your cell size a power of 2 since your total world size is too) that way instead of dividing you can just right shift the bits of your coordinate (i.e. if cell_size = 2^6 instead of doing (int) (y / cell_size) you could do (int) (y >> 6) which is computationally cheaper than division, although it is less obvious programatically what you're doing). You are right, if my cell size is 42949672.96, then the spatial table won't do anything to increase performance since all of my objects most likely will be placed into a single bucket (Because buckets will be too big). So lets say that my biggest object has a width of 1920 pixels and a height of 1080 (Full HD LOL  :P ).  Lets say that my cell size will be the largest value (1920). 1920 its not a power of two so lets make it. If i do log2(1920) = 10.90689059560852 and ceil this number i will get 11. So the closest power of two will be 2^11 = 2048. Now i can easily say 2^32 / 2048 = 2097152. So now i must create a table of 2097152x2097152  :o  :o   RIP MEMORY! Actually, if we say that each bucket holds up to two integer numbers (8 bytes) 2097152 * 8 = 16777216 and 16777216  * 2 = 33554432 bytes = 32 MB. I guess its not that much.
  13. Hello! I read this article https://www.gamedev.net/resources/_/technical/game-programming/spatial-hashing-r2697 about spatial hashing but i have a question. In my 2d engine, the world's borders are specified and its actually a square with length 2^32 . In the spatial hash article, i read that in order to place a point inside a bucket you can use this hash function: row = (int) (y / cell_size), column = (int) (x / cell_size) . So if i have a 2d array, the function above will give me the row and the column of the 2d array where i have to insert my point. Then i said to my self "And how do you initialize the dimension of the array and also make sure that the hash function won't give you a row or a column out of range of the array?" Then i thought about this: Say that i want to divide my space in 100 squares. I know that my world is actually a square with length 2^32, so how do i find the size of the cell? In order to solve this i started thinking with mathematics so i reached to this: 2^32 / cell_size = 100 <=> cell_size  = 2^32 / 100 <=> cell_size  = 42949672.96 So this tell's me that i need to create a 2d array with dimensions 100x100 and each bucket (cell) will be a square with the above cell_size. Am i right? And if we assume that the maximum dimension of my world is 2^32 (square) then the hash function will always give me a number between 0 and 100 both for row and column because the x and y coordinates of a point will always be between -2^16 and 2^16 Please correct me if i am wrong.
  14. babaliaris

    Devide a scene to increase fps.

    Guys, i want an other advice too (For the quadtree). My quadtree is working like this: Every node can have a maximum amount of sprite objects (let's say 10 for this example) which they are stored in a list. If the length of the list become more than the maximum amount, the node will split. Sprite objects which are not entirly fit inside a region, will be place on the parent node. Take this scene as an example. The red cross represents the space division, the blue rectangles moving objects and the black rects static objects.   The quadtree will look something like that: As you can see, 11,12,13 are in the parent node, because they could'nt entirly fit inside of the 4 regions.     There are only 3 objects that can move around the scene with labels 1,2 and 3 (Blue ones). So in order to check for collision i must do something like that: moving = [1,2,3] #Go though all moving objects. for mov in moving: #Get the root of node of the quadtree. root = quadtree.root #While the root node is not a leaf. while not root.isLeaf(): #Get currents node sprite list. sprites = root.get_sprites() #Check collision of the moving sprite with the current sprite list. for sprite in sprites: if mov != sprite and mov.getCollision(sprite): #Collision detected!!! #Move to the next child depending on the moving's sprite position. root = root.getChild(mov.position) In words, for every moving object i must start checking for collision from the root of the quadtree and finish in a leaf. Only and only then i will be sure that i checked all the possible objects which might collide with my moving sprite, am i right?   Please not that the algorithm above will miss a check, because the while not root.isLeaf() wont check for collision with the last node. This can be fixed easily so don't worry about it right now.   Is this efficient? Should i correct something? Please advice me :P
  15. babaliaris

    Devide a scene to increase fps.

    Anyway! With all of your help i think i've got the solution! Just tell me how to create the hash function of a spatial hashing and i'm good to go!!! I will report back to you with my new results!
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!