Jump to content
  • Advertisement

chaosvine

Member
  • Content Count

    16
  • Joined

  • Last visited

Community Reputation

106 Neutral

About chaosvine

  • Rank
    Member
  1. So I've looked up Vertex arrays, they are basically to my understanding a Vertex Buffer that is already wrapped in a dynamic array for you (very convenient). However, this poses the new Question of how to go about using this for my purposes specifically in that I'm not sure exactly how I would create a new object or mesh from this that would display the textures properly. =/   I'm going to look through the links Jtippets provided in his above post. Perhaps there is a clue there. Otherwise maybe someone could fill me in to help on my search?
  2. Yeah sorry, I forgot to show code. I've used vectors extensively in the past, I'm just a bit rusty when it comes to some things. And I forgot about them automatically allocating memory and deallocating it.   As far as dimensions of the array, I'm already using a 1D array like a 3D array. :)   Yeah, I've been told I could get more performance from a vertex array but I'm not sure what that is exactly. I have yet to look it up, was a bit tired last night...   I'm assuming it's something similiar to the Vertex Buffers in OpenGL / DirectX in which case I would have a starting point at least for converting the algorithm.
  3. Ok, so I tried it using sprite objects only (I think this is what you were referring to when you said to use the gpu hardware). It renders very well actually... and I should have known this would work based on my experience in rendering alot of instanced objects with small geometry before in 3d applications. And the SFML documentation even says in the tutorial that the best performance will come from modifying the objects as necessary but instancing the textures all from the same atlas. I don't know why I was actually worried about this. However, I now have another question...   So I'm allocating an array of sprites (in this instance 16 * 64 * 64 long) that I'm using to draw the sprites to the screen. However I was under the impression that I'd have to delete this array at shutdown... when I try to delete the array the debug output window says that its an invalid pointer. I'm confused, so do I still need to delete this array of sprite objects or is sfml taking care of that for me somehow? I couldn't find anything on this in the documentation.
  4. Ok, you're mentioning a few things I'm not exactly clear on understanding. I'll do some more research on texture atlases vertex buffers before I proceed.
  5.   Thanks for these suggestions, you've given me a bit of a direction in which to go. And, before I even read this I decided earlier to do the rendering and layout in chunks of blocks. So what I am doing now is separating the chunks into different textures and then drawing those to the screen like this little update here:     This is actually about 4 different textures (all filled with the same block pattern as I have not implemented a way to make them different yet). The computer draws this quite well, but it is statically generated so that leaves me with a bit of a problem in how to go about dynamically changing the texture each frame.   Also, I still need to improve the algorithm I'm using to create a more natural look, so I'm looking into options.
  6.   Hmm interesting idea, however I'd prefer to have them randomly generated or rather coherently generated so as to be slightly different on each play through.
  7.   Well, I had originally designed this in windows then got interested in cross platform development recently and picked back up this project. I have already considered this and I'm actually leaning towards sfml as it is more concise and lightweight for the purposes of 2D. Though I am more familiar with programming directx. I haven't made up my mind yet.
  8. It seems to me like you've taken the "performance problems" away from the graphics card (which is equipped to handle them) and given them to the CPU instead, inside of a draw loop that copies pixels into an image before then uploading that image to the graphics card. In the process, you've created more performance issues than the ones you thought you might have by doing it another way. Did you try doing it other ways, and make this decision based on data, or did you go by what you "thought" would happen? What you ought to do is go back to the drawing board and write an algorithm that actually makes use of that expensive, fancy high technology that your monitor is plugged into, because it can move pixels around FAR more quickly than you CPU can. That being said... 1) I have written about this kind of thing a few times in the past, for Game Developer Magazine, as well as in my dev journal. The gist is that instead of using a 2D Perlin noise map, you use a 3D Perlin noise function that is used to perturb a 3-dimensional step function oriented along the vertical axis (the ground). This provides volumetric data, and when combined with other fractals such as a pair of multipled Ridged fractals to carve out caves, can present a nicely detailed representation. It's a bit more complicated, though... 2) You might consider using shorter blocks, if pathfinding is an issue. The thing about using cubes is that when a character traverses from one cube to the next, the traversal requires a very steep path of motion. It is simply not "realistic" that a walking character should be able to climb that steeply. Instead, you might consider using half, quarter or even eighth cubes as the basis of your world. Of course, this increases the vertical resolution of the terrain, and thus increases the total number of primitives to be drawn. 3) You need a structure that facilitates random access of a large volumetric data set. The quickest random access, of course, would be an array. Shamus Young some time back did a programming experiment into Minecraft-like block worlds similar to what you are doing, though fully 3D. He termed his experiment Project Octant, because he thought from the start that an octree would be an appropriate structure. And octrees can, indeed, optimize the operations involved in rendering and culling. However, when it came to editing the terrain dynamically, the abstraction of the octree started to get in his way and penalize him, significantly, in the area of performance. So much so that he ditched the octree in favor of a basic dumb array, which is blindingly fast as far as dynamic editing, but doesn't help you much at all in rendering. Fortunately, a top-down orthographically projected isometric world doesn't suffer any where near the culling problems faced by one in which the camera can be facing any direction at any given time. You can do a quick rough pass of culling using the AABB of your view frustum, which distills down to a range of X,Y and Z coordinates that can be used to iterate the cell grid. Easy peasy. 4)If the blocks are opaque, or feature only alpha-tested transparency, they should be sorted and drawn from front to back, to take advantage of depth rejection when drawing deeper blocks. (If a pixel is already drawn, and the depth test fails, then the deeper pixel is not rendered.) If the blocks feature partial transparency or translucency, they will need to be drawn back to front, resulting in quite a bit of overdraw. You can hybridize it by drawing in two passes: first, draw the opaque objects front to back, then draw the alpha-blended objects back to front. 5) Each block will consist of a bit of geometry onto which a texture is projected. In this thread I talk about it a bit more. Each block is a specially UV-mapped bit of geometry that is mapped with a texture based on its type. Additionally, you could try alternative texturing schemes. For example, my current project uses tri-planar texturing (on hex blocks instead of cubes) with scaling so that the repeating textures do not align with cell boundaries. This offsets some of the repeating of textures. screenshot     Thanks for the replies guys. I'll try to address what people have said here.   So my reasoning was that should I make say 100,000 or more draw calls to create individual sprites for each block then I would get huge performance hits wouldn't I? Rendering the blocks to a texture reduces the number of draw calls required by quite a bit...   As for the pathfinding. In the final version of the terrain I intend on having half blocks which create the face of hilled areas so as to be more smooth, while mountains I thought should remain more difficult to traverse.   I don't particularly understand 3D noise all that well as I've only worked with 2D and 1D noise patterns before. But I suppose I could try it. Any tips in that regard?   Are you saying that I should be doing this in 3D rather than 2D? I'm not particularly interested in that approach if I can help it.
  9. Ok, I have another Question as well. How do I go about creating multiple textures and putting them side by side while keeping the map continuous? The reason I am wanting to do this, is there are also performance issues with rendering huge textures for large maps of say several thousand block tiles to the screen. I would need to split the texture into parts or chunks wouldn't? But how do I go about lining them up and rendering to the correct texture for each block?
  10. Hi, so I'm working on a 2D isometric style game with block terrain generation. So far I have set up code for the terrain generation with both DirectX 11's Direct2D in Windows and SFML in Linux. I'd like to make the game cross platform and so far that aspect is working well.   I have a few questions maybe someone can fill me in on, but first I should show you my code so far (at least the terrain generation part) and the output I'm getting. Like so: for (int x = 0; x < MapWidth; x++) { for (int y = 0; y < MapHeight; y++) { // Get the depth value. float nOffX = (float)x / (float)(MapWidth - 1); float nOffY = (float)y / (float)(MapHeight - 1); float nX = 128.0f + nOffX * (256.0f - 128.0f); float nY = 128.0f + nOffY * (256.0f - 128.0f); int MaxDepth = lround(perlinNoise.GetValue(nX,nY, 0) * 10); MaxDepth = noise::ClampValue(MaxDepth, 0, MapDepth); for (int z = -1; z < MaxDepth - layer; z++) { // Adjust for Isometric Coordinates. int isoX = (((x - (z * 2)) + y) * TileWidth / 4); int isoY = ((y - x) * TileHeight / 2); isoX += (600 / 4) - (TileWidth / 2); isoY += (800 / 2) - (TileHeight / 2); terrainImage.copy(rawImage, isoY, isoX,sf::IntRect(0,0,32,32), true); } } } So this is the code I'm using to create the terrain in Isometric adjusted 2D space. This is the SFML version from linux for those not familiar. The code in DirectX is essentially the same except I use the Draw methods there for creating the sprite texture. I'm creating it as one texture and then drawing it to the screen as I figured that I should probably use one draw call for the terrain or I would have major performance issues when rendering huge chunks of blocks. The output of this code looks like this:     The style of game I'm imagining is something like dwarf fortress with graphics mixed with elements from some other genres of games such as rpgs and rts. So in other words you will be in control of a "Kingdom" style of game as the player building and defending your kingdom of isometric 2D blocks from invaders, monsters, other kingdoms, ect...   I realize this style of game has been done before in a couple of instances, but I'd like to make my own as I love this style of game alot. And I think I could make a really good instance of the god game or kingdom builder genre if you will.   This leads me to a couple of Questions about creating a game like this:   Question 1: How do I go about getting better output from the noise algorithm and terrain generation algorithm I'm using? I need the terrain to be a bit more natural looking with hills, and valleys, and possibly even mountains when I go to a much large grid say something like 1024 by 1024 tiles.   Question 2: Is there a better way to go about generating the terrain? And will the way I've chosen lead to any issues when I start doing pathfinding stuff for the characters?   Question 3: How do I go about adding and removing blocks?   Question 4: How should I go about drawing the blocks in real time in a "layered" like fashion? I need the player to be able to run through the layers of blocks for adding and removing blocks in a mining style fashion.   That's all the questions I have for now, and probably the most essential ones for making this style of game. Any help or thoughts on my project/game would be greatly appreciated!   Edit: Also sorry if this is in the wrong forum... I'm kind of new here and not used to posting in this forum. If it is could a moderator please move it to the correct forum where I am more likely to get help with this topic? I wasn't sure where to put this since it's cross platform and cross API development and kind of generalized in questions... thanks again.
  11. Hi, thank you for responding. :)   I'm a little unsure of what it is you're saying. As far as I can tell I am already doing what you are saying (or attempting to), it's just not working for me.   My first switch for and switch loop itirates through all the sprites, but it updates their movement and then handles collisions that occur (for each ball).   My problem as far as I can tell is not in the collision detection, my algorithm there already works using the bounding boxes, it's in the response to the collision. I am trying to seperate the balls accurately when they collide, however multiple collisions are being detected when I try to move them, so they bounce around eachother.   Interestingly if the balls touch pretty much completely diagnoly (where the corners touch), there is no bouncing.   This tells me there is likely something wrong with the way I am trying to seperate them. I have tried almost everything I can think of and still it does not work.   What I am currently doing is calculating the overlaping distance of the collision when it occurs and then moving either A (if it's one of the green balls being collided with) or B (if it's the main sprite being collided with). Then I reverse the velocities of the balls so that in theory they move away from eachother. This is however not working in all cases and I do not know why. Sometimes only 1 collision registers (I display the CollisionCounts variable to the screen so I can see it increase when they touch), and sometimes I get as many as 3 or 4 collisions at once and the balls kind of blip as a result.
  12. I have run into a problem that I am unable to resolve while writing a small DirectX 9 game involving the Collision handling portion of the game.   I have 1 ball that I can control, and 2 balls that bounce around the screen. I want them to also bounce off each other and the one I control without moving it.   This is my code so far: void Game::Update(float dt) { float centeraX; float centeraY; float centerbX; float centerbY; double overX; double overY; //Retrieve the keyboard array ZeroMemory(m_keys, sizeof(m_keys)); for(int a = 0; a < MAX_SPRITES; a++) { switch(a) { /****************************************************/ /* Update the main sprite */ /****************************************************/ case MAIN_SPRITE: //Check the keyboard and see if any keys are pressed switch(m_pInput->GetDeviceState(sizeof(m_keys), m_keys)) { //If both the up and left keys are pressed case UPLEFTDIRECTION: m_Sprites[a].m_MoveY = -2; m_Sprites[a].m_MoveX = -2; m_Sprites[a].m_IsMoving = true; break; //If both the up and right keys are pressed case UPRIGHTDIRECTION: m_Sprites[a].m_MoveY = -2; m_Sprites[a].m_MoveX = 2; m_Sprites[a].m_IsMoving = true; break; //If both the down and left keys are pressed case DOWNLEFTDIRECTION: m_Sprites[a].m_MoveY = 2; m_Sprites[a].m_MoveX = -2; m_Sprites[a].m_IsMoving = true; break; //If both the down and right keys are pressed case DOWNRIGHTDIRECTION: m_Sprites[a].m_MoveY = 2; m_Sprites[a].m_MoveX = 2; m_Sprites[a].m_IsMoving = true; break; //If the up key is pressed case UPDIRECTION: m_Sprites[a].m_MoveY = -2; m_Sprites[a].m_IsMoving = true; break; //If the down key is pressed case DOWNDIRECTION: m_Sprites[a].m_MoveY = 2; m_Sprites[a].m_IsMoving = true; break; //If the left key is pressed case LEFTDIRECTION: m_Sprites[a].m_MoveX = -2; m_Sprites[a].m_IsMoving = true; break; //If the right key is pressed case RIGHTDIRECTION: m_Sprites[a].m_MoveX = 2; m_Sprites[a].m_IsMoving = true; break; default: m_Sprites[a].m_MoveX = 0; m_Sprites[a].m_MoveY = 0; m_Sprites[a].m_IsMoving = false; break; } /******************************************/ /* Collision Detection */ /******************************************/ if(m_Sprites[a].OnScreen() == TOP) { m_Sprites[a].m_Pos.y = 0; m_Sprites[a].m_MoveY = 0; } if(m_Sprites[a].OnScreen() == BOTTOM) { m_Sprites[a].m_Pos.y = m_pGraphics->GetScreenHeight() - m_Sprites[a].m_Height; m_Sprites[a].m_MoveY = 0; } if(m_Sprites[a].OnScreen() == LEFT) { m_Sprites[a].m_Pos.x = 0; m_Sprites[a].m_MoveX = 0; } if(m_Sprites[a].OnScreen() == RIGHT) { m_Sprites[a].m_Pos.x = m_pGraphics->GetScreenWidth() - m_Sprites[a].m_Width; m_Sprites[a].m_MoveX = 0; } for(int b = 0; b < MAX_SPRITES; b++) { if(m_Sprites[a].m_Occupied && a != b && !m_Sprites[a].m_CollisionStatus) { switch(m_Sprites[a].GetCollisionStatus(&m_Sprites[b])) { case TOPLEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; g_CollisionCount++; if(m_debug) { g_debug.open("debug.txt", std::ofstream::app); g_debug<<g_CollisionCount<<" TOPLEFT\n\n"; g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug<<"OverX = "<<overX<<"\n"; g_debug<<"OverY = "<<overY<<"\n\n"; } m_Sprites[a].m_MoveY = 0; m_Sprites[a].m_MoveX = 0; m_Sprites[b].m_Pos.y += overY + m_Sprites[a].m_BoundingOffsetH + 1; m_Sprites[b].m_Pos.x += overX + m_Sprites[a].m_BoundingOffsetW + 1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[b].UpdateSpritePos(m_Rate); m_Sprites[b].m_CollisionStatus = true; if(m_debug) { g_debug<<"After Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug.close(); } break; case TOPRIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; g_CollisionCount++; if(m_debug) { g_debug.open("debug.txt", std::ofstream::app); g_debug<<g_CollisionCount<<" TOPRIGHT\n\n"; g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug<<"OverX = "<<overX<<"\n"; g_debug<<"OverY = "<<overY<<"\n\n"; } m_Sprites[a].m_MoveY = 0; m_Sprites[a].m_MoveX = 0; m_Sprites[b].m_Pos.y -= overY + m_Sprites[a].m_BoundingOffsetH + 1; m_Sprites[b].m_Pos.x += overX + m_Sprites[a].m_BoundingOffsetW + 1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[b].UpdateSpritePos(m_Rate); m_Sprites[b].m_CollisionStatus = true; if(m_debug) { g_debug<<"After Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; } g_debug.close(); break; case BOTTOMLEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top; g_CollisionCount++; if(m_debug) { g_debug.open("debug.txt", std::ofstream::app); g_debug<<g_CollisionCount<<" BOTTOMLEFT\n\n"; g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug<<"OverX = "<<overX<<"\n"; g_debug<<"OverY = "<<overY<<"\n\n"; } m_Sprites[a].m_MoveY = 0; m_Sprites[a].m_MoveX = 0; m_Sprites[b].m_Pos.y += overY + m_Sprites[a].m_BoundingOffsetH + 1; m_Sprites[b].m_Pos.x -= overX + m_Sprites[a].m_BoundingOffsetW + 1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[b].UpdateSpritePos(m_Rate); m_Sprites[b].m_CollisionStatus = true; if(m_debug) { g_debug<<"After Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug.close(); } break; case BOTTOMRIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top; g_CollisionCount++; if(m_debug) { g_debug.open("debug.txt", std::ofstream::app); g_debug<<g_CollisionCount<<" BOTTOMRIGHT\n\n"; g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug<<"OverX = "<<overX<<"\n"; g_debug<<"OverY = "<<overY<<"\n\n"; } m_Sprites[a].m_MoveY = 0; m_Sprites[a].m_MoveX = 0; m_Sprites[b].m_Pos.y -= overY + m_Sprites[a].m_BoundingOffsetH + 1; m_Sprites[b].m_Pos.x -= overX + m_Sprites[a].m_BoundingOffsetW + 1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[b].UpdateSpritePos(m_Rate); m_Sprites[b].m_CollisionStatus = true; if(m_debug) { g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug.close(); } break; } } } /*********************************************************/ /* Update the Sprite's Position */ /*********************************************************/ m_Sprites[a].UpdateSpritePos(m_Rate); break; //Update GreenBall1 case GREEN_BALL1: if(m_Sprites[a].OnScreen() == TOP) { m_Sprites[a].m_Pos.y = 0; m_Sprites[a].m_MoveY *= -1; } if(m_Sprites[a].OnScreen() == BOTTOM) { m_Sprites[a].m_Pos.y = m_pGraphics->GetScreenHeight() - m_Sprites[a].m_Height; m_Sprites[a].m_MoveY *= -1; } if(m_Sprites[a].OnScreen() == LEFT) { m_Sprites[a].m_Pos.x = 0; m_Sprites[a].m_MoveX *= -1; } if(m_Sprites[a].OnScreen() == RIGHT) { m_Sprites[a].m_Pos.x = m_pGraphics->GetScreenWidth() - m_Sprites[a].m_Width; m_Sprites[a].m_MoveX *= -1; } for(int b = 0; b < MAX_SPRITES; b++) { if(m_Sprites[a].m_Occupied && a != b && !m_Sprites[a].m_CollisionStatus) { switch(m_Sprites[a].GetCollisionStatus(&m_Sprites[b])) { case TOPLEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; g_CollisionCount++; if(m_debug) { g_debug.open("debug.txt", std::ofstream::app); g_debug<<g_CollisionCount<<" TOPLEFT\n\n"; g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug<<"OverX = "<<overX<<"\n"; g_debug<<"OverY = "<<overY<<"\n\n"; } m_Sprites[a].m_Pos.y -= overY + m_Sprites[a].m_BoundingOffsetH + 1; m_Sprites[a].m_Pos.x -= overX + m_Sprites[a].m_BoundingOffsetW + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateSpritePos(m_Rate); m_Sprites[b].UpdateSpritePos(m_Rate); m_Sprites[b].m_CollisionStatus = true; if(m_debug) { g_debug<<g_CollisionCount<<" After Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug.close(); } break; case TOPRIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; g_CollisionCount++; if(m_debug) { g_debug.open("debug.txt", std::ofstream::app); g_debug<<g_CollisionCount<<" TOPRIGHT\n\n"; g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug<<"OverX = "<<overX<<"\n"; g_debug<<"OverY = "<<overY<<"\n\n"; } m_Sprites[a].m_Pos.y += overY + m_Sprites[a].m_BoundingOffsetH + 1; m_Sprites[a].m_Pos.x -= overX + m_Sprites[a].m_BoundingOffsetW + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateSpritePos(m_Rate); m_Sprites[b].UpdateSpritePos(m_Rate); m_Sprites[b].m_CollisionStatus = true; if(m_debug) { g_debug<<"After Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug.close(); } break; case BOTTOMLEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top; g_CollisionCount++; if(m_debug) { g_debug.open("debug.txt", std::ofstream::app); g_debug<<g_CollisionCount<<" BOTTOMLEFT\n\n"; g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug<<"OverX = "<<overX<<"\n"; g_debug<<"OverY = "<<overY<<"\n\n"; } m_Sprites[a].m_Pos.y -= overY + m_Sprites[a].m_BoundingOffsetH + 1; m_Sprites[a].m_Pos.x += overX + m_Sprites[a].m_BoundingOffsetW + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateSpritePos(m_Rate); m_Sprites[b].UpdateSpritePos(m_Rate); m_Sprites[b].m_CollisionStatus = true; if(m_debug) { g_debug<<"After Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; } g_debug.close(); break; case BOTTOMRIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top; g_CollisionCount++; if(m_debug) { g_debug.open("debug.txt", std::ofstream::app); g_debug<<g_CollisionCount<<" BOTTOMRIGHT\n\n"; g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug<<"OverX = "<<overX<<"\n"; g_debug<<"OverY = "<<overY<<"\n\n"; } m_Sprites[a].m_Pos.y += overY + m_Sprites[a].m_BoundingOffsetH + 1; m_Sprites[a].m_Pos.x += overX + m_Sprites[a].m_BoundingOffsetW + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateSpritePos(m_Rate); m_Sprites[b].UpdateSpritePos(m_Rate); m_Sprites[b].m_CollisionStatus = true; if(m_debug) { g_debug<<"After Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; } g_debug.close(); break; } } } if(!m_Sprites[a].m_CollisionStatus) { m_Sprites[a].UpdateSpritePos(m_Rate); } else { m_Sprites[a].m_CollisionStatus = false; } break; //Update GreenBall2 case GREEN_BALL2: if(m_Sprites[a].OnScreen() == TOP) { m_Sprites[a].m_Pos.y = 0; m_Sprites[a].m_MoveY *= -1; } if(m_Sprites[a].OnScreen() == BOTTOM) { m_Sprites[a].m_Pos.y = m_pGraphics->GetScreenHeight() - m_Sprites[a].m_Height; m_Sprites[a].m_MoveY *= -1; } if(m_Sprites[a].OnScreen() == LEFT) { m_Sprites[a].m_Pos.x = 0; m_Sprites[a].m_MoveX *= -1; } if(m_Sprites[a].OnScreen() == RIGHT) { m_Sprites[a].m_Pos.x = m_pGraphics->GetScreenWidth() - m_Sprites[a].m_Width; m_Sprites[a].m_MoveX *= -1; } for(int b = 0; b < MAX_SPRITES; b++) { if(m_Sprites[a].m_Occupied && a != b && !m_Sprites[a].m_CollisionStatus) { switch(m_Sprites[a].GetCollisionStatus(&m_Sprites[b])) { case TOPLEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; g_CollisionCount++; if(m_debug) { g_debug.open("debug.txt", std::ofstream::app); g_debug<<g_CollisionCount<<" TOPLEFT\n\n"; g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug<<"OverX = "<<overX<<"\n"; g_debug<<"OverY = "<<overY<<"\n\n"; } m_Sprites[a].m_Pos.y -= overY + m_Sprites[a].m_BoundingOffsetH + 1; m_Sprites[a].m_Pos.x -= overX + m_Sprites[a].m_BoundingOffsetW + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateSpritePos(m_Rate); m_Sprites[b].UpdateSpritePos(m_Rate); m_Sprites[b].m_CollisionStatus = true; if(m_debug) { g_debug<<"After Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug.close(); } break; case TOPRIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; g_CollisionCount++; if(m_debug) { g_debug.open("debug.txt", std::ofstream::app); g_debug<<g_CollisionCount<<" TOPRIGHT\n\n"; g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug<<"OverX = "<<overX<<"\n"; g_debug<<"OverY = "<<overY<<"\n\n"; } m_Sprites[a].m_Pos.y += overY + m_Sprites[a].m_BoundingOffsetH + 1; m_Sprites[a].m_Pos.x -= overX + m_Sprites[a].m_BoundingOffsetW + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateSpritePos(m_Rate); m_Sprites[b].UpdateSpritePos(m_Rate); m_Sprites[b].m_CollisionStatus = true; if(m_debug) { g_debug<<"After Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; } g_debug.close(); break; case BOTTOMLEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top; g_CollisionCount++; if(m_debug) { g_debug.open("debug.txt", std::ofstream::app); g_debug<<g_CollisionCount<<" BOTTOMLEFT\n\n"; g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug<<"OverX = "<<overX<<"\n"; g_debug<<"OverY = "<<overY<<"\n\n"; } m_Sprites[a].m_Pos.y -= overY + m_Sprites[a].m_BoundingOffsetH + 1; m_Sprites[a].m_Pos.x += overX + m_Sprites[a].m_BoundingOffsetW + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateSpritePos(m_Rate); m_Sprites[b].UpdateSpritePos(m_Rate); m_Sprites[b].m_CollisionStatus = true; if(m_debug) { g_debug<<"After Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; } g_debug.close(); break; case BOTTOMRIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top; g_CollisionCount++; if(m_debug) { g_debug.open("debug.txt", std::ofstream::app); g_debug<<g_CollisionCount<<" BOTTOMRIGHT\n\n"; g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug<<"OverX = "<<overX<<"\n"; g_debug<<"OverY = "<<overY<<"\n\n"; } m_Sprites[a].m_Pos.y += overY + m_Sprites[a].m_BoundingOffsetH + 1; m_Sprites[a].m_Pos.x += overX + m_Sprites[a].m_BoundingOffsetW + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateSpritePos(m_Rate); m_Sprites[b].UpdateSpritePos(m_Rate); m_Sprites[b].m_CollisionStatus = true; if(m_debug) { g_debug<<"Before Collision:\n\n"; g_debug<<"Ax = "<<m_Sprites[a].m_Pos.x<<"\n"; g_debug<<"Ay = "<<m_Sprites[a].m_Pos.y<<"\n"; g_debug<<"Atop = "<<m_Sprites[a].m_Bounding.top<<"\n"; g_debug<<"Aleft = "<<m_Sprites[a].m_Bounding.left<<"\n"; g_debug<<"Abottom = "<<m_Sprites[a].m_Bounding.bottom<<"\n"; g_debug<<"Aright = "<<m_Sprites[a].m_Bounding.right<<"\n\n"; g_debug<<"Bx = "<<m_Sprites[b].m_Pos.x<<"\n"; g_debug<<"By = "<<m_Sprites[b].m_Pos.y<<"\n"; g_debug<<"Btop = "<<m_Sprites[b].m_Bounding.top<<"\n"; g_debug<<"Bleft = "<<m_Sprites[b].m_Bounding.left<<"\n"; g_debug<<"Bbottom = "<<m_Sprites[b].m_Bounding.bottom<<"\n"; g_debug<<"Bright = "<<m_Sprites[b].m_Bounding.right<<"\n\n"; g_debug.close(); } break; } } } if(!m_Sprites[a].m_CollisionStatus) { m_Sprites[a].UpdateSpritePos(m_Rate); } else { m_Sprites[a].m_CollisionStatus = false; } break; } } } int Sprite::GetCollisionStatus(Sprite* sprite) { int collisionstatus = NULL; RECT a; RECT b; a.top = m_Bounding.top; a.bottom = m_Bounding.bottom; a.left = m_Bounding.left; a.right = m_Bounding.right; b.top = sprite->m_Bounding.top; b.bottom = sprite->m_Bounding.bottom; b.left = sprite->m_Bounding.left; b.right = sprite->m_Bounding.right; if(m_Index != sprite->m_Index) { if(a.bottom < b.top || a.top > b.bottom || a.right < b. left || a.left > b.right) { collisionstatus = NOCOLLISION; } else { //If A's top is less than B's top then A is overlapping B's top if(a.top < b.top) { collisionstatus += TOP; } //If A's top is greater than B's top then A is overlapping B's bottom if(a.top > b.top) { collisionstatus += BOTTOM; } //If A's left is less than B's left then A is overlapping B's left if( a.left < b.left) { collisionstatus += LEFT; } //If A's left is greater than B's left then A is overlapping B's right if( a.left > b.left) { collisionstatus += RIGHT; } } } return collisionstatus; } void Sprite::UpdateSpritePos(float dt) { m_AnimRate = dt; m_Pos.x += m_MoveX * m_AnimRate; m_Pos.y += m_MoveY * m_AnimRate; m_Bounding.top = m_Pos.y + m_BoundingOffsetH; m_Bounding.left = m_Pos.x + m_BoundingOffsetW; m_Bounding.bottom = m_Pos.y + m_OffsetHeight; m_Bounding.right = m_Pos.x + m_OffsetWidth; } The balls are not behaving as they should because multiple collisions are occuring when they touch and I don't know why. I have exhaustively looked through my code and algorithms, and looked through online collision detection resources and I can not figure out what I am doing wrong. Please Help if you can.
  13. Can no one help me? Or am I just in the wrong forum? :(
  14. So I've made a few changes to my code here they are: void Game::Update(float dt) { float centeraX; float centeraY; float centerbX; float centerbY; double overX; double overY; //Retrieve the keyboard array ZeroMemory(m_keys, sizeof(m_keys)); for(int a = 0; a < MAX_SPRITES; a++) { switch(a) { /****************************************************/ /* Update the main sprite */ /****************************************************/ case MAIN_SPRITE: //Check the keyboard and see if any keys are pressed switch(m_pInput->GetDeviceState(sizeof(m_keys), m_keys)) { //If both the up and left keys are pressed case UPLEFTDIRECTION: m_Sprites[a].m_MoveY = -2; m_Sprites[a].m_MoveX = -2; m_Sprites[a].m_IsMoving = true; break; //If both the up and right keys are pressed case UPRIGHTDIRECTION: m_Sprites[a].m_MoveY = -2; m_Sprites[a].m_MoveX = 2; m_Sprites[a].m_IsMoving = true; break; //If both the down and left keys are pressed case DOWNLEFTDIRECTION: m_Sprites[a].m_MoveY = 2; m_Sprites[a].m_MoveX = -2; m_Sprites[a].m_IsMoving = true; break; //If both the down and right keys are pressed case DOWNRIGHTDIRECTION: m_Sprites[a].m_MoveY = 2; m_Sprites[a].m_MoveX = 2; m_Sprites[a].m_IsMoving = true; break; //If the up key is pressed case UPDIRECTION: m_Sprites[a].m_MoveY = -2; m_Sprites[a].m_IsMoving = true; break; //If the down key is pressed case DOWNDIRECTION: m_Sprites[a].m_MoveY = 2; m_Sprites[a].m_IsMoving = true; break; //If the left key is pressed case LEFTDIRECTION: m_Sprites[a].m_MoveX = -2; m_Sprites[a].m_IsMoving = true; break; //If the right key is pressed case RIGHTDIRECTION: m_Sprites[a].m_MoveX = 2; m_Sprites[a].m_IsMoving = true; break; default: m_Sprites[a].m_MoveX = 0; m_Sprites[a].m_MoveY = 0; m_Sprites[a].m_IsMoving = false; break; } /******************************************/ /* Collision Detection */ /******************************************/ if(m_Sprites[a].OnScreen() == TOP) { m_Sprites[a].m_Pos.y = 0; m_Sprites[a].m_MoveY = 0; } if(m_Sprites[a].OnScreen() == BOTTOM) { m_Sprites[a].m_Pos.y = m_pGraphics->GetScreenHeight() - m_Sprites[a].m_Height; m_Sprites[a].m_MoveY = 0; } if(m_Sprites[a].OnScreen() == LEFT) { m_Sprites[a].m_Pos.x = 0; m_Sprites[a].m_MoveX = 0; } if(m_Sprites[a].OnScreen() == RIGHT) { m_Sprites[a].m_Pos.x = m_pGraphics->GetScreenWidth() - m_Sprites[a].m_Width; m_Sprites[a].m_MoveX = 0; } for(int b = 0; b < MAX_SPRITES; b++) { if(m_Sprites[a].m_Occupied && a != b) { centeraX = m_Sprites[a].m_Pos.x + (m_Sprites[a].m_Width / 2); centeraY = m_Sprites[a].m_Pos.y + (m_Sprites[a].m_Height / 2); centerbX = m_Sprites[b].m_Pos.x + (m_Sprites[b].m_Width / 2); centerbY = m_Sprites[b].m_Pos.y + (m_Sprites[b].m_Height / 2); switch(m_Sprites[a].GetCollisionStatus(&m_Sprites[b])) { case TOPLEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; m_Sprites[b].m_Pos.y += overY + 1; m_Sprites[b].m_Pos.x += overX + 1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[b].UpdateBounding(); break; case TOPRIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; m_Sprites[b].m_Pos.y -= overY + 1; m_Sprites[b].m_Pos.x += overX + 1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[b].UpdateBounding(); break; case BOTTOMLEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; overY = m_Sprites[b].m_Bounding.top - m_Sprites[a].m_Bounding.bottom; m_Sprites[b].m_Pos.y += overY + 1; m_Sprites[b].m_Pos.x -= overX + 1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[b].UpdateBounding(); break; case BOTTOMRIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top; m_Sprites[b].m_Pos.y -= overY + 1; m_Sprites[b].m_Pos.x -= overX + 1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[b].UpdateBounding(); break; case TOP: overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; m_Sprites[b].m_Pos.y += overY + 1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].UpdateBounding(); break; case BOTTOM: overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top; m_Sprites[b].m_Pos.y -= overY + 1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].UpdateBounding(); break; case LEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; m_Sprites[b].m_Pos.x += overX + 1; m_Sprites[b].m_MoveX *= -1; m_Sprites[b].UpdateBounding(); break; case RIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; m_Sprites[b].m_Pos.x -= overX + 1; m_Sprites[b].m_MoveX *= -1; m_Sprites[b].UpdateBounding(); break; } } } /*********************************************************/ /* Update the Sprite's Position */ /*********************************************************/ m_Sprites[a].UpdateSpritePos(m_Rate); break; //Update GreenBall1 case GREEN_BALL1: if(m_Sprites[a].OnScreen() == TOP) { m_Sprites[a].m_Pos.y = 0; m_Sprites[a].m_MoveY *= -1; } if(m_Sprites[a].OnScreen() == BOTTOM) { m_Sprites[a].m_Pos.y = m_pGraphics->GetScreenHeight() - m_Sprites[a].m_Height; m_Sprites[a].m_MoveY *= -1; } if(m_Sprites[a].OnScreen() == LEFT) { m_Sprites[a].m_Pos.x = 0; m_Sprites[a].m_MoveX *= -1; } if(m_Sprites[a].OnScreen() == RIGHT) { m_Sprites[a].m_Pos.x = m_pGraphics->GetScreenWidth() - m_Sprites[a].m_Width; m_Sprites[a].m_MoveX *= -1; } for(int b = 0; b < MAX_SPRITES; b++) { if(m_Sprites[a].m_Occupied && a != b) { centeraX = m_Sprites[a].m_Pos.x + (m_Sprites[a].m_Width / 2); centeraY = m_Sprites[a].m_Pos.y + (m_Sprites[a].m_Height / 2); centerbX = m_Sprites[b].m_Pos.x + (m_Sprites[b].m_Width / 2); centerbY = m_Sprites[b].m_Pos.y + (m_Sprites[b].m_Height / 2); switch(m_Sprites[a].GetCollisionStatus(&m_Sprites[b])) { case TOPLEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; m_Sprites[a].m_Pos.y -= overY + 1; m_Sprites[a].m_Pos.x -= overX + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateBounding(); break; case TOPRIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; m_Sprites[a].m_Pos.y += overY + 1; m_Sprites[a].m_Pos.x -= overX + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateBounding(); break; case BOTTOMLEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; overY = m_Sprites[a].m_Bounding.top - m_Sprites[b].m_Bounding.bottom; m_Sprites[a].m_Pos.y -= overY + 1; m_Sprites[a].m_Pos.x += overX + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateBounding(); break; case BOTTOMRIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; overY = m_Sprites[b].m_Bounding.top - m_Sprites[a].m_Bounding.top; m_Sprites[a].m_Pos.y += overY + 1; m_Sprites[a].m_Pos.x += overX + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateBounding(); break; case TOP: overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; m_Sprites[a].m_Pos.y += overY + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[a].UpdateBounding(); break; case BOTTOM: overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top; m_Sprites[a].m_Pos.y -= overY + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[a].UpdateBounding(); break; case LEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; m_Sprites[a].m_Pos.x += overX + 1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateBounding(); break; case RIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; m_Sprites[a].m_Pos.x -= overX + 1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateBounding(); break; } } } m_Sprites[a].UpdateSpritePos(m_Rate); break; //Update GreenBall2 case GREEN_BALL2: if(m_Sprites[a].OnScreen() == TOP) { m_Sprites[a].m_Pos.y = 0; m_Sprites[a].m_MoveY *= -1; } if(m_Sprites[a].OnScreen() == BOTTOM) { m_Sprites[a].m_Pos.y = m_pGraphics->GetScreenHeight() - m_Sprites[a].m_Height; m_Sprites[a].m_MoveY *= -1; } if(m_Sprites[a].OnScreen() == LEFT) { m_Sprites[a].m_Pos.x = 0; m_Sprites[a].m_MoveX *= -1; } if(m_Sprites[a].OnScreen() == RIGHT) { m_Sprites[a].m_Pos.x = m_pGraphics->GetScreenWidth() - m_Sprites[a].m_Width; m_Sprites[a].m_MoveX *= -1; } for(int b = 0; b < MAX_SPRITES; b++) { if(m_Sprites[a].m_Occupied && a != b) { centeraX = m_Sprites[a].m_Pos.x + (m_Sprites[a].m_Width / 2); centeraY = m_Sprites[a].m_Pos.y + (m_Sprites[a].m_Height / 2); centerbX = m_Sprites[b].m_Pos.x + (m_Sprites[b].m_Width / 2); centerbY = m_Sprites[b].m_Pos.y + (m_Sprites[b].m_Height / 2); switch(m_Sprites[a].GetCollisionStatus(&m_Sprites[b])) { case TOPLEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; m_Sprites[a].m_Pos.y -= overY + 1; m_Sprites[a].m_Pos.x -= overX + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateBounding(); break; case TOPRIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; m_Sprites[a].m_Pos.y += overY + 1; m_Sprites[a].m_Pos.x -= overX + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateBounding(); break; case BOTTOMLEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; overY = m_Sprites[a].m_Bounding.top - m_Sprites[b].m_Bounding.bottom; m_Sprites[a].m_Pos.y -= overY + 1; m_Sprites[a].m_Pos.x += overX + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateBounding(); break; case BOTTOMRIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; overY = m_Sprites[b].m_Bounding.top - m_Sprites[a].m_Bounding.top; m_Sprites[a].m_Pos.y += overY + 1; m_Sprites[a].m_Pos.x += overX + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateBounding(); break; case TOP: overY = m_Sprites[a].m_Bounding.bottom - m_Sprites[b].m_Bounding.top; m_Sprites[a].m_Pos.y += overY + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[a].UpdateBounding(); break; case BOTTOM: overY = m_Sprites[b].m_Bounding.bottom - m_Sprites[a].m_Bounding.top; m_Sprites[a].m_Pos.y -= overY + 1; m_Sprites[a].m_MoveY *= -1; m_Sprites[b].m_MoveY *= -1; m_Sprites[a].UpdateBounding(); break; case LEFT: overX = m_Sprites[a].m_Bounding.right - m_Sprites[b].m_Bounding.left; m_Sprites[a].m_Pos.x += overX + 1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateBounding(); break; case RIGHT: overX = m_Sprites[b].m_Bounding.right - m_Sprites[a].m_Bounding.left; m_Sprites[a].m_Pos.x -= overX + 1; m_Sprites[a].m_MoveX *= -1; m_Sprites[b].m_MoveX *= -1; m_Sprites[a].UpdateBounding(); break; } } } m_Sprites[a].UpdateSpritePos(m_Rate); break; } } } void Sprite::UpdateBounding() { m_Bounding.top = m_Pos.y + m_BoundingOffsetH; m_Bounding.left = m_Pos.x + m_BoundingOffsetW; m_Bounding.bottom = m_Pos.y + m_OffsetHeight; m_Bounding.right = m_Pos.x + m_OffsetWidth; } void Sprite::UpdateSpritePos(float dt) { m_AnimRate = dt; m_Pos.x += m_MoveX * m_AnimRate; m_Pos.y += m_MoveY * m_AnimRate; m_Bounding.top = m_Pos.y + m_BoundingOffsetH; m_Bounding.left = m_Pos.x + m_BoundingOffsetW; m_Bounding.bottom = m_Pos.y + m_OffsetHeight; m_Bounding.right = m_Pos.x + m_OffsetWidth; } int Sprite::GetCollisionStatus(Sprite* sprite) { int collisionstatus = NULL; RECT a; RECT b; a.top = m_Bounding.top; a.bottom = m_Bounding.bottom; a.left = m_Bounding.left; a.right = m_Bounding.right; b.top = sprite->m_Bounding.top; b.bottom = sprite->m_Bounding.bottom; b.left = sprite->m_Bounding.left; b.right = sprite->m_Bounding.right; if(m_Index != sprite->m_Index) { if(a.bottom < b.top || a.top > b.bottom || a.right < b. left || a.left > b.right) { collisionstatus = NOCOLLISION; } else { if(a.top < b.top) { collisionstatus += TOP; } if(a.top > b.top) { collisionstatus += BOTTOM; } if( a.left < b.left) { collisionstatus += LEFT; } if( a.left > b.left) { collisionstatus += RIGHT; } } } return collisionstatus; } The results are a bit better now, but they are still jumping around a bit when they collide.   Also is this forum even the correct place for this question?   Again, any help in this matter would be greatly appreciated.
  15. Hi, I'm attempting to make a small C++/Direct X 9 game and I'm running into problems with handling the collision detection of some sprites (which are bouncing balls, but I am using bounding boxes). The problem is in the behavior of the balls when they collide, they sometimes move too much or slightly in the opposite direction and then away from eachother. They do not hook on eachother, however. Here is the code with some short explanations as to what I'm doing:   Here is my collision detection function, as far as I can tell it works and is effective for my purposes atm: int Sprite::GetCollisionStatus(Sprite* sprite) { int collisionstatus = NULL; RECT a; RECT b; a.top = m_Bounding.top; a.bottom = m_Bounding.bottom; a.left = m_Bounding.left; a.right = m_Bounding.right; b.top = sprite->m_Bounding.top; b.bottom = sprite->m_Bounding.bottom; b.left = sprite->m_Bounding.left; b.right = sprite->m_Bounding.right; if(m_Index != sprite->m_Index) { if(a.bottom < b.top || a.top > b.bottom || a.right < b. left || a.left > b.right) { collisionstatus = NOCOLLISION; } else { if(a.top < b.top) { collisionstatus += TOP; } if(a.top > b.top) { collisionstatus += BOTTOM; } if( a.left < b.left) { collisionstatus += LEFT; } if( a.left > b.left) { collisionstatus += RIGHT; } } } return collisionstatus; } And here is the Collision handling portion of my Update function in the Game class. Note, this is ittirated for each sprite: for(int i = 0; i < MAX_SPRITES; i++) { switch(m_Sprites[c].GetCollisionStatus(&m_Sprites[i])) { case TOPLEFT: overX = abs(m_Sprites[c].m_Bounding.right - m_Sprites[i].m_Bounding.left); overY = abs(m_Sprites[c].m_Bounding.bottom - m_Sprites[i].m_Bounding.top); overX /= 2; overY /= 2; m_Sprites[c].m_Pos.y -= overY + 1; m_Sprites[c].m_Pos.x -= overX + 1; m_Sprites[c].m_MoveY *= -1; m_Sprites[c].m_MoveX *= -1; m_Sprites[i].m_Pos.y += overY + 1; m_Sprites[i].m_Pos.x += overX + 1; m_Sprites[i].m_MoveY *= -1; m_Sprites[i].m_MoveX *= -1; break; case TOPRIGHT: overX = abs(m_Sprites[i].m_Bounding.right - m_Sprites[c].m_Bounding.left); overY = abs(m_Sprites[c].m_Bounding.bottom - m_Sprites[i].m_Bounding.top); overX /= 2; overY /= 2; m_Sprites[c].m_Pos.y -= overY + 1; m_Sprites[c].m_Pos.x += overX + 1; m_Sprites[c].m_MoveY *= -1; m_Sprites[c].m_MoveX *= -1; m_Sprites[i].m_Pos.y += overY + 1; m_Sprites[i].m_Pos.x -= overX + 1; m_Sprites[i].m_MoveY *= -1; m_Sprites[i].m_MoveX *= -1; break; case BOTTOMLEFT: overX = abs(m_Sprites[i].m_Bounding.right - m_Sprites[i].m_Bounding.right); overY = abs(m_Sprites[c].m_Bounding.top - m_Sprites[i].m_Bounding.bottom); overX /= 2; overY /= 2; m_Sprites[i].m_Pos.y -= overY + 1; m_Sprites[i].m_Pos.x += overX + 1; m_Sprites[i].m_MoveY *= -1; m_Sprites[i].m_MoveX *= -1; m_Sprites[c].m_Pos.y += overY + 1; m_Sprites[c].m_Pos.x -= overX + 1; m_Sprites[c].m_MoveY *= -1; m_Sprites[c].m_MoveX *= -1; break; case BOTTOMRIGHT: overX = abs(m_Sprites[i].m_Bounding.right - m_Sprites[c].m_Bounding.left); overY = abs(m_Sprites[i].m_Bounding.top - m_Sprites[c].m_Bounding.top); overX /= 2; overY /= 2; m_Sprites[i].m_Pos.y -= overY + 1; m_Sprites[i].m_Pos.x -= overX + 1; m_Sprites[i].m_MoveY *= -1; m_Sprites[i].m_MoveX *= -1; m_Sprites[c].m_Pos.y += overY + 1; m_Sprites[c].m_Pos.x += overX + 1; m_Sprites[c].m_MoveY *= -1; m_Sprites[c].m_MoveX *= -1; break; case TOP: overY = abs(m_Sprites[c].m_Bounding.bottom - m_Sprites[i].m_Bounding.top); overY /= 2; m_Sprites[c].m_Pos.y -= overY + 1; m_Sprites[c].m_MoveY *= -1; m_Sprites[i].m_Pos.y += overY + 1; m_Sprites[i].m_MoveY *= -1; break; case BOTTOM: overY = abs(m_Sprites[i].m_Bounding.bottom - m_Sprites[c].m_Bounding.top); overY /= 2; m_Sprites[i].m_Pos.y -= overY + 1; m_Sprites[i].m_MoveY *= -1; m_Sprites[c].m_Pos.y += overY + 1; m_Sprites[c].m_MoveY *= -1; break; case LEFT: overX = abs(m_Sprites[c].m_Bounding.right - m_Sprites[i].m_Bounding.left); overX /= 2; m_Sprites[c].m_Pos.x -= overX + 1; m_Sprites[c].m_MoveX *= -1; m_Sprites[i].m_Pos.x += overX + 1; m_Sprites[i].m_MoveX *= -1; break; case RIGHT: overX = abs(m_Sprites[i].m_Bounding.right - m_Sprites[i].m_Bounding.left); overX /= 2; m_Sprites[i].m_Pos.x -= overX + 1; m_Sprites[i].m_MoveX *= -1; m_Sprites[c].m_Pos.x -= overX + 1; m_Sprites[c].m_MoveX *= +1; break; } } Any help in resolving this problem would be greatly appreciated. :)
  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!