[SOLVED] Adding rigidbody collisions to VR example

I want to add varying terrain and platform heights to the VR example. It looks like it would require almost a complete rewrite to move the player using forces instead of direct translations in order to collide with other rigidbodies. Is there a simple way to do this that’s documented somewhere?

1 Like

Will there be anything in the world that would affect the player? Eg can the player get pushed by a moving rock etc?

I do have moving platforms in mind to carry the player around but there won’t be anything that pushes the player from the side or above in this project.

You could do this without physics. Eg. Parent the player to the platform so it moves with it.

True. That’s probably how I’ll go about implementing that feature.

The main thing I want to do with the physics is allow the player to climb up and down slopes and run into static obstacles.

That’s tricky to do in VR in general. Adding a dynamic rigidbody to the player shouldn’t be difficult and honestly should ‘just work’. You will need to change the entity.setPosition into rigidbody.teleport though when teleporting around the level.

I managed to get movement up and down slopes working nicely by replacing the movement code with the following.

    this.vec2A.set(x, y);
    
    this.vec2B.x = this.camera.forward.x;
    this.vec2B.y = this.camera.forward.z;
    this.vec2B.normalize(); 
    
    var rad = Math.atan2(this.vec2B.x, this.vec2B.y) - (Math.PI / 2);
        
    var t =        this.vec2A.x * Math.sin(rad) - this.vec2A.y * Math.cos(rad);
    this.vec2A.y = this.vec2A.y * Math.sin(rad) + this.vec2A.x * Math.cos(rad);
    this.vec2A.x = t;
    
    var force = new pc.Vec3();
    force.set(this.vec2A.x,0,this.vec2A.y).scale(3);
    this.entity.rigidbody.applyImpulse(force);

Rotation and teleportation now use the teleport function. May need to clean up the rotation as it’s still doing the full translation on the entity itself and then teleporting the rigidbody from that. But it works for now and hasn’t shown any side-effects yet.

this.entity.rigidbody.teleport(this.entity.getPosition(), this.entity.getRotation());

I’m also trying to get Physics working in my VR Project. Adding a Dynamic Rigid Body + Collision to the Camera Parent works when outside of VR, but as soon as I enter VR, collisions don’t work. I also tried applying these elements to the Camera directly, as well as the Controller models, but none of these elements have correct physics working.

I have a simple ball in the scene that rolls nicely when physics are applied to it, which is how I’m doing my testing.

Jimage, it seems your script is just for collision with the floor, correct? Is there an explanation as to why just adding RigidBody + Collision to the Player or other assets doesn’t “just” work?

Thanks!

My solution works with walls as well.

The main problem was that the original script sets the entity position directly but you need to use rigidbody forces in order for collisions to trigger.

The second teleport was added to the end of the rotation script because otherwise the rigidbody overrides the entity’s rotation whenever it stops moving. So now it teleports the rigidbody to its current position with the entity’s new rotation values. In theory that should probably be replaced with torque forces but since my player collision is just a capsule I don’t need to worry about the rotation triggering collisions.

I used 0.95 linear damping and 20kg for the rigidbody settings. This seems to give good results up and down slopes and comfortable speed on flat ground for both VR movement and the regular first person keyboard script for non-VR mode.

I was having the same issue of joystick with physics. I have resolved that by changing the collider type of Camera parent to Sphere. Here is a link to the project: https://playcanvas.com/editor/scene/1170469

2 Likes