The Mixed Reality Forums here are no longer being used or maintained.
There are a few other places we would like to direct you to for support, both from Microsoft and from the community.
The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.
For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.
If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.
And always feel free to hit us up on Twitter @MxdRealityDev.
Collision detection with Spatial Map
I can't get OnCollisionEnter to fire when my sphere collides with the spatial map while being manipulated.
So I have a sphere that floats (I do not want to apply physics to it). It has a Sphere Collider component on it. I have the following code execute on the object.
//add a BoxCollider if the interactible does not contain a collider. itemCollider = GetComponentInChildren<Collider>() ?? gameObject.AddComponent<BoxCollider>(); itemCollider.enabled = true; /* * add a rigidbody for collision detection. */ var rigidBody = GetComponentInChildren<Rigidbody>() ?? gameObject.AddComponent<Rigidbody>(); //we don't want to be at the mercy of physics. rigidBody.isKinematic = true; rigidBody.detectCollisions = true; rigidBody.collisionDetectionMode = CollisionDetectionMode.Continuous;
The spatial map is composed of game objects on which there is a Mesh Collider. I have added the following code to the SpatialMappingSource.AddSurfaceObject function.
MeshCollider collider = surface.GetComponent<MeshCollider>(); collider.enabled = true;
In a script component of my sphere I have the following function and it never gets called when the sphere hits the spatial map.
void OnCollisionEnter(Collision collision) { System.Diagnostics.Debug.WriteLine("COLLISION!"); }
Is there anyway to detect when the sphere hits the spatial map without applying physics to the sphere?
Best Answers
-
timg mod
You won't be able to use the Physics events (OnCollision*, OnTrigger*) without using Physics.
You could, in an Update method somewhere look at whether your object's Collider's bounds intersect with some other object's Collider's bounds.
public GameObject CheckForCollisionsWithThisObjectsCollider; void Update() { if (CheckForCollisionsWithThisObjectsCollider) { if (GetComponent<Collider>().bounds.Intersects(CheckForCollisionsWithThisObjectsCollider.GetComponent<Collider>().bounds)) { Debug.Log("Colliders overlap"); } } }
5 -
mtoronto ✭
So I sort of figured out a way to achieve this without applying physics to the sphere (all the time).
I set the Rigidbody to initially useGravity = false and isKinematic = true. When in this state collision detection does not function. Whenever a movement through manipulation gesture is started or placement with gaze is started then I set isKinematic = false which enables collision detection. Then when the manipulation gesture is finished or placement with gaze is finished then I set isKinematic = true.
In this manner collision detection is enabled only when the object is being interacted with and it is unaffected by physics when not being interacted with.
5
Answers
Could you make the Sphere Collider a trigger instead (isTrigger = true) and then use OnTriggerEnter instead. I don't think the rigidbody would be necessary with this approach.
void OnTriggerEnter(Collider collider) { System.Diagnostics.Debug.WriteLine("COLLISION!"); }
Thanks for the suggestion but no luck.
You won't be able to use the Physics events (OnCollision*, OnTrigger*) without using Physics.
You could, in an Update method somewhere look at whether your object's Collider's bounds intersect with some other object's Collider's bounds.
Thanks for the answer. Maybe if I set gravity to 0 or something I can apply physics to the sphere without it falling to the ground.
So I sort of figured out a way to achieve this without applying physics to the sphere (all the time).
I set the Rigidbody to initially useGravity = false and isKinematic = true. When in this state collision detection does not function. Whenever a movement through manipulation gesture is started or placement with gaze is started then I set isKinematic = false which enables collision detection. Then when the manipulation gesture is finished or placement with gaze is finished then I set isKinematic = true.
In this manner collision detection is enabled only when the object is being interacted with and it is unaffected by physics when not being interacted with.
I have come to a similar point in trying to get my "hologram" to collide and stop against spatial map wall or floor. I have looked closely at the Holograms app and it appears that they are using some sort of physics because the hologram will rotate if not aligned and pushed against a wall. But it doesn't appear that they are applying a force as the hologram moves with the gesture. I arrived at the same solution you suggested, but no such luck. Did you ever find a solution that worked?
Oh, I am not doing gaze placement. I am moving the object with gestures like the Holograms app.
@bfichter Did you find a solution of this? I am also looking for some help to achieve similar results.
@bfichter @hitesh_sharma Have one of you found a solution? Having the same problem here..
@bfichter @AngeloG @hitesh_sharma do you found a solution for the problem. I have an > @bfichter said:
Hi @bfichter,
I'm working on the same problem, did you achieve to fix the problem?