Jump to content
  • Advertisement

Nazgulled

Member
  • Content Count

    5
  • Joined

  • Last visited

Community Reputation

104 Neutral

About Nazgulled

  • Rank
    Newbie
  1. Well, this is getting confusing lol... I appreciate your help but I don't care about this any longer (at least for now). This university project is due tomorrow and I can't be bothered with this problem anymore, I still need to finish the report about the whole thing (which I haven't started yet). I decided to go with bounding spheres, they seem to work just fine for this project. However, someone helped me out over at StackOverflow and I believe his answer does exactly what I want but I don't have time to test it and see if it really works. But I suppose it does so I'll just leave a link here for anyone else interested: http://stackoverflow.com/questions/6053522/how-to-recalculate-axis-aligned-bounding-box-after-translate-rotate Again, thank you for your time. Regards.
  2. I'm not saying I don't agree with you completely, you have some valid points but I don't agree with them all. But I'm not going to discuss the refactoring issue any longer, it's not relevant to my problem. Although I think you're making a big fuss out of nothing. It's not like the code I posted is unreadable or hard to understand because I didn't move a matrix-vector multiplication into it's ow function. That's nothing to compared some atrocities I see on other topics (not necessarily this forum). I'm always really careful when I post topics such as this in forums (or StackOverflow which I use more often). I compose my topic carefully and as easy to understand as possible, text and code wise. The better and more clear I make my post, the easier and faster someone will help me out. Basically, I help people help me. Yes, that's what I have, a min and max vector (that's in box.max and box.min). Let's say I load an object and draw it on screen without any translation/rotation. The object will be drawn at the origin and if I draw a box with the max/min vectors, everything is fine and looks good. The problem is that when I translate the max/min doesn't translate along, in other words, the max/min are in object space (like the object is at the origin) and I need them in world-space. That's easy to fix, when I need the translated AABB I just need to add the object's current position to max and min vectors. It gets tricky when we are talking about a rotation instead of translation. That's my problem. I don't know how to convert the max/min object-space vectors into world-space ones. Let me give an example... Let's say I have a cube of size 1.0 on all sides. Then max will be (0.5, 0.5, 0.5) while min will be (-0.5, -0.5, -0.5). Drawing a box around the object with these values is easy, I just need to calculate the 8 points of the box and draw it. If I translate along the x axis by 2. drawing a box around the object is still easy. I just need to add that to max and min, which will now be (2.5, 0.5, 0.5) and (-2.5, 0.5, 0.5) respectively. Again, with rotations is where it gets tricky. For simplification let's assume I didn't translate, just rotated, by 45º along the Y axis. Since this is a cube of size 1.0 on all sides, I know that the new max and min will be around (0.7, 0.5, 0.7) and (-0.7, -0.5, -0.7) respectively. How do I know that, well, rotating 45º on the Y axis makes the cube diagonals aligned with the X and Z axises and so I have to use those to calculate the new max and min. And the diagonal will be SQRT(1^2 + 1^2) = 1.4, which gives 0.7 when diving by 2 for the max/min vectors. But this doesn't work (or I don't know how to make it work) when the object is not a cube (but a rectangle instead, in other words, different sizes for all axises) and when the rotated angle is something different, like 30º. With 45º we have the diagonals aligned with the X and Z axises which is what we need for a good AABB. But rotation something different doesn't give us aligned diagonals and so we can't use them (or I don't know how to use them to make it work). Understand better my problem now? It's a little confusing and since English is not my main English, it can be hard for me sometimes to explain exactly what I want to say...
  3. Oh, that... Well, I don't think it would make sense to use AABB that way, I would rather use a Sphere instead (what I'm currently doing). If I choose to go with AABB, I want it to be as tight as possible on all axis. About the refactoring discussion, it's your opinion (and I'm sure others) and I respect it, but I don't fully agree with it. I think it all comes down on you and how you are more productive. I'm more productive if I refactor in the end. Specially when I don't know exactly what I'm doing and I'm just trying really hard to make things work, experimenting. IMO, it's pointless to keep refactoring the code in a situation like this. But like I said, this is just me, I achieve better results like this. Everyone should do what's best for them I still don't get it... Where is the rotated AABB? Where are the values for the box 8 corners? I understand that a rotated AABB is basically a OBB, but how do I get the values that define that AABB/OBB after the object rotation then? That's exactly what (I think) I'm trying to do by extracting the modelview matrix and generating the new world-space AABB that fully contains the object. That's the problem, is not working. The question now is, what I'm doing wrong? Or maybe I'm doing everything wrong and the question becomes, how do I do it then?
  4. I don't know what that means, could you clarify please? Sure, but if I waste too much time refactoring code like that and then end up with code that doesn't work or doesn't do what I want and need to recode everything, then I wasted precious time refactoring. First I'm trying to make things work as I want it, then I refactor and wrap code like that in a proper function. This is confusing me... The problem is that I don't think I have a "rotated AABB". I only have one AABB, the original one. Then I translate/rotate my object and that AABB no longer has any meaning, it needs to be recalculated. That's what I'm trying to achieve... Ok, you lost me there... I'm actually trying to work with bounding spheres now and see whether they are enough for most objects but I'm afraid they won't suffice for some other objects, that's why I'm trying to get AABB to work and then pick the ones that work better with each specific object. And since you mentioned bounding spheres, I'm having a tiny little problem with them and I'm not sure what I'm doing wrong. Again, the problem I'm having is with the calculation and it's basically finding the the radius and better fits the whole object. I've tried 2 different approaches (which I'll post below). The first one, depending on the object, does not completely enclose it in a sphere. For instance, a cube will have it's corners outside of the bounding sphere. The second one is creating bouding spheres much larger than what they should be but I can't understand where my calculations are failing. What I'm doing wrong? First approach with tight bounding spheres:[color="#010001"] [source lang="cpp"] glmDimensions(objPlayer.model, objDim); objDim[0] /= 2.0f, objDim[1] /= 2.0f, objDim[2] /= 2.0f; maxRadius = MAX(objDim[0], MAX(objDim[1], objDim[2])); [/source] Second approach with big bounding spheres: [color="#010001"][source lang="cpp"] glmDimensions(objTower[0].fireModel, objDim); objDim[0] /= 2.0f, objDim[1] /= 2.0f, objDim[2] /= 2.0f; vAux.SetValue(objDim[0], objDim[1], objDim[2]); // Vector (x,y,z) maxRadius = vAux.Magnitude(); [/source] The problem is that I wanted to completely enclose the object in the bounding sphere (keep the cube corners inside) but I don't understand why my second approach is giving me pretty large bounding spheres for some objects.
  5. Hi there, I'm having a hard time with this AABBs and despite lots of more topics on this board about the same subject, I still had to register and post my own question as I can't make it work... Hopefully someone here can guide me in fixing my problems and tell me what am I doing wrong. I can easily calculate the max/min vertex of some object and create the bounding box of that object. It's also easy to move that box when the object moves; I just need to sum max/min with the object position and that's it. But when it comes to rotations, I'm finding it really hard to calculate the AABB. And I probably need to take a different approach. From what I've read, I need to multiply the modelview matrix by the max/min vector or maybe the 8 vertices of the bounding box (it's probably the same, converting from one to the other is trivial). But that's exactly my problem... I just can't calculate the AABB after rotating the object and I have no idea what I'm doing wrong. The object I'm trying to create the AABB for is the player, which starts at the world's origin. With my current code, if I don't move the player and just rotate, the bounding box starts shrinking/enlarging but not correctly. If I move the player a bit and then rotate, the bounding box starts shrinking/enlarging as before but it will also rotate around some arbitrary point and I don't know why that happens. Bottom line is, my code is not working as I want it... [source lang="cpp"]typedef struct sAxisAlignedBoundingBox { Vector3D bounds[8]; Vector3D max, min; } AxisAlignedBoundingBox; void drawAxisAlignedBoundingBox(AxisAlignedBoundingBox box) { glPushAttrib(GL_LIGHTING_BIT | GL_POLYGON_BIT); glEnable(GL_COLOR_MATERIAL); glDisable(GL_LIGHTING); glColor3f(1.0f, 1.0f, 0.0f); glBegin(GL_LINE_LOOP); glVertex3f(box.bounds[0].x, box.bounds[0].y, box.bounds[0].z); glVertex3f(box.bounds[1].x, box.bounds[1].y, box.bounds[1].z); glVertex3f(box.bounds[2].x, box.bounds[2].y, box.bounds[2].z); glVertex3f(box.bounds[3].x, box.bounds[3].y, box.bounds[3].z); glEnd(); glBegin(GL_LINE_LOOP); glVertex3f(box.bounds[4].x, box.bounds[4].y, box.bounds[4].z); glVertex3f(box.bounds[5].x, box.bounds[5].y, box.bounds[5].z); glVertex3f(box.bounds[6].x, box.bounds[6].y, box.bounds[6].z); glVertex3f(box.bounds[7].x, box.bounds[7].y, box.bounds[7].z); glEnd(); glBegin(GL_LINE_LOOP); glVertex3f(box.bounds[0].x, box.bounds[0].y, box.bounds[0].z); glVertex3f(box.bounds[5].x, box.bounds[5].y, box.bounds[5].z); glVertex3f(box.bounds[6].x, box.bounds[6].y, box.bounds[6].z); glVertex3f(box.bounds[1].x, box.bounds[1].y, box.bounds[1].z); glEnd(); glBegin(GL_LINE_LOOP); glVertex3f(box.bounds[4].x, box.bounds[4].y, box.bounds[4].z); glVertex3f(box.bounds[7].x, box.bounds[7].y, box.bounds[7].z); glVertex3f(box.bounds[2].x, box.bounds[2].y, box.bounds[2].z); glVertex3f(box.bounds[3].x, box.bounds[3].y, box.bounds[3].z); glEnd(); glPopAttrib(); } void calculateAxisAlignedBoundingBox(GLMmodel *model, float matrix[16]) { AxisAlignedBoundingBox box; float dimensions[3]; // This will give me the absolute dimensions of the object glmDimensions(model, dimensions); // This calculates the max and min points in object space box.max.x = dimensions[0] / 2.0f, box.min.x = -1.0f * box.max.x; box.max.y = dimensions[1] / 2.0f, box.min.y = -1.0f * box.max.y; box.max.z = dimensions[2] / 2.0f, box.min.z = -1.0f * box.max.z; // These calculations are probably the culprit but I don't know what I'm doing wrong box.max.x = matrix[0] * box.max.x + matrix[4] * box.max.y + matrix[8] * box.max.z + matrix[12]; box.max.y = matrix[1] * box.max.x + matrix[5] * box.max.y + matrix[9] * box.max.z + matrix[13]; box.max.z = matrix[2] * box.max.x + matrix[6] * box.max.y + matrix[10] * box.max.z + matrix[14]; box.min.x = matrix[0] * box.min.x + matrix[4] * box.min.y + matrix[8] * box.min.z + matrix[12]; box.min.y = matrix[1] * box.min.x + matrix[5] * box.min.y + matrix[9] * box.min.z + matrix[13]; box.min.z = matrix[2] * box.min.x + matrix[6] * box.min.y + matrix[10] * box.min.z + matrix[14]; /* NOTE: If I remove the above calculations and do something like this: box.max = box.max + objPlayer.position; box.min = box.min + objPlayer.position; The bounding box will move correctly when I move the player, the same does not happen with the calculations above. It makes sense and it's very simple to move the box like this. The only problem is when I rotate the player, the box should be adapted and increased/decreased in size to properly fit the object as a AABB. */ box.bounds[0] = Vector3D(box.max.x, box.max.y, box.min.z); box.bounds[1] = Vector3D(box.min.x, box.max.y, box.min.z); box.bounds[2] = Vector3D(box.min.x, box.min.y, box.min.z); box.bounds[3] = Vector3D(box.max.x, box.min.y, box.min.z); box.bounds[4] = Vector3D(box.max.x, box.min.y, box.max.z); box.bounds[5] = Vector3D(box.max.x, box.max.y, box.max.z); box.bounds[6] = Vector3D(box.min.x, box.max.y, box.max.z); box.bounds[7] = Vector3D(box.min.x, box.min.y, box.max.z); // This draw call is for testing porpuses only drawAxisAlignedBoundingBox(box); } void drawObjectPlayer(void) { static float mvMatrix[16]; if(SceneCamera.GetActiveCameraMode() == CAMERA_MODE_THIRD_PERSON) { objPlayer.position = SceneCamera.GetPlayerPosition(); objPlayer.rotation = SceneCamera.GetRotationAngles(); objPlayer.position.y += -PLAYER_EYE_HEIGHT + 0.875f; /* Only one of the two code blocks below should be active at the same time Neither of them is working as expected. The bounding box doesn't is all messed up with either code. */ // Attempt #1 glPushMatrix(); glTranslatef(objPlayer.position.x, objPlayer.position.y, objPlayer.position.z); glRotatef(objPlayer.rotation.y + 180.0f, 0.0f, 1.0f, 0.0f); glCallList(gameDisplayLists.player); glGetFloatv(GL_MODELVIEW_MATRIX, mvMatrix); glPopMatrix(); // Attempt #2 glPushMatrix(); glLoadIdentity(); glTranslatef(objPlayer.position.x, objPlayer.position.y, objPlayer.position.z); glRotatef(objPlayer.rotation.y + 180.0f, 0.0f, 1.0f, 0.0f); glGetFloatv(GL_MODELVIEW_MATRIX, mvMatrix); glPopMatrix(); calculateAxisAlignedBoundingBox(objPlayer.model, mvMatrix); } }[/source]
  • 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!