CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

Thread: [RESOLVED] how create a 3D collision function?

1. Senior Member
Join Date
Apr 2009
Posts
1,298

[RESOLVED] how create a 3D collision function?

i did a 3D collision function:
Code:
```bool IsCollision3D(Position3D ObjectPosition1, Size3D ObjectSize1, Position3D ObjectPosition2, Size3D ObjectSize2) {
return ((ObjectPosition1.X
<= (ObjectPosition2.X + ObjectSize2.width))
&& (((ObjectPosition1.X + ObjectSize1.width)
>= ObjectPosition2.X)
&& ((ObjectPosition1.Y
<= (ObjectPosition2.Y + ObjectSize2.height))
&& (((ObjectPosition1.Y + ObjectSize1.height)
>= ObjectPosition2.Y)
&& ((ObjectPosition1.Z
<= (ObjectPosition2.Z + ObjectSize2.ZDepth))
&& ((ObjectPosition1.Z + ObjectSize1.ZDepth)
>= ObjectPosition2.Z))))));
}```
is these function correct?
(ok... i'm using VB6 and not C... but these function was translated...)
sometimes can work and others don't.... maybe it's the parameters that i add it.. but i don't know...
so i need ask: is these function correct for get the 3D collision?

2. Member +
Join Date
Feb 2017
Posts
564

Re: how create a 3D collision function?

Originally Posted by Cambalinho
is these function correct?
I think the parentheses are wrong for symmetry reasons. The test for collision should be the same in all 3 dimensions. The return statement has 12 lines so there are 4 lines per dimension. If you look at line 3 it starts with && ((( and so does line 7. It means also 11 should start like that but it doesn't so the third dimension is treated differently. It shouldn't so something is wrong.

Say you have a line segment (P1, P1+S1) where P1 is the left point and P1+S1 is the right point (the left point with a size S1 added). Then you have another line segment (P2, P2+S2). These line segments overlap (collide) according to this test.

(P1 <= (P2 + S2)) && ((P1 + S1) >= P2)

This is the test that is used in your code and it seems okay to me.

Now for a collision in three dimensions to occur there must be a segment overlap in all three dimensions. With width (w), height (h) and depth (d) we end up with,
Code:
```return ((P1.w <= (P2.w + S2.w))  && ((P1.w + S1.w) >= P2.w)) &&
((P1.h <= (P2.h + S2.h))  && ((P1.h + S1.h) >= P2.h)) &&
((P1.d <= (P2.d + S2.d))  && ((P1.d + S1.d) >= P2.d));```
Nicely symmetric and hopefully correct.
Last edited by wolle; May 26th, 2020 at 04:29 PM.

3. Senior Member
Join Date
Apr 2009
Posts
1,298

Re: how create a 3D collision function?

yes i added the parentheses... but seen your code: where is X and Y? maybe it's another way for get the 3D collision...
i'm trying using the same function for see if the line origin is inside the camera(for draw only what we see).
so the size(w,h,d) is zero.
but i get wrong results
that's why i'm asking if my function is correct

4. Member +
Join Date
Feb 2017
Posts
564

Re: how create a 3D collision function?

Originally Posted by Cambalinho
but seen your code: where is X and Y?
Okay I used the same member names for P and S that is w, h and d but okay that's a little confusing so I change to x, y and z for P to better relate to your code,
Code:
```return ((P1.x <= (P2.x + S2.w))  && ((P1.x + S1.w) >= P2.x)) &&
((P1.y <= (P2.y + S2.h))  && ((P1.y + S1.h) >= P2.y)) &&
((P1.z <= (P2.z + S2.d))  && ((P1.z + S1.d) >= P2.z));```
Furthermore, P and S corresponds to ObjectPosition and ObjectSize respectively in your code.
Last edited by wolle; May 26th, 2020 at 05:22 PM.

5. Senior Member
Join Date
Apr 2009
Posts
1,298

Re: how create a 3D collision function?

yes... now it's the same.
thanks for all... now i must create a new topic for the next created function.
thank you

6. Senior Member
Join Date
Apr 2009
Posts
1,298

Re: how create a 3D collision function?

i'm sorry, but i need show you something:
(i'm sorry but, for now i'm using VB6... but you understand the code easy)
when i do:
Code:
`If (IsCollision3D(Camera1.Position, Camera1.Size, Position, Size) = False) Then Exit Sub`
i know it's correct, because the plane is drawed... until here works fine.
but when i draw the 1st line, i calculate the 1st point and then the second point:
Code:
```'Floor:    'Vector1
FillPosition3D NewPosition3D, Position.X, Position.Y, Position.Z
NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition) 'we can ignore the rotation, because the result it's the same

Dim sizeD As Size3D
FillSize3D sizeD, 0, 0, 0 '1 point don't have size
If (IsCollision3D(NewPosition3D, sizeD, Camera1.Position, Camera1.Size) = False) Then
MsgBox "hey"```
if the 1st collision is detected, why these one isn't?
(yes the messagebox it's called)
these line it's inside the camera.. i'm confused.. sorry
edited: was only the code or too my language?
Last edited by VictorN; May 27th, 2020 at 03:53 PM.

7. Re: how create a 3D collision function?

Last edited by VictorN; May 27th, 2020 at 03:55 PM.

8. Senior Member
Join Date
Apr 2009
Posts
1,298

Re: how create a 3D collision function?

thanks VictorN.. thank you
but was only the code or too the language?
Last edited by VictorN; May 27th, 2020 at 03:55 PM.

9. Re: how create a 3D collision function?

Last edited by VictorN; May 27th, 2020 at 03:56 PM.

10. Senior Member
Join Date
Apr 2009
Posts
1,298

Re: how create a 3D collision function?

wolle?
please VictorN can you disable the 'RESOLVED'?
Last edited by VictorN; May 27th, 2020 at 03:56 PM.

11. Re: how create a 3D collision function?

Done

12. Senior Member
Join Date
Apr 2009
Posts
1,298

Re: how create a 3D collision function?

thanks for all... maybe now the Wolle notice more easy
thank you

13. Member +
Join Date
Feb 2017
Posts
564

Re: how create a 3D collision function?

Originally Posted by Cambalinho
i'm sorry, but i need show you something:
I cannot say what is wrong but the first step must be to check out carefully whether the isCollision function works properly. It is based on two principles. The first is a certain detection test to determine whether two line segments are overlapping. The second is the idea that a collision in many dimensions happens when there's a line segment overlap in all dimensions, in your case 3.

Convince yourself that these two principles are proper and convince yourself by running tests that check all possible input cases that it works indeed (including corner cases like for example when the size of an object is 0 in some dimension).

The next step is to trace isCollision. Inside isCollision print the data it's being called with. If it doesn't return the result you expected there are two possible reasons why. The first is that isCollision is wrong and there's a bug. The second is that isCollision is right only it has been called with unexpected input.

If isCollision itself is well tested it is almost certain it's the second situation you are dealing with. This is part of a program development strategy called Stepwise Refinement. You make sure that what you have works before you continue. Otherwise when you encounter unexpected behavior you're standing on a morass with no solid ground in sight.
Last edited by wolle; May 30th, 2020 at 05:12 AM.

14. Senior Member
Join Date
Apr 2009
Posts
1,298

Re: how create a 3D collision function?

testing and review the code(with more help before of these problems):
1 - the floor is drawed(the left line): from bottom left to top left.. and both have the same X,Y and the Z is changed.;
2 - knowing these the bottom left is outside the camera view so it's correct.
thank you so much for all... thank you
i'm learning much more about 3D math... ok.. it's the basic for start, but it's great... thank you so much

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•