Forum Discussion
AndyOHart
11 years agoHonored Guest
Hydra collision detection?
Hey guys. Not sure if this has been done or looked into, but I really want to stop my hydras from being able to pass through objects. I'm not sure if it is as simple as adding a mesh to the hand model and setting up colliders or if it is more difficult then that. I know it could take away from the immersion by including it as there would be no physical object in front of you when you push your hand out, but I think it would be a bit better than having your hand, or lightsource going through the walls.
Any ideas on how this could be done?
Any ideas on how this could be done?
6 Replies
Replies have been turned off for this discussion
- serrarensHonored GuestHi Andy,
I have been (and will be) working on this. I agree that it breaking immersion when you physical hand is not stopped by an object while you virtual hand is, but moving your hands through walls is not immersive either.
The basics are not very hard, but when you want to make it more realistic things get quite complicated.
I assume you have the normal setup with a target gameobject which is followed by your virtual hands.
First, make sure that the objects you don't want to go through are marked static.
Then attach a trigger collider and a kinematic rigidbody to the target . Normal colliders don't work, as we are not using physics here (so no gravity or physics based collisions)
then implement an OnTriggerEnter function on the target gameobject. In this function you freeze the position of the virtual hand when the target enters the static object.
Then an OnTriggerExit will unfreeze the hand again and then make sure the hand matches the position of the target again.
Hope this helps! - AndyOHartHonored GuestHi thanks for the great in-depth response!
Yeah I had a feeling it could be quite complicated depending on how you want it to be handled in the game.
Yeah my setup is essentially the Hydra presets that they give you, and I added in a movement system to the joystick so I can walk around.
So are you saying essentially I need to mark all objects and terrain in my world as static? Does this have any other effects on the game or anything??
For the OnTriggerEnter, that will work in general for all of the collidables marked static right?
Ah so since this isn't physics based, the hand will essentially just stop moving altogether when it touches an object, it wont 'slide' along the wall or anything as you move your hand near it? - serrarensHonored GuestMarking objects static will make your game faster :). It tells the game engine that the object will never move which changes the way the object is rendered, collisions are detected and shadows are computed. That said, you should only mark those objects which do not move as static.
So what about the other objects then? Give them a rigidbody (non-kinematic) and a normal collider. If you attach a kinematic collider to your virtual hands you can interact with them. (you can read my blog posting on this for an example: https://serrarens.nl/en/tutorial-grabbing-virtual-hands-in-unity/)
I forgot to mention that the OnTriggerEnter function should check that the collider we are entering is attached to a static gameobject. Otherwise your hand will stop for all objects, including those which can move.
Your hand won't slide indeed. This is a point things get less obvious. If you want this, you need to start raycasting to determine the position of the virtual hand in relation to the target. I haven't found the best way yet. I have tried raycasting from the virtual hand to the target and from the shoulder to the target, but both did not give a satisfying result in all cases. I still have to try raycasting toward the target along the normal of the object we touch.
So if you find a good way I am certainly interested! - AndyOHartHonored GuestAh that's good to hear. I knew there was a benefit as I did it before I just couldn't remember if it had a trade-off or anything.
Ah so I want to be able to pick up objects so anything that should react to physics should be the non kinematic with a normal collider?
Could you provide some sample code for the OnTriggerEnter at all? I'm not sure what I should have there really? Is it something like
OnTriggerEnter(){
if( EnteredCollider == ??? )
Stop hands here some how
}
Just not sure what I should do in it really.
Ah I see, I guess I will look into it when I have a basic version setup.
Do you find it more immersive or less immersive when your hands cannot pass through the objects but suddenly stop? Or is it more immersive having your hands be able to pass through walls, but not have that physical barrier stopping hands.
Thanks again - serrarensHonored GuestIndeed you will be able to pick things up when they are non kinematic and have a normal collider.
For the code: I already planned to make a 'VR hands, part 2' blog about collisions with static objects. I think I should write it the forthcoming week ;-) For a full explanation I need more time and space...
Thing is that I use two targets for each hand: 1 which is directly connected to the hydra controller (handTarget) and 1 which is the targets my hands are following (cHandTarget). In the normal, non-colliding case the second target has the same position/rotation as the first but when they collide with a static object, the connection is broken.
So in Update() I have:
if (collided) {
Ray ray = new Ray(shoulder.position, handTarget.position - shoulder.position);
RaycastHit hit;
Vector3 dir = handTarget.position - shoulder.position;
int rcLayers = Physics.DefaultRaycastLayers & ~(1<<11) | 1;
if (Physics.Raycast(ray, out hit, dir.magnitude + 0.1f, rcLayers)) {
Vector3 contactPoint = hit.point - dir.normalized * 0.1f;
cHandTarget.position = contactPoint;
}
} else {
cHandTarget.position = handTarget.position;
}
cHandTarget.rotation = handTarget.rotation;
The layers line is because the raycast should not be hitting my virtual hands (layer 11) but should hit defaults (layer 0).
The 0.1f is because I use a sphere with radius 0.1f for a collider on the handTargets.
The the OnTriggerEnter/Exit:
void OnTriggerEnter(Collider other) {
if (other.gameObject.isStatic == true) {
collided = true;
collidedObject = other;
}
}
void OnTriggerExit(Collider other) {
if (collided && other == collidedObject) {
collided = false;
collidedObject = null;
}
}
I hope I didn't introduce a lot of errors in the code above, because I extracted it from more complex code without testing. Like I said, I will be working on a full blog article on this this week which should include an update to the Unity package 'VR Hands' which you can download on my site. (But first I want to release v2 of my VR Body Movements package in the Unity Asset Store which should be able to support Sixense STEM and Oculus Rift DK2 ;-).
Finally, about the immersion. I did an experiment tonight with my two hands: left one stops at the wall, right one did not stop and moves into the wall. I had a strong preference for the left arm. Arms going through wall do not look right, the wall does not seem real as if it were a projection on a smoke screen. The left arm does disconnect from the physical arm, but something remarkable seems to happen: my physical hand stops moving quite quickly after my virtual hand stops. Of course I can continue moving my real hand, but subconsciously my brain tells me that the arm cannot go further and that it does not make sense to move any further. So the disconnection does not extend very far.
Besides that, the hand tracking is not 100%. I don't calibrate the Hydra so the accuracy is limited. If the physical and virtual hand move away from each other a little more because of a wall, it does not break immersion directly. This may change however when we get 100% accurate hand tracking though!
And even more finally: how do you want to interact with the environment in a realistic way if everything is either not connected to the world or is a ghost object? - serrarensHonored GuestI have written the article and created the Unity package containing the code and a demo scene.
You can find them here: https://serrarens.nl/en/virtual-hands-and-static-object-collisions/.
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 8 months ago
- 2 years ago