# Getting point where Physics.OverlapSphere() is overlapping a collider

Discussion in 'Physics' started by nilcheck, Mar 20, 2016.

1. ### nilcheck

Joined:
Mar 20, 2016
Posts:
4
Hi,

I am implementing a train game. Here is an example in BabylonJS: http://synth2014.github.io/Age-of-Steam/babylon/rk4.html

I am trying to implement this in Unity 5.3 now.
The locomotives are cuboids and the tracks can only be straight and circular sections connected together.

For proper loco movement on circular sections, I have taken 2 points in the loco - the front-most point and the back-most point. At every iteration I move the front point according to the loco speed and then have to calculate the back point. Since its 3D, and the loco length is fixed, the back of the loco must lie on a sphere centered at the front most point and of radius = length of the loco.

Now I have to intersect the sphere with another section(behind the loco of course) which can be a line or a circle in 3D. Intersecting the line is easy mathematically(without using colliders).

But a intersecting a circular section, is not always stable if I do it via maths: http://gamedev.stackexchange.com/qu...e-intersection-and-circle-sphere-intersection

So I am planning to use Physics.OverlapSphere(), with the sphere centered at the front point and radius = length of loco. I will attach colliders to all track sections behind the loco only.

http://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html

But Physics.OverlapSphere() wont give me the point of intersection between the sphere and the collider. How can I get it?

Is this the correct way: http://docs.unity3d.com/ScriptReference/Collider.ClosestPointOnBounds.html ?
The above needs the previous track sections to be rigid bodies, and they check for the closest point in their bounding volumes to the given point(loco front point)

Last edited: Mar 20, 2016

### Digital Ape

Joined:
Apr 11, 2010
Posts:
29,723
Sounds like you would be better off with spherecast or raycast to deliver rich information about the surrounding.

3. ### HiddenMonk

Joined:
Dec 19, 2014
Posts:
987
This guy
https://roystanross.wordpress.com/category/unity-character-controller-series/
has a custom character controller that uses overlapspheres and a series of methods for getting the intersection point. You can scroll down to near the bottom of that page to see the start of his journey.

Here is his github
https://github.com/IronWarrior/Supe...e/master/Assets/SuperCharacterController/Core
Have a look at the SuperCollider class and the BSPTree class which is whats used for mesh colliders.

While I dont use his character controller, I have used those 2 classes and I havent had any trouble yet.

ouzer likes this.