# Slight Issue

This topic is 2642 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all,I am currently working on AABB's to start myself off without trying something too heavy.

I've read around before coming here to ask, it is most likely a simple answer but struggling to figure the solution.

I've defined simple objects using V-Arrays, successfully managed to find the Min/Max values and display the AABB to make sure it's around my objects which is fine.
The trouble I'm having is the collision response section of my code;

 bool aabbCollision(){	if(AABB.max.x < AABB.min.x) return false;	if(AABB.max.y < AABB.min.y) return false;	if(AABB.max.z < AABB.min.z) return false;	if(AABB.min.x > AABB.max.x) return false;   	if(AABB.min.y > AABB.max.y) return false;	if(AABB.min.z > AABB.max.z) return false;        return true;}

Very trivial for you guys obviously, I'm just checking the AABB against itself since I want to just get two similar objects with collision for now.

All is fine when compiling but when I put a condition for response it doesn't function at all.
Example:

    if(GetKeyState(VK_UP))      {         if(!OnCollision)		      {               Forward(1);                  // Move forward            }      }

if(!OnCollision) - If I insert this I can't move forwards at all, kind of frustrating. It is like the collision is consistently taking place without having a collision in the first place?.

Any assistance would be appreciated; Any other code or additional information required I am happy to supply if it helps me to resolve this.

Kind Regards.

##### Share on other sites
Your function aabbCollision() will always return true. You don't specify, but if OnCollision is somehow related to that function, that could cause the condition you're getting.

##### Share on other sites
Quote:
 Original post by BuckeyeYour function aabbCollision() will always return true. You don't specify, but if OnCollision is somehow related to that function, that could cause the condition you're getting.

Hi, thankyou for the reply, sorry for the delay in responding!

I've been trying to figure out why the function for collision is always returning true? I know this is probably dumb to ask but I have fiddled around with my code for a while and haven't had the slightest progress in figuring the solution.

I've made a function to draw the BoundingBoxes, but they obviously appear correctly when translated with the object itself + its the correct fit/shape.

I have come to a dead end with this, searching on webpages etc but not really getting any results, it tells me how to check for the collision, but whether I'm doing it correct myself is another story. I'll supply code so you maybe could best advise me where I'm going wrong or what I need to modify etc...

Object of same type: Drawn Using V-array, my aabb is calculated by looping through the vertice array to find min/max values.
I then tried creating two similar objects, having seperate AABB calculations which were Box & Box1, tried to compare the two but havent had any success with this either. I would prefer to keep to one AABB for the same objects.

AABBCollision - I have included several attempts, even though the same, but different variations trying to correct my issue I described in the first post.

I havent' given up on trying to work this out, but I really can't see where I'm going wrong.

[Edited by - Silarr on October 23, 2010 2:40:22 PM]

##### Share on other sites
What are the min and max values? What are they assigned and what do they represent?

by definition, max is always greater than the minimum, unless they are both equal, therefore the if statements which keep asking if max is less than min, will always return false, therefore skip all of the return false; statements. In this event, there will be only one statement left, return true;, and therefore your function will always return true.

##### Share on other sites
Quote:
 Original post by boogyman19946What are the min and max values? What are they assigned and what do they represent?

I will supply what I use to construct object/aabb. Representation on draw is a simple cuboid, simple enough for testing purposes, obviously I will try more complex shapes but right now I'm struggling with the most simple...

[Edited by - Silarr on October 23, 2010 2:32:50 PM]

##### Share on other sites
You want to be testing collision between two AABB's, but your code is obviously only testing one with itself!
It will always return true because a proper AABB will always have positive volume, so a collision test against itself will always be true.

Edit: You might have wanted to do something like this. Note that I did not check to see if your code was correct, but merely changed the overall design:

bool aabbCollision(Box AABB2){	if(AABB.max.x < AABB2.min.x) return false;	if(AABB.max.y < AABB2.min.y) return false;	if(AABB.max.z < AABB2.min.z) return false;	if(AABB.min.x > AABB2.max.x) return false;   	if(AABB.min.y > AABB2.max.y) return false;	if(AABB.min.z > AABB2.max.z) return false;        return true;}

##### Share on other sites
Quote:
 Original post by xytorYou want to be testing collision between two AABB's, but your code is obviously only testing one with itself!Edit: You might have wanted to do something like this. Note that I did not check to see if your code was correct, but merely changed the overall design:*** Source Snippet Removed ***

Yeh When I realised this, I changed the collision accordingly, but apparently it's still turning true. I'm trying to work it out logically where the problem is;

1: Define V-Array for Object1
2: Define V-Array for Object 2
3: AABB Struct
4. Box,Box1 --> AABB Struct
5. Pointer to V-Arrays
6. Calculate first AABB for Object 1 looping V.Array
7. Same as 6 but for Object 2 Array

8. void Object 1 { Enable Arrays, PushMatrix-->Random location<--- Draw Object, Disable Arrays}

9. Same as 8.
10. Collision Detection - Box vs Box1 Min/Max.

11. Assign key to move forward only if Collision is false. [Obviously wont work because collision is still turning true.]

I'm trying to solve the "why" but can't figure out logically where I've gone wrong :/. [I posted code in previous posts if a reference is needed].

If I have two seperate Arrays, so two similar objects but different values, then calculate a AABB for each object, then compare the two... surely it should work, but for some reason it isn't, and that's what I can't seem figure out.

Solution is probably staring me in the face :d

##### Share on other sites
Quote:
Original post by Silarr
Quote:
 Original post by xytorYou want to be testing collision between two AABB's, but your code is obviously only testing one with itself!Edit: You might have wanted to do something like this. Note that I did not check to see if your code was correct, but merely changed the overall design:*** Source Snippet Removed ***

Yeh When I realised this, I changed the collision accordingly, but apparently it's still turning true. I'm trying to work it out logically where the problem is;

1: Define V-Array for Object1
2: Define V-Array for Object 2
3: AABB Struct
4. Box,Box1 --> AABB Struct
5. Pointer to V-Arrays
6. Calculate first AABB for Object 1 looping V.Array
7. Same as 6 but for Object 2 Array

8. void Object 1 { Enable Arrays, PushMatrix-->Random location<--- Draw Object, Disable Arrays}

9. Same as 8.
10. Collision Detection - Box vs Box1 Min/Max.

11. Assign key to move forward only if Collision is false. [Obviously wont work because collision is still turning true.]

I'm trying to solve the "why" but can't figure out logically where I've gone wrong :/. [I posted code in previous posts if a reference is needed].

If I have two seperate Arrays, so two similar objects but different values, then calculate a AABB for each object, then compare the two... surely it should work, but for some reason it isn't, and that's what I can't seem figure out.

Solution is probably staring me in the face :d

It's better to post code rather than your own logic because C/C++ is standardized, while your own logic syntax is not; it's less readable to me.

Anyway, you should do some live debugger sanity checks. For example, put a breakpoint inside the collision function and make sure that the two AABBs are different.

##### Share on other sites

[Edited by - Silarr on October 23, 2010 2:31:25 PM]

##### Share on other sites
Quote:
 if (key == 'a'){if (aabbCollision==false)Forward(5);}

The line aabbCollision==false appears to be comparing the address of the function to the value for false. Since the address of the function is greater than 0 and false is probably defined as 0, "aabbCollision==false" is always false and Forward never gets called.

Try calling the function:

if( !aabbCollision( Box, Box1 ) ) Forward(5);

I don't know what IDE you're using, but you need to learn how to debug by setting breakpoints in your code (as suggested), outputting messages to a debug window, etc.

##### Share on other sites
Quote:
 Original post by BuckeyeThe line aabbCollision==false appears to be comparing the address of the function to the value for false. Since the address of the function is greater than 0 and false is probably defined as 0, "aabbCollision==false" is always false and Forward never gets called.Try calling the function:if( !aabbCollision( Box, Box1 ) ) Forward(5);I don't know what IDE you're using, but you need to learn how to debug by setting breakpoints in your code (as suggested), outputting messages to a debug window, etc.

Thankyou for the advice[highly appreciated], but whats causing the statements to always return true is a mystery to me at this moment[Debug:It breaks if the first condition is true with every other statement in the collision detection so something is wrong. I corrected my statement to the suggested line you gave above but no success.

The only thing I am unsure I maybe missed is updating the current positions of the AABB's relative to the current position of the objects in 3d space?[I have calculated the AABB's min/max, but im geussing I need to do the same for locations of the objects or not?] For example, My objects are drawn in a certain location, will that mean AABB's are calculated but drawn to the exact same location or will no matter what they will be drawn to the same location as the object it relates to?

I do believe what I have shown you guys already is on the right track, but I'm no expert which is why I brought my issue to you guys.

I don't want to irritate people on this forum, but I'm struggling to see what I'm missing or incorrectly doing. [3d] One static object and one dynamic object moving with the camera's movement, when hit they have a collision, for this example I'm testing, the user can no longer move forward if that is the case.

I have examined the Axis-Aligned BoundingBox articles related to C++ etc, found a few books online showing similar construction[ I seem to have done the AABB correctly for both objects], it's not that I have done lack of research on this topic, as I'm beginning to find it quite frustrating as to why it's giving undesired result.

##### Share on other sites
I assume you remove your code and don't post your fixes because you are afraid somebody will steal it. Don't worry; most of us who will help you can write what you are trying to do in a half hour.

My point is, don't be shy about posting your code for every post. If it's in source tags, it will only help us help you.

I can only guess you're not translating the box properly...

##### Share on other sites
Quote:
 Original post by xytorI assume you remove your code and don't post your fixes because you are afraid somebody will steal it. Don't worry; most of us who will help you can write what you are trying to do in a half hour. My point is, don't be shy about posting your code for every post. If it's in source tags, it will only help us help you.I can only guess you're not translating the box properly...

Would probably take one of you guys to do what Im trying to accomplish in 10minutes :d.

Yeh I apologise, just assumed since I turned my last post into a more unrelated question to my code it wasn't of much use since you had previously checked it.

I will post again, but this time I had a slight development with the OnCollision - It appeared when debugging, the values it was comparing, since the max values had been a negative value it would produce some unwanted results which made the OnCollision always to be true.

I've since fixed that problem by switching some statements(needs to be checked though tbh as its probably a dirty runaround), but now I'm stuck with actually translating the AABB's like you have geussed. I'm unsure of what I have is correct because the OnCollision is checking the current static V-array with one another but doesn't take into account the translated world position of the objects... which I'm having trouble with. This part I can't seem to find in any books/webpages regarding this. Any guidance is welcomed a lot.

[Edited by - Silarr on October 26, 2010 7:57:30 AM]

##### Share on other sites
Your AABB needs to change each frame.

Since you store it as a min and a max, you need to translate those every frame and check collision on the AABBs only.

When you are creating your AABBs, you have a for loop but you don't use i anywhere.

By the way, please don't change your code for the post only. That makes reading it very difficult.

For example, you have two functions called AABB. Also, I cant tell if the high level design sucks because you changed the code for the post or not.

##### Share on other sites
I have made some progress but something is slightly incorrect and can't figure out what's causing it.

For some reason my objects are instantly colliding with themselves, well the AABB is colliding with the object it's surrounding. I tested that if I '+' the AABB surrounding the object in any direction e.g. Z-Axis, my collision is no longer true and I can move once the AABB leaves the object, but when I move the AABB back to touching the object it collides. This all happens without ever touching object #2 far away with its own AABB.

I don't believe I require translation because the AABB's co-ordinates are the objects - If I translate the object, the AABB moves with it no matter what.

I used this to move the AABB outside of the object it's surrounding vice-versa:
 	if (key == 'z'){glPushMatrix();				glTranslatef(Box.max.z -=0.1f);				glTranslatef(Box.min.z -=0.1f);              			glPopMatrix();}	if (key == 'x'){glPushMatrix();				glTranslatef(Box.max.z +=0.1f);				glTranslatef(Box.min.z +=0.1f);              			glPopMatrix();}

I'm puzzled!, The code used is in the previous post and I can't really see whats causing the AABB to collide with the object its encompassing. I'm not trying to do something advanced here, just basic collision...

##### Share on other sites
How are you even getting that to compile? Doesn't glTranslatef take 3 floats?

At this point, I would suggest a better high-level design. For example, try to separate graphics from game code.

Either that, or post your whole entire code so that we can compile and run it ourselves and see what's happening.