Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Updating position and rotation of physics with velocity simulation

Discussion in 'Physics' started by modegames, Feb 19, 2015.

  1. modegames

    modegames

    Joined:
    Dec 1, 2014
    Posts:
    37
    We are having an issue with updating the position and rotation of game objects and then getting the right effect for collision with various falling objects.

    We wrote and created a small demo scene to test this with a sphere and an oscillating cube the source code is like so for the moving cube.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BoxMoveScript : MonoBehaviour {
    5.  
    6.     public float velocityAmount = 100.0f;
    7.  
    8.     private float position = 0.0f;
    9.     Vector3 startPosition;
    10.    
    11.     void Start () {
    12.         startPosition = this.transform.position;
    13.     }
    14.  
    15.     void FixedUpdate () {
    16.         position += 0.1f;
    17.  
    18.         float offsetY = Mathf.Sin(position)*2.0f;
    19.  
    20.         Vector3 currentPosition = this.transform.position;
    21.         currentPosition.y = startPosition.y + offsetY;
    22.  
    23.         Vector3 previousPosition = this.transform.position;
    24.  
    25.         this.transform.position = currentPosition;
    26.  
    27.         Rigidbody rigidBody = this.rigidbody;
    28.  
    29.         rigidBody.MovePosition (currentPosition);
    30.  
    31.         Vector3 velocity = currentPosition - previousPosition;
    32.  
    33.         velocity *= velocityAmount;
    34.  
    35.         rigidBody.velocity = velocity;
    36.     }
    37. }
    We have to manually set the velocity and multiply by a factor when updating the cubes position, otherwise the sphere does not react to velocity when colliding with the cube underneath it. I suppose the reason for this multiplication factor is to take into consideration mass and gravity and the force required.

    We also found that kinematic bodies which are intended for manually controlled movement for rigid bodies never allows for any transfer of velocity between bodies on collision, so we must manually update a dynamic body and update the velocity ourselves. There doesn't seem to be a way to use Kinematic and have 2 objects which can collide taking into consideration velocity.

    Unfortunately we must set the position and rotation of our character based on input from the sensor so we can't use AddForce or AddTorque or so I believe that wont have the right effect for what we are doing.

    Does anyone have any ideas for the best way to approach this kind of problem?
     
  2. modegames

    modegames

    Joined:
    Dec 1, 2014
    Posts:
    37
    For anyone looking for the answer to this, I'll answer it myself :)

    It was a stupid mistake. I forgot velocity is distance over time not just distance for each time updated, so dividing the calculated velocity by Time.deltaTime makes this feel right!