# Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

### 3 Dimensional Chat / [SOLVED] 3D object Z collision

Message
Posted: 1st Jun 2020 15:31 Edited at: 1st Jun 2020 18:07
Hi all, picked up AppGameKit studio a couple of months ago, so not totally used to ii all yet.
I'm making a fun 3D bus driver game with my daughter, but having a few problems with collision detection on the Z axis with simple AI traveling in the opposite direction.
The AI bus is supposed to honk its horn and wave when it gets near. I'm using this to move the AI repeatedly along the Z axis of my plane (when it gets to the bottom of the plane it resets and repeats its journey)

so far so good. I'm traveling on the opposite side of the road in the opposite direction when they meet on the Z I'm using

It doesn't trigger the horn. Interestingly as a check, I used >= and it triggers, but loops and never stops (which makes sense).
Question is, what am I doing wrong with my Z collision? (please note bus is just passing by, not crashing into me).
Thanks in advance for any kind help.

Sorry I seem to have messed up the code snippet. [Fixed by Moderator]

The author of this post has marked a post as an answer.

Posted: 2nd Jun 2020 12:33 Edited at: 2nd Jun 2020 12:34
This post has been marked by the post author as the answer.
My guess would be that it is floating point imprecision that is causing your problem. The position of busmesh0 and busmesh2 are never at exactly the same place an any particular frame.

You have two options:
1) Create a function that will accept that two values are close enough but not necessarily equal
2) Stick with the > which you know works but set a flag to say the horn has already sounded so don't sound it again.

They would look something like this:
Option One:

Option Two:
Posted: 2nd Jun 2020 15:37
Thanks Scraggle, both solutions work perfectly

Out of curiosity, how would I use the 'closeButNoCigar' function to trigger an event 20 yards away? (say for example traffic light change as you approach)
I'm not sure how to change the range to a wider trigger.
Posted: 2nd Jun 2020 16:01
Adjust the third parameter to a larger value.
In the example I gave, I set it to 0.5. Simply make that bigger to fit the size of your world.
Posted: 2nd Jun 2020 18:51
I was struggling with that 0.5 value and range, if I increased those values the sound would play multiple times.
As your suggestion, I played around with it. Best results were target + 80 and the play sound wouldn't echo.

Thanks again.
Posted: 2nd Jun 2020 19:07 Edited at: 2nd Jun 2020 19:13
Realistically, you should only use that function with low range values to determine floating point inaccuracies.
If you want to test if a point is within a certain range of another point then you are better off using a Pythagorean distance check

2D:

3D:
Posted: 3rd Jun 2020 00:05
Once again thanks for your code wisdom, I'll give that Pelopenisian algorithm a shot after lunch.