Search Unity

Try to add physics to VR melee combat

Discussion in 'AR/VR (XR) Discussion' started by zoidsidss, Nov 17, 2017.

  1. zoidsidss

    zoidsidss

    Joined:
    Dec 10, 2016
    Posts:
    5
    Hello everyone, over one year I’m trying to add some physical features to the melee combat in VR games because of the amazing experience that VR bring to me. The basic idea is that the position and rotation of the VR controller will be the target position and rotation of the weapon in game, but some physical properties are added to limit the movement of the weapon for preventing meaningless wielding.

    Now the list below are the features that I’m try to do, and perhaps it make sense in VR games.

    1. The weapon in hand is a rigidbody that all external force and torque will change its movement and rotation state, including force and torque applied by hand, gravity or collision. So weapon’s position and rotation will not always be the position and rotation of controller.

    2. While there are no collision happened, the movement of weapons will based on the A. the controller’s position and rotation, B. weapon’s intrinsic property, C. weapon’s intrinsic property, D. strength of the character’s arm and wrist, and F. gravity

    B. Weapon’s intrinsic property included (1) mass, (2) position of the center of mass, (3) moment of inertia, (4) principle axes.
    C. Weapon’s extrinsic properties included (1) velocity and (2) angular velocity.
    D. Strength of the character’s arm and wrist included (1) maximum torque the shoulder can applied, (2) maximum torque the wrist can applied and (3) maximum force the arm can applied.

    3. The parameters mentioned above will be used to calculate the final force and torque that should be applied to the weapon and make it move.

    4. While there are collisions happened, the movement of weapons will based on the previous two principles but an external force from the collisions will be consider in calculation. So a strongly external force from collision will totally change the movement of weapon in hand from the target the player want.

    5. For the weapons with blades which have the ability to penetrate somethings, the (1) contact points of the weapon, (2) weapon’s direction of movement, and (3) component of collision impulse in the direction of blade edge, are used to determine if penetration happened or not when collision is happened.

    6. When penetration is happened, the weapons movement and rotation directions will be restrained depended on (1) the materials of the object, (2) the geometry of the weapon (blade thickness etc.).

    7. To prevent the weapon flying away when it hit a high speed (moment) object, a joint is used to restrain the maximum distance from character to the weapon.


    From 1 to 7 I have made some scripts to realize those concept, but it is still a rough prototype. The two video will demo those features I mentioned above.


    This video demonstrates the penetration determination when collision is happened. And the movement restrain during penetrations. Penetrations happened at 0’28’’, 0’39’’ and 1’22’’.


    This video demonstrates the collision reactions between weapon and an accelerated object.


    Surely the scripts I made and parameters I set are far from ideal and many details should be utilized, but it presented the basic features I wanted. The code that I used contain some physical calculations and math, so if anyone have an interest in the scripts I have made, just let me know, I’m glad to share all the scripts and to explain the physical principles behind the code.

    Otherwise, there are some other features (written below) I want to realize and share with people, but it cost time to transform the concept to the scripts and will take all my efforts. For now I’m still a graduate student and not major in programming, it’s will take long time to make them come true (if they have meanings :p). So… if anyone have the same hobby and want to finish those things I want to realize. You are welcome to contact me lol.

    8. Calculate the reasonable position of the character’s elbow in game use the position and rotation of the controller (same as the position and rotation of hand). If done, we won’t need other motion detector on our arms at all to define the position of the full arms (but we can’t control the character’s elbow position by our elbows too). And the player’s character in the VR games may finally have its all arm be presented, not just hands only lol.

    9. If feature 8 is done, the weapon’s movement should be limited by the movement limitations of joints of wrist, elbow and shoulder. This feature may make the movement (also position and rotation) of the weapon be more reasonable.

    10. Base on the concepts 1 to 3, make a reasonable script to define the movement of a two-hand melee weapon.

    I already have some basic ideas for the above three features. If I have time to make the corresponding script, I will update here lol.

    PS: Thanks to Mikalai Naletska for sharing the great 3D sword model. It’s a pretty nice work!
     
    vochie, IsDon and krougeau like this.
  2. BeyondInfiniti

    BeyondInfiniti

    Joined:
    Jun 24, 2018
    Posts:
    15
    I'm trying to figure out sword penetration in VR but can't find much info on how to do it. Care to help?
     
  3. shadowjohnr

    shadowjohnr

    Joined:
    Feb 21, 2018
    Posts:
    3
    I'm in the same boat with you Beyond. I'm hoping to have something like this for my own project.
     
    BeyondInfiniti likes this.
  4. zoidsidss

    zoidsidss

    Joined:
    Dec 10, 2016
    Posts:
    5
    vochie and BeyondInfiniti like this.
  5. shadowjohnr

    shadowjohnr

    Joined:
    Feb 21, 2018
    Posts:
    3
    From the oc their self! Thank you for the reply zoid! Sometimes it does take awhile before an eye lands where you thought it was not attracted to before. I think this is superbly unique and some really fun stuff you've done. I thank you again for sharing.
     
    Last edited: Jan 24, 2019
    vochie likes this.
  6. BeyondInfiniti

    BeyondInfiniti

    Joined:
    Jun 24, 2018
    Posts:
    15
    Thank you for sharing the scripts! I'm glad that my research led me here.
     
  7. JW86

    JW86

    Joined:
    Jan 11, 2019
    Posts:
    13
    Awesome work, I'm interested in learning more from your knowledge for my own VR project. (Hobby, not-for-profit)

    I'm going to play with the scripts you produced, but am a newb. Would you care to explain a little about the function of the scripts you uploaded, and how to best utilise them, necessity of rigidbody/colliders, etc.? I'm going to start experimenting now but an answer would be really helpful. Best of luck!
     
  8. zoidsidss

    zoidsidss

    Joined:
    Dec 10, 2016
    Posts:
    5
  9. kingburrito666

    kingburrito666

    Joined:
    Jun 28, 2019
    Posts:
    2
    I just want to say, despite how convoluted this code is -- thank you; I don't understand why you store everything in dictionaries.
     
    Last edited: Aug 12, 2019
  10. CRYWOLF86

    CRYWOLF86

    Joined:
    Jul 23, 2016
    Posts:
    18
    Thanks for putting this out there, but this code really can't be worked with. Your variable naming conventions hurt my soul. Its all too convoluted. I'll have an easier time figuring it out myself.
     
  11. Extrys

    Extrys

    Joined:
    Oct 25, 2017
    Posts:
    345
    What I use to do for these things is just creating my own "joints" from scratch
    having a Drive force algorithm should be enough,
    and then you can work on give it custom properties,
    like force curves or even 2 handed holdings or anything

    it is just about trying to match the speed and weapon position
    I part from this simple formula, for the final force addition and the same for rotation.
    force = Spring * (targetPosition - position) + Damper * (targetVelocity - velocity)

    Which can be found in the unity manual
    https://docs.unity3d.com/Manual/class-ConfigurableJoint.html
    In the Drive Forces section

    Then you can process all values before adding the forces and make it super custom
    Like make it use of player force, define different processings for calculating the spring and damp depending on some threshold weight, etc

    In order to get a good code architecture, I recommend to do these algorithms in different instances of plain c# classes, so you can create an instance of any and execute them via a method they have,

    PhyshicGrabAlgorythm algorithm = new PhyshicGrabAlgorythmForShields(rigidbody, anythingElse);


    Code (CSharp):
    1. void FixedUpdate()
    2. {
    3.      algorythm.Execute();
    4. }
     
  12. zoidsidss

    zoidsidss

    Joined:
    Dec 10, 2016
    Posts:
    5